0

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.

enter image description here

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

user2690051
  • 155
  • 1
  • 1
  • 9
  • In which version of the libraries did the code you show actually work? I strongly doubt that it ever has. – ImportanceOfBeingErnest Feb 14 '18 at 00:30
  • [Here](https://stackoverflow.com/questions/42813890/python-making-combined-bar-and-line-plot-with-secondary-y-axis) is an answer that claims this to be impossible. Also earlier questions, like [this](https://stackoverflow.com/questions/30133280/pandas-bar-plot-changes-date-format) and [this](https://stackoverflow.com/questions/22623324/plot-bar-graph-and-timeseries-plot-on-different-axis-using-pandas) would confirm this assumption. The solution would be to plot everything with matplotlib. – ImportanceOfBeingErnest Feb 14 '18 at 00:37
  • I believe the versions were: numpy: 1.13.0, matplotlib: 2.0.2, pandas: 0.20.2, or ssomething very similar. I added a sample plot above. Note how the X axis is labeled with hierarchical datetime labels by pandas. – user2690051 Feb 14 '18 at 16:26
  • Just to make sure I understand correctly: You are saying the graph you show has been produced with a pandas `.plot` and a matplotlib `.bar` on the same axes? – ImportanceOfBeingErnest Feb 14 '18 at 16:56
  • Yes, I added clarification (code) to the post – user2690051 Feb 14 '18 at 17:40
  • As said I am under the impression that this should never have worked. If you are right and it did indeed, I would be interested in the reason it now fails as well. If possible provide a [mcve] that would allow people to test the case without room for interpretation. – ImportanceOfBeingErnest Feb 14 '18 at 18:31
  • @ImportanceOfBeingErnest My first code snippet qualifies as that, right? I've edited it to add the necessary import statements and a few comments. Thank you. – user2690051 Feb 15 '18 at 18:11
  • In a way yes, but the daterange does not match the plot and p and tempv are not in the data. Such things leave room for interpretation. – ImportanceOfBeingErnest Feb 15 '18 at 18:33
  • @ImportanceOfBeingErnest I added the plot as evidence/illustration that it could be done before. Perhaps I should delete the accompanying code and leave just the first snippet as the minimal/complete/verifiable example. Would that make it more clear? – user2690051 Feb 15 '18 at 23:29
  • I don't think so. If you ask how that plot would be produced, my answer would clearly be: by not mixing pandas and matplotlib. But here your question is different: You say "the following code did produce in version x.y. In version x.y+1 it does not work anymore." This then requires a complete & verifiable example, such that someone can use those versions and reproduce the issue. – ImportanceOfBeingErnest Feb 15 '18 at 23:41
  • The first snippet appears to work as intended on much newer versions: matplotlib=3.3.2 and pandas=1.1.5. – zgana Jun 12 '21 at 17:46

0 Answers0