使用plot_date和bar函数绘制带有折线图的条形图

时间:2016-04-23 11:39:27

标签: python matplotlib

有两个子图

  1. 有日期的线图(没有问题)
  2. 带有日期(问题)的条形图
  3. 我正在尝试在子图中创建一个沿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
    

1 个答案:

答案 0 :(得分:0)

基本上,我只需要很少的修改就可以复制并粘贴你的代码,并且使用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()

enter image description here