1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| from mpl_toolkits.axisartist.parasite_axes import HostAxes, ParasiteAxes import matplotlib.pyplot as plt import numpy as np
ao = pd.read_csv("AO.txt",sep='\s+',header=None, names=['year','month','AO']) ao_dec = ao[ao.month==12] ao_jan = ao[ao.month==1] ao_feb = ao[ao.month==2] ao_djf = (ao_jan.AO.values+ao_feb.AO.values+ao_dec.AO.values)/3
fig = plt.figure()
ax = HostAxes(fig, [0, 0, 0.9, 0.9])
ax1 = ParasiteAxes(ax, sharex=ax) ax2 = ParasiteAxes(ax, sharex=ax) ax3 = ParasiteAxes(ax, sharex=ax) ax.parasites.append(ax1) ax.parasites.append(ax2) ax.parasites.append(ax3)
ax.axis['right'].set_visible(False) ax1.axis['right'].set_visible(True) ax1.axis['right'].major_ticklabels.set_visible(True) ax1.axis['right'].label.set_visible(True)
ax.set_ylabel('DJF') ax.set_xlabel('year') ax1.set_ylabel('Dec') ax2.set_ylabel('Jan') ax3.set_ylabel('Feb')
axisline2 = ax2.get_grid_helper().new_fixed_axis axisline3 = ax3.get_grid_helper().new_fixed_axis ax2.axis['right2'] = axisline2(loc='right', axes=ax2, offset=(40,0)) ax3.axis['right3'] = axisline3(loc='right', axes=ax3, offset=(80,0))
fig.add_axes(ax)
ax.plot(np.arange(1950,2020,1), ao_djf, label="DJF", color='black') ax1.plot(np.arange(1950,2020,1), ao_dec.AO, label="Dec", color='red') ax2.plot(np.arange(1950,2020,1), ao_jan.AO, label="Jan", color='green') ax3.plot(np.arange(1950,2020,1), ao_feb.AO, label="Feb", color='orange') ax2.set_ylim(-4,4) ax3.set_ylim(-5,5) ax4.set_ylim(-6,6) ax.legend()
ax1.axis['right'].major_ticks.set_color('red') ax2.axis['right2'].major_ticks.set_color('green') ax3.axis['right3'].major_ticks.set_color('blue') ax1.axis['right'].major_ticklabels.set_color('red') ax2.axis['right2'].major_ticklabels.set_color('green') ax3.axis['right3'].major_ticklabels.set_color('blue') ax1.axis['right'].line.set_color('red') ax2.axis['right2'].line.set_color('green') ax3.axis['right3'].line.set_color('blue')
|