matplotlib.subplot()画子图并共享y坐标轴的方法

作者:UU吴 时间:2023-03-27 12:26:10 

有时候想要把几张图放在一起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()

结果:

matplotlib.subplot()画子图并共享y坐标轴的方法

来源:https://blog.csdn.net/m0_38125087/article/details/106593500

标签:matplotlib.subplot(),子图
0
投稿

猜你喜欢

  • python 使用OpenCV进行简单的人像分割与合成

    2021-06-16 20:46:39
  • vue中el-table两个表尾合计行联动同步滚动条实例代码

    2023-07-02 17:08:56
  • pygame游戏之旅 创建游戏窗口界面

    2022-05-19 18:42:00
  • 基于mysql实现group by取各分组最新一条数据

    2024-01-13 05:48:18
  • DreamWeaver操作超级技巧大全

    2008-10-03 20:50:00
  • 教你如何升级SQL Server数据库系统

    2009-01-19 14:42:00
  • python中单双下划线的区别对比分析

    2023-12-18 23:58:45
  • python 实现rolling和apply函数的向下取值操作

    2022-03-15 07:10:08
  • 使用jupyter notebook运行python和R的步骤

    2023-03-30 18:22:50
  • 使用python实现男神女神颜值打分系统(推荐)

    2021-07-17 23:06:57
  • 在Visual Studio中使用GIT进行源码管理

    2022-09-24 19:28:02
  • Django读取Mysql数据并显示在前端的实例

    2023-11-09 17:36:49
  • python Tkinter是什么

    2022-07-26 08:31:51
  • PHP操作数组的一些函数整理介绍

    2023-11-24 14:24:17
  • python 判断linux进程,并杀死进程的实现方法

    2022-06-24 22:31:54
  • 详解如何使用Python编写vim插件

    2023-01-08 13:30:39
  • 解决TensorFlow模型恢复报错的问题

    2022-06-15 16:24:31
  • python二叉树遍历的实现方法

    2021-09-19 03:53:14
  • 推荐技术人员一款Python开源库(造数据神器)

    2023-11-10 19:57:45
  • Golang将Map的键值对调的实现示例

    2024-04-27 15:32:40
  • asp之家 网络编程 m.aspxhome.com