【发布时间】:2015-08-18 00:13:39
【问题描述】:
我是 python 新手,我正在尝试创建一系列子图,唯一的参数变化是每个图的 fill_between 参数。目前我重复整个代码并更改每个子图的 fill_between 。有没有一种更有效的方法来为子图创建循环,其中唯一改变的是 fill_between?下面是我试图制作的地块类型的一个例子。
import numpy as np
import matplotlib.pyplot as plt
#data
data1 = [11,20,25,80]
data2 = [15,35,50,90]
data3 =[25,36,58,63]
data4=[30,40,68,78]
element = np.arange(4)
fig = plt.figure()
#first plot
ax1 = fig.add_subplot(2,2,1)
phase1 = ax1.plot(data1,color='blue', lw=2)
phase2 = ax1.plot(data2,color='blue', lw=2)
phase3 = ax1.plot(data3,color='green', lw=2)
phase4 = ax1.plot(data4,color='green', lw=2)
plt.xticks(element,('La','Ce','Pr','Nd'))
ax1.fill_between(element,data1,data2,color='grey')
ax1.set_yscale('log')
fig.set_size_inches(10,5)
#second plot
ax2 = fig.add_subplot(2,2,2,sharex=ax1,sharey=ax1)
phase1 = ax2.plot(data1,color='blue', lw=2)
phase2 = ax2.plot(data2,color='blue', lw=2)
phase3 = ax2.plot(data3,color='green', lw=2)
phase4 = ax2.plot(data4,color='green', lw=2)
plt.xticks(element,('La','Ce','Pr','Nd'))
#the fill is the ONLY thing to change
ax2.fill_between(element,data3,data4,color='red')
ax2.set_yscale('log')
plt.show()
【问题讨论】:
标签: python loops matplotlib subplot