【问题标题】:plot bar chart with line chart using plot_date and bar function使用 plot_date 和 bar 函数绘制带有折线图的条形图
【发布时间】:2016-08-17 01:24:56
【问题描述】:

有两个子情节

  1. 带日期的折线图(没有问题)
  2. 带日期的条形图(问题)

我正在尝试在子图中沿 x 轴创建一个带有日期的条形图。 下面是代码

     days = pd.to_datetime(df['indx'].astype(str), format='%Y%m%d')
     RSI_14 = df['RSI_14']
     ATR_14 = df['ATR_14']
     fig5 = plt.figure(figsize=(14,9), dpi=200)
     ax1 = fig5.add_subplot(211)
     ax2 = fig5.add_subplot(212)
     ax1.plot_date(x=days, y=RSI_14,fmt="r-",label="ROC_7")
     ax2.bar(days,ATR_14,width=1)
     ax2.xaxis_date()
     pp.savefig()

这会引发错误。

TypeError: float() argument must be a string or a number

如果我们将条形图与折线图一起绘制会不会有问题

indx    ATR_14  RSI_14
20141015    0.01737336  99.48281325
20141016    0.017723579 99.48281325
20141017    0.020027102 99.53091876
20141020    0.024023488 99.67180924
20141021    0.02415369  99.72027954
20141022    0.026266531 99.76100661
20141023    0.026764327 85.41188977

【问题讨论】:

  • 你在哪一行得到了 TypeError?我会检查 ATR_14 看看它是否是你认为应该的。
  • @Hun File "graph_of_every_stock.py",第 90 行,在 我不明白你提到的 ATR_14 问题

标签: python matplotlib


【解决方案1】:

基本上,我复制并粘贴了您的代码,几乎没有修改,它使用 python 3.4 对我有用。 xlabels 需要一些改进。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('data', delimiter='\s+')
days = pd.to_datetime(df['indx'].astype(str), format='%Y%m%d')
RSI_14 = df['RSI_14']
ATR_14 = df['ATR_14']

fig5 = plt.figure()
ax1 = fig5.add_subplot(211)
ax2 = fig5.add_subplot(212)
ax1.plot_date(x=days, y=RSI_14,fmt="ro-",label="ROC_7")
ax2.bar(days,ATR_14,width=1)
ax2.xaxis_date()
plt.show()

【讨论】:

    猜你喜欢
    • 2019-05-24
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    相关资源
    最近更新 更多