0

I have made several matplotlib subplots in a single figure using gridspec and setting the desired coordinate for each subplot. However, the frame/edges of the subplots do not show and I cannot seem to fix this by setting edgecolor for each subplot frame individually. I am only able to set facecolor for each subplot but there seems to be no option to set the edge color too. this has left me with subplots which have all the tickamarks but no frame/bounding box for them.

I am attaching my code below and a snapshot of the output.

`

   # Create a function to make the figures

    def make_plot(x, y, xerr, yerr, marker, mfc, legend, y_label):
        plot = plt.errorbar(x, y, yerr=yerr, xerr=xerr,  marker=marker,
                        linestyle='none', alpha=.9, label=legend, mfc=mfc, capsize=3,
                        ms=8, ecolor='black', elinewidth=0.7, capthick=0.5, mec='black', color     ='black'), 
    plt.xlabel(r'MgO (wt.%)', size=10, font='Arial'),
    plt.ylabel(y_label, size=10, font='Arial'),
    plt.tick_params(axis='both', which='major', labelsize=9), 
    plt.tick_params(direction='in', which='minor', length=3, bottom=True, top=True, left=True, right=True), 
    plt.tick_params(direction='in', right=True, top=True, left=True, bottom=True), 
    plt.minorticks_on(), 
    plt.grid(False)
    return plot

    # Make several errorbar plots as subplots of a single figure.
    fig = plt.figure(1, figsize=(7, 7))
    # fig.set_facecolor('white')
    fig.set_edgecolor('black')

    gs = gridspec.GridSpec(4, 4)
    gs.update(wspace=1.5, hspace=0.6)

    #Make first panel
    xtr_subplot = fig.add_subplot(gs[0:2, 0:2])
    xtr_subplot.set_facecolor('white')
    for i in range(8):
    make_plot(x[i], avg_si[i], x_err[i], avg_si_err[i],
              markers[i], mfc[i], legend[i], y_labels[0])
    #Make 4th panel
    xtr_subplot = fig.add_subplot(gs[2:4, 2:4])
    xtr_subplot.set_facecolor('white')
    for i in range(8):
        make_plot(x[i], avg_Ca[i], x_err[i], avg_Ca_err[i],
              markers[i], mfc[i], legend[i], y_labels[3])

output figure of subplots

I tried setting my desired edgecolor to the entire figure like so:

fig = plt.figure(1, figsize=(7, 7)) fig.set_facecolor('white') fig.set_edgecolor('black')

However, this did not fix it. Also, I could not find any way to set the edgecolor for each subplot. This is strange since the facecolor for each subplot can be set using fig.set_facecolor('white') for example.

O. Edward
  • 3
  • 4
  • According to [changing the color of a plot frame in matplotlib](https://stackoverflow.com/questions/7778954/elegantly-changing-the-color-of-a-plot-frame-in-matplotlib) : `plt.setp(xtr_subplot.spines.values(), color='black')` for both places where you create `xtr_subplot`. Matplotlib uses the term "spines" for "the edges of a subplot". Note that subplots are usually named `ax` (or a variant) in matplotlib's documentation and examples. Also note that it's a recommended coding practice to give different names to different subplots. – JohanC Nov 01 '22 at 17:34
  • I kinda gave up on this for a couple of days but only to come back and see that @JohanC answered. Thanks a lot. It worked. Also, i have now renamed the different subplots. Thanks for the help. – O. Edward Nov 08 '22 at 08:24

0 Answers0