I have a DataFrame with the row index as a DatetimeIndex.
This index is coming up differently on the x-axis while I am making line and bar plots. My code is as follows:
start_date = datetime.datetime.strptime('2017-02-20', '%Y-%m-%d').date()
end_date = datetime.datetime.strptime('2017-02-23', '%Y-%m-%d').date()
daterange = pd.date_range(start_date, end_date)
df = pd.DataFrame(index = daterange, data = {'Male':[12, 23, 13, 11], 'Female': [10, 25, 15, 9]})
df.plot(kind='line')
df.plot(kind='bar', stacked = False, grid=1)
The plots i am obtaining are as follows. Line plot with nice formatting of dates on x-axis:
Bar plot without formatting of dates on x-axis:
In the line plot the x-axis labels are well formatted with the month and year on left corner and the dates used as x-ticks. But in the bar plot, the entire date along with the time (00:00:00) is shown unlike in the line plot.
How can I get the proper formatting of dates on x-axis in the bar plot and without the time being shown?
The problem is in the source code of pandas
. You cannot get the bar plot to use the pd.DateTimeFormatter
without deriving custom subclasses or using matplotlib
directly.
In line 1766 (1784 in the dev version) of pandas.tools.plotting
the datetime formatting for LinePlot
is done. This is not present in BarPlot
, for reasons that I can only hypothesize:
Line charts are intended to print timeseries data, whereas the same does not necessarily make sense for bar charts.
I would still like to see bar plots being able to format dates properly without using matplotlib, so you might want to open an issue with the pandas
project.
With matplotlib
directly:
import pandas as pd
import datetime
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
start_date = datetime.datetime.strptime('2017-02-20', '%Y-%m-%d').date()
end_date = datetime.datetime.strptime('2017-02-23', '%Y-%m-%d').date()
daterange = pd.date_range(start_date, end_date)
df = pd.DataFrame(index = daterange, data = {'Male':[12, 23, 13, 11], 'Female': [10, 25, 15, 9]})
ax=df.plot.bar(xticks=df.index.month, stacked = False, grid=1)
ticklabels = [item.strftime('%b %d') for item in df.index]
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.gcf().autofmt_xdate()
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With