Pandas情节条形图意外布局

2024-09-27 04:22:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用折线图绘制条形图。我创建了2个子批次。 使用以下代码

         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()

下面创建的图像与我期望的非常不同。我没试过别的方法,但没想到。 感谢任何帮助。在

enter image description here

这是样本数据

^{pr2}$

Tags: adddfplot绘制pltrsi折线图ax1
2条回答

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

我希望你能从这一点开始管理整个子流程。在

我不确定你是否想把它看作是主次轴,但下面是你如何做到的。在

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

相关问题 更多 >

    热门问题