这与此问题有关。
我有一系列数字和子图,它们都具有非常相似的设置。 但是,我似乎无法找到一种方法同时设置它们。 这是一个简化版本(我通常使用更多实例):
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)我觉得应该有一种方法可以在每个子图中打开一个东西,或者至少在图中的每个子图中都应用。 就像是
fspec.set_global_grid(True) fspec.set_global_ylim(1e-3, None)但我找不到它。
我看了一下之前的一些,但它们似乎都不适合我,因为我一次不使用一个图形或轴,我同时使用它们。
干杯。
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.
最满意答案
可以使用matplotlib rc参数全局设置一些主要涉及图形样式的设置。 例如,在整个脚本中设置网格,放置
plt.rcParams['axes.grid'] = True在文件的开头(导入后)。
像轴限制这样的其他东西确实特定于绘图本身,并且没有全局参数。 但是你仍然可以按照链接问题的方式进行操作,即编写自己的函数来完成你需要的大部分工作。
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)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'] = Trueat 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)更多推荐
发布评论