【问题标题】:How to plot both Price and Volume in same Chart如何在同一张图表中同时绘制价格和交易量
【发布时间】:2019-02-01 13:38:23
【问题描述】:

我有一个如下所述的数据框:

Date,Time,Price,Volume
31/01/2019,09:15:00,10691.50,600
31/01/2019,09:15:01,10709.90,13950
31/01/2019,09:15:02,10701.95,9600
31/01/2019,09:15:03,10704.10,3450
31/01/2019,09:15:04,10700.05,2625
31/01/2019,09:15:05,10700.05,2400
31/01/2019,09:15:06,10698.10,3000
31/01/2019,09:15:07,10699.90,5925
31/01/2019,09:15:08,10699.25,5775
31/01/2019,09:15:09,10700.45,5925
31/01/2019,09:15:10,10700.00,4650
31/01/2019,09:15:11,10699.40,8025
31/01/2019,09:15:12,10698.95,5025
31/01/2019,09:15:13,10698.45,1950
31/01/2019,09:15:14,10696.15,3900
31/01/2019,09:15:15,10697.15,2475
31/01/2019,09:15:16,10697.05,4275
31/01/2019,09:15:17,10696.25,3225
31/01/2019,09:15:18,10696.25,3300

数据框包含大约 8000 行。我想在同一张图表中同时绘制价格和交易量。 (音量范围:0 - 8,00,000)

【问题讨论】:

  • 到目前为止,您尝试了哪些无效的方法?

标签: python python-3.x matplotlib


【解决方案1】:

假设您想比较价格和数量与时间,试试这个:

df = pd.read_csv('your_path_here')
df.plot('Time', ['Price', 'Volume'], secondary_y='Price')

编辑:x轴自定义

既然你想要自定义 x 轴,试试这个(这只是一个你可以遵循的基本示例):

# Create a Datetime column while parsing the csv file
df = pd.read_csv('your_path_here', parse_dates= {'Datetime': ['Date', 'Time']})

然后您需要创建两个列表,一个包含 x 轴上的位置,另一个包含标签。 假设您希望每 5 秒添加一次标签(您可以在 30 分钟发出请求,但不能使用您提供的数据)

positions = [p for p in df.Datetime if p.second in range(0, 60, 5)]
labels = [l.strftime('%H:%M:%S') for l in positions]

然后您将位置和标签列表传递给set_xticksset_xticklabels

ax = df.plot('Datetime', ['Price', 'Volume'], secondary_y='Price')
ax.set_xticks(positions)
ax.set_xticklabels(labels)

【讨论】:

  • 先生,是否可以在 X 轴上每隔 30 分钟添加一次时间
  • 我编辑了我的答案以满足您的要求。由于您的日期列被限制在 20 秒范围内,因此无法设置 30 分钟间隔
猜你喜欢
  • 1970-01-01
  • 2023-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多