2016-04-24 14 views
1

Ich versuche, Bar Char mit Liniendiagramm zu plotten. Ich habe 2 Subplots erstellt. den Code unten verwendenpandas plot Balkendiagramm - Unerwartetes Layout

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

Das Bild unten erstellt ist sehr verschieden von dem, was ich erwarte. Ich habe einige andere Methoden ausprobiert, konnte es aber nicht herausfinden. Jede Hilfe wird geschätzt.

enter image description here

Hier ist Beispieldaten

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 
+0

Was genau erwarten Sie? Kannst du ein Bild oder einen Link posten? –

+0

Ich möchte, dass mein Balkendiagramm mit dem Datum auf der X-Achse und dem Wert auf der Y-Achse schön formatiert ist. Aber das Ergebnis ist nicht richtig formatiert. @Reblochon Maske – nnnnmmm

Antwort

0

Ich bin nicht sicher, ob Sie diese als Primär- und Sekundärachse sehen möchten, aber hier ist, wie Sie das tun würde.

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

Produziert: enter image description here

0

Sie sind x-Achse wird der Index des Datenrahmens sein, so müssen Sie die Daten, um sicherzustellen, dass als Datum Objekte dargestellt werden, und dass die Datumsspalte ist die Index:

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

Ergebnis:

Pandas bar plot

Ich hoffe, Sie können die ganze Subplots Sache von diesem Punkt aus verwalten.

Verwandte Themen