【Python】matplotlib 雙y軸繪制及合並圖例


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

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

 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()函數

 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軸的圖例已經合並了

 

  2)使用figure.legend()

 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設置。

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

 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')

   可以看到圖例已經正常了。

 

轉自:StackOverflow


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM