如何在倍数子图matplotlib中重复一个图

时间:2017-05-02 16:28:06

标签: python matplotlib

我需要重复一个气候图(fill_between(x,y1,y2)在多个子图中,是否存在解决问题的提示?这是我的代码的一部分。

from matplotlib import pyplot as plt
plt.figure()
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True)
ax = axs[0,0]
ax.fill_between(month, y2,y3 , alpha=0.25, color='grey')
ax.errorbar(a1[0], a1[1], a1[2], marker='o')

ax = axs[0,1]
ax.fill_between(month, y2,y3 , alpha=0.25, color='grey')
ax.errorbar(a2[0], a2[1], a2[2], marker='o')

ax = axs[1,0]
ax.fill_between(month, y2,y3 , alpha=0.25, color='grey')
ax.errorbar(a3[0], a3[1], a3[2], marker='o')

ax = axs[1,1]
ax.fill_between(month, y2,y3 , alpha=0.25, color='grey')
ax.errorbar(a4[0], a4[1], a4[2], marker='o')

数据a1,a2,a3,...... a24因为24年来自

a1 = graf1.graf1('mon1992.dat')
a2 = graf1.graf1('mon1993.dat')
a3 = graf1.graf1('mon1994.dat')
a4 = graf1.graf1('mon1995.dat')   

graf1是一个模块

import pandas as pd
def graf1(archivo):

archivo = '/Users/ccasiccia/Desktop/research/dataO3_180/' + archivo
data2 = pd.read_csv(archivo, delim_whitespace = True, header=None)

xd2 = data2.ix[:,0]
yd2 = data2.ix[:,1]
sd2 = data2.ix[:,2]

return xd2, yd2, sd2

考虑到与graf1功能我需要建立与副区4(2×2),这里的问题图获得的数据:如何可以添加バ情节(ax.fill_between(月,Y2,Y3,α-= 0.25,颜色=每个子图中都有“灰色”?但不是我怎么做,重复每个子图上的指令。 按照数据一年(例如mon1996.dat)

1   290.1931    23.21468
2   280.32778   17.70719
3   274.70455   19.08037
4   292.43913   27.8067
5   292.49667   24.57176
6   301.64667   26.96397
7   323.13889   20.30883
8   319.76  22.01486
9   306.432 20.07016
10  310.54444   45.90831
11  341.484 27.99424
12  300.71935   12.98657

这是fill_between的气候数据

1   322.418 20.25   20.287  342.668 302.168
2   315.1   21.534  21.578  336.634 293.566
3   293.268 23.694  23.738  316.962 269.574
4   292.928 26.499  26.55   319.427 266.429
5   301.565 31.153  31.21   332.718 270.412
6   304.135 35.883  35.953  340.018 268.252
7   317.792 36.85   36.916  354.642 280.942
8   321.36  35.798  35.863  357.158 285.562
9   324.558 33.472  33.535  358.03  291.086
10  336.043 45.679  45.762  381.722 290.364
11  338.736 33.518  33.58   372.254 305.218
12  327.578 27.093  27.144  354.671 300.485

1 个答案:

答案 0 :(得分:0)

根据数据的组织方式,可以很容易地循环绘制图表以填充它们。

import numpy as np
from matplotlib import pyplot as plt

month=np.linspace(1,8)
y2 = -0.15*(month-4)**2+2.3
y3 = 0.1*(month-3.7)**2
x = np.logspace(1,5,base=1.5, num=16).reshape(4,4).T
y = np.sinc(x-3)**2+1
yerr = np.sqrt(y)


fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True)
for i, ax in enumerate(axs.flatten()):
    ax.fill_between(month, y2,y3 , alpha=0.25, color='gold')
    ax.errorbar(x[:,i], y[:,i],yerr[:,i], marker='o')

plt.show()

enter image description here