【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')
可以看到图例已经正常了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App