2

This is related to this and this question.

I have a sequence of figures and subplots that have all very similar settings. However, I can't seem to find a way to set them all at the same time. Here's a simplified version (I generally work with more instances):

fspec = plt.figure(1)
spC = fspec.add_subplot(211)
spC.set_title('Surface concentrations')
spC.grid(True)
spC.set_ylim(1e-3, None)
spT = fspec.add_subplot(212, sharex=spC)
spT.set_title('Surface temperature')
spT.grid(True)
spT.set_ylim(1e-3, None)

fener = plt.figure(2)
enC = fener.add_subplot(211)
enC.set_title('Surface concentrations')
enC.grid(True)
enC.set_ylim(1e-3, None)
enT = fener.add_subplot(212, sharex=enC)
enT.set_title('Surface temperature')
enT.grid(True)
enT.set_ylim(1e-3, None)

I feel like there should be a way to apply something to every subplot open, or AT LEAST every subplot in a figure. Something like

fspec.set_global_grid(True)
fspec.set_global_ylim(1e-3, None)

But I can't find it.

I took a look at some of the previous but none of them seem to work for me, since I don't work with one figure or axis at a time, I work with all of them kind of at the same time.

Cheers.

Community
  • 1
  • 1
TomCho
  • 3,204
  • 6
  • 32
  • 83

1 Answers1

10

Some settings concerning mostly the style of the figure can be set globally using the matplotlib rc parameters. For example, setting the grid on throughout the script, put

plt.rcParams['axes.grid'] = True

at the beginning of your file (after the imports).

Other things like axis limits are really specific to the plot itself, and there is no global parameter for that. But you can still go the way, as outlined in the linked questions, i.e. write your own function that does most of the stuff you need.

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['axes.grid'] = True

def plotfunction(fig, x1, y1, x2, y2,
                 title1 = 'Surface concentrations',
                 title2 = 'Surface temperature', **kwargs ):
    ax = fig.add_subplot(211)
    ax2 = fig.add_subplot(212, sharex=ax)
    ax.set_title(title1)
    ax2.set_title(title2)
    ax.set_ylim(1e-3, None)
    ax2.set_ylim(1e-3, None)
    ax.plot(x1, y1, **kwargs)
    ax2.plot(x2, y2, **kwargs)


fspec = plt.figure(1)
fener = plt.figure(2)

x1, y1, x2, y2 = np.loadtxt("spectrum.txt", unpack=True)
plotfunction(fspec,x1, y1, x2, y2)

x1, y1, x2, y2 = np.loadtxt("energy.txt", unpack=True)
plotfunction(fener,x1, y1, x2, y2, linewidth=3)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • The `matplotlib.rc` tip is good, but I don't the function approach works particularly well for me. Is there a way to set `plt.legend()` globally, just like you did with the grid? – TomCho Jan 31 '17 at 00:27
  • Legend is specific to the plot, it contains the plot's artist handles; thus it's pretty obvious that it cannot be set globally. But of course you can integrate it into the function to have it set automatically. If you cannot go into detail on why this function approach isn't working for you, I fear you'll find little help here. – ImportanceOfBeingErnest Jan 31 '17 at 00:33