1

I try to plot a Pandas Dataframe with the matplotlib.pyplot package mpl_finance for a proper Candlestick chart. The plotting works but the date range of the x-axis is wrong. The header of the Dataframe is the following:

Dataframe rows

The mpl_finance method candlestick_ohlc needs the time in the float date format so I convert the dates with the folowing method df_ohlc['Datum'] = df['Datum'].map(mdates.date2num).

After the conversion i plot the chart

fig = plt.figure()
ax1 = fig.add_subplot(111)
candlestick_ohlc(ax1, df_ohlc.values, colorup='g', width=0.6)
ax1.xaxis_date()
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%b'))
fig.autofmt_xdate()
plt.show()

But the plot differs from the Dataframe. In the Dataframe the period 2017-12-23 - 2017-12-26 is missing but the plot creates this date range. Plotting of the Candlestick chart

Comment the date formatting lines doesn't help. When I plot the float values, I get the same plot.

Edit: I've added my sample code for the Dataframe creation.

df = pd.read_csv('wkn_766403_historic.csv', sep=';', thousands='.', decimal=',',
                     usecols=['Datum', 'Erster', 'Hoch', 'Tief', 'Schlusskurs'], parse_dates=True, index_col='Datum')
df.index = df.index.tz_localize('Europe/Berlin')
df = df.sort_index()
df = df.loc['2017-12-20':'2017-12-29']
df = df.reset_index()
df_ohlc = df.copy()
df_ohlc['Datum'] = df['Datum'].map(mdates.date2num)
Neal Mc Beal
  • 245
  • 3
  • 16
  • 1
    Can you add a sample dataframe of this data, not a screenshot? It would help with possibly finding a solution. – Sander van den Oord Nov 28 '18 at 10:59
  • Hey Sander. I've postet my code for the Dataframe creation. I hope this helps. The code produces the same Dataframe shown in the screenshot. – Neal Mc Beal Nov 28 '18 at 11:05
  • Well... in the screenshoft of your data I see the dates ranging from 2017-12-20 to 2017-12-29, .... that is exactly what is plotted there. The X-axis is continuous and matplotlib does not automatically leave out these dates. – KahntM Nov 28 '18 at 11:05
  • Hello KahntM. Do you know how i can exclude the blank dates in the plot? – Neal Mc Beal Nov 28 '18 at 11:07
  • I can't help you, but did you check some other similar questions/answers on stackoverflow, for example: https://stackoverflow.com/questions/9673988/intraday-candlestick-charts-using-matplotlib – Sander van den Oord Nov 28 '18 at 11:33
  • Hello Sander! Ty for the helpful question. Maybe you can provide your tipp as solution and i accept it as the answer? It was really helpful. – Neal Mc Beal Nov 29 '18 at 09:20

0 Answers0