【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

posted @ 2018-03-08 20:39  Vincent丶丶  阅读(102044)  评论(1编辑  收藏  举报