双Y轴图形Legend配置

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    x = np.arange(0,101)
    wn = np.random.rand(101)
y1 = -1+0.02*x+0.1*wn  
y2 = np.sin(0.5*x)
y3 = 0.0001*x**2-0.03*x+1
y4 = 0.8*np.cos(x)
 
fig,ax1 = plt.subplots()
ax1.scatter(x,y1,c='r',marker ='*',label = 'line1')
ax1.plot(x,y2,'g-o',label = 'line2')
 
ax2 = ax1.twinx()
ax2.plot(x,y3,'b-+',label='line3')
ax2.plot(x,y4,'m--',label='line4')
 
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines1 + lines2, labels1 + labels2, loc=0)
 
plt.show()

  

 

posted @   华小电  阅读(230)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示