【Python】matplotlib 雙y軸繪製及合併圖例

1.雙y軸繪製 關鍵函數:twinx()

  問題在於此時圖例會有兩個。dom

 1 # -*- coding: utf-8 -*-
 2 import numpy as np  3 import matplotlib.pyplot as plt  4 from matplotlib import rc  5 rc('mathtext', default='regular')  6 
 7 time = np.arange(10)  8 temp = np.random.random(10)*30
 9 Swdown = np.random.random(10)*100-10
10 Rn = np.random.random(10)*100-10
11 
12 fig = plt.figure() 13 ax = fig.add_subplot(111) 14 ax.plot(time, Swdown, '-', label = 'Swdown') 15 ax.plot(time, Rn, '-', label = 'Rn') 16 ax2 = ax.twinx() 17 ax2.plot(time, temp, '-r', label = 'temp') 18 ax.legend(loc=0) 19 ax.grid() 20 ax.set_xlabel("Time (h)") 21 ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)") 22 ax2.set_ylabel(r"Temperature ($^\circ$C)") 23 ax2.set_ylim(0, 35) 24 ax.set_ylim(-20,100) 25 ax2.legend(loc=0) 26 plt.savefig('0.png')

  每一個句柄對應一個圖例。函數

2. 合併圖例

  1) 僅使用一個軸的legend()函數spa

 1 # -*- coding: utf-8 -*-
 2 import numpy as np  3 import matplotlib.pyplot as plt  4 from matplotlib import rc  5 rc('mathtext', default='regular')  6 
 7 time = np.arange(10)  8 temp = np.random.random(10)*30
 9 Swdown = np.random.random(10)*100-10
10 Rn = np.random.random(10)*100-10
11 
12 fig = plt.figure() 13 ax = fig.add_subplot(111) 14 
15 lns1 = ax.plot(time, Swdown, '-', label = 'Swdown') 16 lns2 = ax.plot(time, Rn, '-', label = 'Rn') 17 ax2 = ax.twinx() 18 lns3 = ax2.plot(time, temp, '-r', label = 'temp') 19 
20 # added these three lines
21 lns = lns1+lns2+lns3 22 labs = [l.get_label() for l in lns] 23 ax.legend(lns, labs, loc=0) 24 
25 ax.grid() 26 ax.set_xlabel("Time (h)") 27 ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)") 28 ax2.set_ylabel(r"Temperature ($^\circ$C)") 29 ax2.set_ylim(0, 35) 30 ax.set_ylim(-20,100) 31 plt.savefig('0.png')

  能夠看到y1軸和y2軸的圖例已經合併了3d

 

  2)使用figure.legend()code

 1 # -*- coding: utf-8 -*-
 2 import numpy as np  3 import matplotlib.pyplot as plt  4 
 5 x = np.linspace(0,10)  6 y = np.linspace(0,10)  7 z = np.sin(x/3)**2*98
 8 
 9 fig = plt.figure() 10 ax = fig.add_subplot(111) 11 ax.plot(x,y, '-', label = 'Quantity 1') 12 
13 ax2 = ax.twinx() 14 ax2.plot(x,z, '-r', label = 'Quantity 2') 15 fig.legend(loc=1) 16 
17 ax.set_xlabel("x [units]") 18 ax.set_ylabel(r"Quantity 1") 19 ax2.set_ylabel(r"Quantity 2") 20 
21 plt.savefig('0.png')

  能夠看到圖例位置不對,已經出界,須要使用bbox_to_anchor和bbox_transform設置。orm

  fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax.transAxes)blog

 1 # -*- coding: utf-8 -*-
 2 import numpy as np  3 import matplotlib.pyplot as plt  4 
 5 x = np.linspace(0,10)  6 y = np.linspace(0,10)  7 z = np.sin(x/3)**2*98
 8 
 9 fig = plt.figure() 10 ax = fig.add_subplot(111) 11 ax.plot(x,y, '-', label = 'Quantity 1') 12 
13 ax2 = ax.twinx() 14 ax2.plot(x,z, '-r', label = 'Quantity 2') 15 fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax.transAxes) 16 
17 ax.set_xlabel("x [units]") 18 ax.set_ylabel(r"Quantity 1") 19 ax2.set_ylabel(r"Quantity 2") 20 
21 plt.savefig('0.png')

   能夠看到圖例已經正常了。three

 

轉自:StackOverflowutf-8

相關文章
相關標籤/搜索