Reputation: 3507
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.
Upvotes: 2
Views: 25839
Reputation: 339052
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)
Upvotes: 10