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.

Jacob
  • 21
  • 4
  • 3
    Please do not put text and code as images and copy them into codeblocks. – pu239 Aug 15 '21 at 08: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. – JohanC Aug 15 '21 at 14:53
  • You will get more and better help if you provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/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](https://github.com/mwaskom/seaborn-data), such as `fmri = sns.load_dataset("fmri")` – a11 Aug 15 '21 at 15:28

0 Answers0