What is the recommended way to create a time series bar plot in pandas, preferably using the nice hierarchical datetime labeling as is usual in pandas? Bar plot in pandas labels every bar individually, which creates a mess. I used to be able to achieve this by mixing pandas and matplotlib like:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randn(60, 2), index=pd.date_range('2000-01-01', periods=60),
columns=list('AB'))
# Line plot using pandas (sets up X datetime axis)
ax = df.A.plot()
# Add bars with matplotlib
ax.bar(df.index, df.B)
but in the current versions (matplotlib: 2.1.2, pandas: 0.22.0) ax.bar() breaks with:
ValueError: Unrecognizable date '2000-01-01T00:00:00.000000000'
EDIT: I believe mixing pandas line plot with matplotlib bar plot was possible in the following (or similar) versions: numpy: 1.13.0, matplotlib: 2.0.2, pandas: 0.20.2. Below is a sample plot.
The plot above was produced by this code (where df has columns p and tempv):
fig, ax = plt.subplots()
# Plot invisible precipitation to set up X axis
df.p.plot(ax=ax, alpha=0, label='_')
# Plot temperature as bar
ax.bar(df.index, df.p, color='#348ABD', edgecolor='#348ABD', label='Precipitation')
# Plot precipitation on the right axis
df.tempv.plot(ax=ax, secondary_y=True, color='DarkOrange', label='Temperature' )
Many thanks for any pointers,
Alex