matplotlib.subplot()画子图并共享y坐标轴的方法
有时候想要把几张图放在一起plot,比较好对比,subplot和subplots都可以实现,具体对比可以查看参考博文。这里用matplotlib库的subplot来举个栗子。
数据长什么样
有两个数据段,第一个数据是DataFrame类型,第二个是ndarray类型。每个数据都有3列,我想画1*3的折线子图,第一个数据的第n列和第二个数据的第n列画在一张子图上。先来看一下两个数据长什么样儿(为显示方便,只看前5行)。
In [1]: testing_set.head() # DataFrame类型
Out [1]: Open High Low
0 778.81 789.63 775.80
1 788.36 791.34 783.16
2 786.08 794.48 785.02
3 795.26 807.90 792.20
4 806.40 809.97 802.83 # ndarray类型
In [2]: predicted_stock_price #这里就只看前5行
Out [2]: [[790.6911 796.39215 779.3191 ]
[790.24524 796.0866 778.9673 ]
[789.5737 795.52606 778.3751 ]
[790.1047 796.10864 778.92395]
[790.8686 796.94104 779.7281 ]]
实现过程
注:plt.setp()是用来共享y坐标轴
# 创建画布
fig = plt.figure(figsize = (30,10), dpi = 80)
# 子图1
ax1 = plt.subplot(131)
ax1.set_title('Open Price')
ax1.plot(testing_set.values[:,0], color = 'red', label = 'Real Open Price')
ax1.plot(predicted_stock_price[:,0], color = 'blue', label = 'Predicted Open Price')
plt.setp(ax1.get_xticklabels(), fontsize=6)
ax1.legend()
# 子图2
ax2 = plt.subplot(132,sharey=ax1)
ax2.set_title('High Price')
ax2.plot(testing_set.values[:,1], color = 'red', label = 'Real High Price')
ax2.plot(predicted_stock_price[:,1], color = 'blue', label = 'Predicted High Price')
ax2.legend()
# 子图3
ax3 = plt.subplot(133,sharey=ax1)
ax3.set_title('Low Price')
ax3.plot(testing_set.values[:,2], color = 'red', label = 'Real Low Price')
ax3.plot(predicted_stock_price[:,2], color = 'blue', label = 'Predicted Low Price')
ax3.legend()
plt.show()
结果:
(资源库 www.zyku.net)
原文链接:https://blog.csdn.net/m0_38125087/article/details/106593500
栏 目:Python教程
下一篇:详解python字符串驻留技术
本文标题:matplotlib.subplot()画子图并共享y坐标轴的方法
本文地址:https://www.zyku.net/python/9717.html
您可能感兴趣的文章
- 02-09matplotlib共享坐标轴的实现(X或Y坐标轴)
- 11-26抖音热点关联在哪开
- 02-22DedeCMS导入采集规则提示“该规则不合
- 12-28迅车通-迅车通应用软件功能介绍
- 04-04ubuntu下Chrome提示输入秘钥环的解决
- 02-28Windows服务器更改administrator账号
- 01-14壹达外卖-壹达外卖应用软件功能介绍
- 02-17在建设url时到底需要注意哪些问题啊?
- 03-10Sublime Text3下配置SublimeLinter进
- 10-30QQ新增个人信息隐私保护功能介绍
- 01-12速记记加班-速记记加班应用软件功能介
- 09-23华为平板恢复出厂设置方法分享
- 09-12真我gtneo2怎么打开5G网络
- 07-05Linux sudo命令
- 12-25小学数学知识归纳-小学数学知识归纳应
- 09-19word文档如何更改背景图片
- 01-12特效变音魔术师-特效变音魔术师应用软
- 12-29氚云办公-氚云办公应用软件功能介绍
- 04-10小米11青春版开启广角模式方法
- 12-22HTML <html> 标签
最近更新
阅读排行
猜你喜欢
- 07-05Linux bunzip2命令
- 03-04wordpress防止垃圾评论的几种方法
- 04-16Dedecms修改MySql端口号的方法
- 12-09iqoo8怎么启用双击亮屏
- 01-08PPT免费模板-PPT免费模板应用软件功能
- 09-24微信在哪里设置密码支付
- 02-21MySQL SET PASSWORD语法详解
- 11-27苹果13定位找回步骤分享
- 09-20华为手机小艺建议如何添加至桌面
- 09-28支付宝参与喜茶盲盒步骤一览