怎么用matplotlib画子图并共享y坐标轴

发布时间:2022-02-25 16:32:05 作者:iii
来源:亿速云 阅读:211

这篇文章主要介绍了怎么用matplotlib画子图并共享y坐标轴的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么用matplotlib画子图并共享y坐标轴文章都会有所收获,下面我们一起来看看吧。

数据长什么样

有两个数据段,第一个数据是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()

关于“怎么用matplotlib画子图并共享y坐标轴”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“怎么用matplotlib画子图并共享y坐标轴”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. python中如何使用matplotlib画盒图、子图解决坐标轴标签重叠的问题
  2. matplotlib给子图添加图例的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

matplotlib

上一篇:List集合去重的方法有哪些

下一篇:AmazeUI基础导航条实现效果的代码怎么写

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》