2

I am trying to plot three graphs in one row. However, seaborn regplot is popping with error message: IndexError: too many indices for array Here is my Code:

slope, intercept, rv1, pv1, std_err = 
stats.linregress(ban_fc['TOTAL'],ban_fc['pm10'])
slope, intercept, rv2, pv2, std_err = 
stats.linregress(dma_fc['TOTAL'],dma_fc['pm10'])
slope, intercept, rv3, pv3, std_err = 
stats.linregress(prd_fc['TOTAL'],prd_fc['pm10'])

sns.set_style('whitegrid')
with sns.color_palette(n_colors=1):
    fig, axs = plt.subplots(1,3, figsize=(18,8),sharex=True, 
    sharey=True)
    sns.set(font_scale = 1.3)
    sns.regplot(x='TOTAL', y='pm10', 
    data=ban_fc,ax=axs[0,0],color='gray',ci=None, 
    label="r={0:.1f} p={1:.1f}".format(rv1, pv1)).legend(loc="best")
    sns.regplot(x='TOTAL', y='pm10', 
    data=dma_fc,ax=axs[0,1],color='gray',ci=None, 
    label="r={0:.1f} p={1:.1f}".format(rv2, pv2)).legend(loc="best")
    sns.regplot(x='TOTAL', y='pm10', 
    data=prd_fc,ax=axs[0,2],color='gray',ci=None, 
    label="r={0:.1f} p={1:.1f}".format(rv3, pv3)).legend(loc="best")

    for ax in axs.flat:
        ax.set(xlabel='Fire Count',ylim=(0,8500))

    for ax in axs.flat:
        ax.set(ylabel='PM$_1$$_0$('r'$\mu$g/m$^3$)',ylim=(0,200))
    fig.tight_layout()

When I increase the number of subplot rows to two the error disappeared, but the plot messed up with an extra blank subplot.

slope, intercept, rv1, pv1, std_err = 
stats.linregress(ban_fc['TOTAL'],ban_fc['pm10'])
slope, intercept, rv2, pv2, std_err = 
stats.linregress(dma_fc['TOTAL'],dma_fc['pm10'])
slope, intercept, rv3, pv3, std_err = 
stats.linregress(prd_fc['TOTAL'],prd_fc['pm10'])

sns.set_style('whitegrid')
with sns.color_palette(n_colors=1):
    fig, axs = plt.subplots(2,3, figsize=(18,8),sharex=True, 
    sharey=True)
    sns.set(font_scale = 1.3)
    sns.regplot(x='TOTAL', y='pm10', 
    data=ban_fc,ax=axs[0,0],color='gray',ci=None, 
    label="r={0:.1f} p={1:.1f}".format(rv1, pv1)).legend(loc="best")
    sns.regplot(x='TOTAL', y='pm10', 
    data=dma_fc,ax=axs[0,1],color='gray',ci=None, 
    label="r={0:.1f} p={1:.1f}".format(rv2, pv2)).legend(loc="best")
    sns.regplot(x='TOTAL', y='pm10', 
    data=prd_fc,ax=axs[0,2],color='gray',ci=None,
    label="r={0:.1f} p={1:.1f}".format(rv3, pv3)).legend(loc="best")

    for ax in axs.flat:
        ax.set(xlabel='Fire Count',ylim=(0,8500))

    for ax in axs.flat:
        ax.set(ylabel='PM$_1$$_0$('r'$\mu$g/m$^3$)',ylim=(0,200))
    fig.tight_layout()

Any help or suggestions are appreciated.

3
  • 3
    Please do not put text and code as images and copy them into codeblocks. Commented Aug 15, 2021 at 8:07
  • 1
    You can use fig, axs = plt.subplots(1,3, ..., squeeze=False) to always make axs a 2D array. Default squeeze=True which makes the axs array 1D when there is only one row or one column. Commented Aug 15, 2021 at 14:53
  • You will get more and better help if you provide a Minimal, Reproducible Example. This means including a sample of your dataframe. It is easy to create a simple dummy dataframe (df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})), or you can use the stock example datasets provided by Seaborn, such as fmri = sns.load_dataset("fmri") Commented Aug 15, 2021 at 15:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.