question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using pandas.TimeSeries_DateFormatter in bar plot?

See original GitHub issue

xref #5774

When time series is set as index, line plot uses the TimeSeries_DateFormatter for recognizable date formatting. However, bar plot use the FixedFormatter which outputs the datetime as it is. Can bar plot also use the DateFormatter?

In [1]: from pandas import *
In [2]: from numpy.random import *
In [3]: ts = Series(randn(100), index=date_range('1/1/2000', periods=100))
In [4]: df = DataFrame(randn(100, 4), index=ts.index, columns=['A', 'B', 'C', 'D'])
In [5]: df = df.cumsum()

In [7]: a = df.plot(kind='bar')
In [9]: a.get_xaxis().get_major_formatter()
Out[9]: <matplotlib.ticker.FixedFormatter instance at 0x108700f38>

In [11]: a = df.plot()
In [12]: a.get_xaxis().get_major_formatter()
Out[12]: <pandas.tseries.converter.TimeSeries_DateFormatter instance at 0x10877ccf8>

Issue Analytics

  • State:open
  • Created 11 years ago
  • Reactions:6
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

17reactions
pwallercommented, Apr 25, 2017

I found a workaround, by applying this at the end:

In this case, d is the DataFrame I’m plotting, and d.index is the DatetimeIndex.

plt.gca().xaxis.set_major_formatter(plt.FixedFormatter(d.index.to_series().dt.strftime("%b %Y")))
8reactions
pwallercommented, Jul 31, 2017

I’m not a huge fan of the workaround and hit this quite frequently still. It also doesn’t produce x-axis labels which are as good a data.plot() does, because the latter does Year labels on a separate row than Month labels, and I don’t know how to achieve the same effect. I’d love for bar chars to be able to display time series data in the same way that line plots do, sometimes it is a less misleading way to present the data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas bar plot changes date format - Stack Overflow
The pandas.tseries.converter.TimeSeries_DateFormatter that Pandas uses to format the dates in the "good" plot works well with line plots ...
Read more >
Pandas & Matplotlib: personalize the date format in a bar chart
He wanted to change the format of the dates on the x-axis in a simple bar chart with data read from a csv...
Read more >
Customize Dates on Time Series Plots in Python Using ...
Using the DateFormatter module from matplotlib, you can specify the format that you want to use for the date using the syntax: "%X...
Read more >
Plotting with Pandas - Dates and Bar Plots | by Dalya Gartzman
Using Pandas Python package to make nice plots with dates and other shenaniganz.
Read more >
Time series plot with Matplotlib - Python Graph Gallery
subplots(figsize=(8, 6)) half_year_locator = mdates.MonthLocator(interval=6) year_month_formatter = mdates.DateFormatter("% ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found