python绘图时如何添加图例_【Python】matplotlib双y轴绘制
及合并图例
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.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.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)
matplotlib中subplotax.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_ansAxes)
# -*- 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_ansAxes)
ax.set_xlabel("x [units]")
ax.set_ylabel(r"Quantity 1")
ax2.set_ylabel(r"Quantity 2")
plt.savefig('0.png')
可以看到图例已经正常了。
matplotlib 双y轴绘制及合并图例
Python教程:matplotlib 绘制双Y轴曲线图
前⾔ 本⽂的⽂字及图⽚来源于⽹络,仅供学习.交流使⽤,不具有任何商业⽤途,版权归原作者所有,如有问题请及时以作处理. 作者:数据⽪⽪侠 双X轴的可以理解为共享y轴 ax1=ax.twiny() ...
Python科学计算技巧积累四——双y轴图像绘制
双y轴图像具有单y轴图像没有的对⽐效果,在MATLAB中有plotyy函数可以实现,Python的实现⽅式没有MATLAB那样⽅便,不过实现效果却也不见得差. 以往我常⽤的绘图命令是import ma ...
绘制复数图形和双y轴图形
clearclct=0:0.1:2*pi;x=sin(t);y=cos(t);z=x+i*y;subplot(1,3,1)plot(t,z,'r') %注:这种⽅式下,不论参数t,z哪个是复数,都将忽 ...
highchart 设置双Y轴坐标 双x轴坐标⽅法
我们的图表⼀旦引⼊了两种不同单位或者数量级相差很⼤的数据以后,这时候需要两种坐标对其进⾏计量. 下⾯以设置双Y轴为例, y轴坐标的参数设置成: yAxis: [{ title: { text: '坐标 ...
Jqplot使⽤总结之⼆(双Y轴)
最近需要⽤Jqplot做双Y轴的Chart图,⾸先我到了⽂档上的例⼦并对数据做了⼀些调整: 1.例⼦展⽰: var s1 = [["2002-01-01", 112000], [ ...
MSChart使⽤之双Y轴使⽤
protected void SearchChart() { Chart1.ChartAreas.Clear(); Chart1.Series.Clear(); ChartArea _ChartAre ...
echarts使⽤笔记四:双Y轴
1.双Y轴显⽰数量和占⽐ app.title = '坐标轴刻度与标签对齐'; option = { title : { //标题 x : 'center', y : 5, text : '数量和占⽐图 ...
matlab画⼆维直⽅图以及双y轴坐标如何修改另⼀边y轴的颜⾊
1.⾸先讲⼀下如何⽤hist画⼆维直⽅图 x=[- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...
随机推荐
【代码笔记】iOS-向服务器传JSON数据的两种⽅式
⼀,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...
css+js定位到屏幕中间
ex:让⼀个div始终显⽰在屏幕中间 ⼀. css:#idName{position: absolute;z-index: 999;width: ?px;margin-top: ?px;}//此处的初 ...
Oracle⾝份认证⽅式
Oracle对于普通账户和超级管理员(指sysdba和sysoper)的认证机制不⼀样,前者是通过数据字典,后者主要是通过操作系统验证和密码⽂件验证.因此⼀般提到操作系统认证或密码⽂件认证,针对的都是 ...
excel vba 当cell的值变化时 进⾏判断操作
⽰例效果: ----------- 在excel的sheet1中, 当A列的值 ⼤于100时 ,其对应B列背景显⽰红⾊,C列显⽰"有数据" 否则,B列背景⾊正常,C列清空相应的数据 ...
从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第⼆讲 WPF中 绑定
说到WPF, 当然得从绑定说起,这也是WPF做的很成功的⼀个地⽅,这也是现在⼤家伙都在抛弃使⽤winform的其中⼀个主要原因,Binding这个东西从早说到完其实都说不完的,我先就做⼀些基本的介绍, ...
ViewPager使⽤笔记
1.ViewPager.setCurrentItem(position),即使已设置动画,但是没有动画效果 原因:因为ViewPager滑动之前的时间间隔太短,可以通过反射,去修改ViewPager⾃ ...
JVM的参数设置与OutOfMemoryError异常关系
Java堆中存放Object对象数据,例如new出来的Object.当没有任何引⽤指向某对象时,该对象可能被垃圾回收.有关垃圾回收算法,可参考其他有关⽂章,⽹上很多.关于对象引⽤,按强弱还有强引⽤,软 ...
ArcGIS ⽹络分析[2.4] OD成本矩阵
什么是OD成本矩阵? 先不说这个东西是什么,我们还是举⼀个实际的例⼦: 现在存在3个城市北京.上海.武汉,请分析他们两两之间的通⾏时间.很简单嘛!北京到上海,北京到武汉,上海到武汉都来⼀次最短路径分 ...
XCopy命令实现增量备份
xcopy XCOPY是COPY的扩展,可以把指定的⽬录连⽂件和⽬录结构⼀并拷贝,但不能拷贝系统⽂件:使⽤时源盘符.源⽬标路径名.源⽂件名⾄少指定⼀个:选⽤/S时对源⽬录下及其⼦⽬录下的所有⽂件进⾏C ...
JavaScript常⽤代码书写规范
javascript 代码规范 代码规范我们应该遵循古⽼的原则:“能做并不意味着应该做”. 全局命名空间污染 总是将代码包裹在⼀个⽴即的函数表达式⾥⾯,形成⼀个独⽴的模块. 不推荐 , y = ; c ...

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。