熊猫情节条形图 - 意外布局

时间:2016-04-24 03:02:23

标签: python pandas matplotlib

我正在尝试使用折线图绘制条形码。我创建了2个子图。 使用以下代码

var data = d3.json("linedata.json");

下面创建的图像与我期望的非常不同。我尝试过其他一些方法,但无法弄清楚。 任何帮助表示赞赏。

enter image description here

以下是样本数据

         RSI_14 = df['RSI_14']
         df['ATR_14'] = df['ATR_14'].astype(float)
         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 = df[['indx','ATR_14']].plot(kind='bar', title ="V comp",figsize=(7,4),legend=True, fontsize=12)
         ticklabels = ['']*len(df.indx)
         ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
         plt.gcf().autofmt_xdate()
         pp.savefig()

2 个答案:

答案 0 :(得分:0)

我不确定您是否希望将此视为主要和次要轴,但这是您如何做到这一点。

import matplotlib.pyplot as plt
import pandas as pd
from pandas import Timestamp

df = pd.DataFrame(
  {'ATR_14': {Timestamp('2014-10-15 00:00:00'): 0.01737336,
            Timestamp('2014-10-16 00:00:00'): 0.017723579,
            Timestamp('2014-10-17 00:00:00'): 0.020027101999999998,
            Timestamp('2014-10-20 00:00:00'): 0.024023488,
            Timestamp('2014-10-21 00:00:00'): 0.02415369,
            Timestamp('2014-10-22 00:00:00'): 0.026266531,
            Timestamp('2014-10-23 00:00:00'): 0.026764327},
 'RSI_14': {Timestamp('2014-10-15 00:00:00'): 99.48281325,
            Timestamp('2014-10-16 00:00:00'): 99.48281325,
            Timestamp('2014-10-17 00:00:00'): 99.53091876,
            Timestamp('2014-10-20 00:00:00'): 99.67180924,
            Timestamp('2014-10-21 00:00:00'): 99.72027954,
            Timestamp('2014-10-22 00:00:00'): 99.76100661,
            Timestamp('2014-10-23 00:00:00'): 85.41188977}},
  columns=['ATR_14', 'RSI_14'])

fig, ax1 = plt.subplots()
ax1.bar(df.index, df['ATR_14'], width=0.65, align='center', color='#F27727',
        edgecolor='#F27727')
ax1.set_xlabel('Date')
ax1.set_ylabel('ATR_14')

ax2 = ax1.twinx()
ax2.plot(df.index, df['RSI_14'], color='#058DC7', linewidth=4, marker='o',
         markersize=10, markeredgecolor='w', markeredgewidth=3)
ax2.set_ylabel('RSI_14', rotation=270)


fig.autofmt_xdate()
#plt.tight_layout()

plt.show()

产地: enter image description here

答案 1 :(得分:0)

你的x轴将是你的数据帧的索引,所以你需要确保日期表示为日期对象,并且日期列是索引:

import datetime
import pandas as pd

# Init data
df = pd.DataFrame()
df['indx']= [20141015, 20141016, 20141017, 20141020, 20141021, 20141022, 20141023]
df['ATR_14']= [0.01737336, 0.017723579, 0.020027102, 0.024023488, 0.02415369, 0.026266531, 0.026764327]
df['RSI_14']= [99.48281325, 99.48281325, 99.53091876, 99.67180924, 99.72027954, 99.76100661, 85.41188977]

# change type of 'indx' column to date
df['indx'] = df['indx'].apply(lambda x: datetime.datetime.strptime(str(x), "%Y%m%d").date())

# Set 'indx' column as actual index; select a column and display it as bars
df.set_index('indx')['ATR_14'].plot.bar(title ="V comp",figsize=(7,4),legend=True, fontsize=12)
plt.show()

结果:

Pandas bar plot

我希望你能从这一点管理整个子图。