1.双y轴绘制 关键函数:twinx()

  问题在于此时图例会有两个。

 # -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular') time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10 fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(time, Swdown, '-', label = 'Swdown')
ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
ax2.plot(time, temp, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)")
ax2.set_ylabel(r"Temperature ($^\circ$C)")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
ax2.legend(loc=0)
plt.savefig('0.png')

  每个句柄对应一个图例。

2. 合并图例

  1) 仅使用一个轴的legend()函数

 # -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular') time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10 fig = plt.figure()
ax = fig.add_subplot(111) lns1 = ax.plot(time, Swdown, '-', label = 'Swdown')
lns2 = ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
lns3 = ax2.plot(time, temp, '-r', label = 'temp') # added these three lines
lns = lns1+lns2+lns3
labs = [l.get_label() for l in lns]
ax.legend(lns, labs, loc=0) ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)")
ax2.set_ylabel(r"Temperature ($^\circ$C)")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
plt.savefig('0.png')

  可以看到y1轴和y2轴的图例已经合并了

  2)使用figure.legend()

 # -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt x = np.linspace(0,10)
y = np.linspace(0,10)
z = np.sin(x/3)**2*98 fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y, '-', label = 'Quantity 1') ax2 = ax.twinx()
ax2.plot(x,z, '-r', label = 'Quantity 2')
fig.legend(loc=1) ax.set_xlabel("x [units]")
ax.set_ylabel(r"Quantity 1")
ax2.set_ylabel(r"Quantity 2") plt.savefig('0.png')

  可以看到图例位置不对,已经出界,需要使用bbox_to_anchor和bbox_transform设置。

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

 # -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt x = np.linspace(0,10)
y = np.linspace(0,10)
z = np.sin(x/3)**2*98 fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y, '-', label = 'Quantity 1') ax2 = ax.twinx()
ax2.plot(x,z, '-r', label = 'Quantity 2')
fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax.transAxes) ax.set_xlabel("x [units]")
ax.set_ylabel(r"Quantity 1")
ax2.set_ylabel(r"Quantity 2") plt.savefig('0.png')

   可以看到图例已经正常了。

转自:StackOverflow

最新文章

  1. html之select标签
  2. ActiveX: 如何用.inf和.ocx文件生成cab文件
  3. Majority Element
  4. Web Api 2 怎么支持 Session
  5. UVA 524
  6. [转] 整理了一份React-Native学习指南
  7. DTD简单使用
  8. CF-599B - Spongebob and Joke
  9. Unity3D 游戏开发架构篇 ——性格一流的设计和持久性
  10. jquery按钮倒计时
  11. Struts2 中添加 Servlet
  12. 开发人员需要熟知的常用Linux命令Version、Kernel查看
  13. 使用LSTM和Softmx来进行意图识别
  14. python绘制等边三角形
  15. [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction
  16. 安装站点时出现“连接数据库出现数据库server或登录password无效,无法连接数据库,请又一次设定”解决方法
  17. 语音VLAN异常流量分析
  18. 关于MySQL慢日志,你想知道的都在这
  19. Visual Studio 2010 出现关于ActivityLog.xml错误的解决方案
  20. C语言复习:编译

热门文章

  1. VS2010/MFC编程入门之二十一(常用控件:编辑框Edit Control)
  2. laravel 中间件排除
  3. RC1意思
  4. Python3.x:os.mkdir与 os.makedirs(创建目录方法)区别
  5. Python3.x:open()文件操作
  6. MAC nginx代理设置
  7. ubinize的用法
  8. python swap
  9. TC 配置插件
  10. 谈谈刚接触sea.js框架得看法