When plotting time-series data, you may notice that the x-axis displays dates such as January 1970 instead of the expected months or years. This issue often occurs when using pandas.DataFrame.plot(kind="bar") together with Matplotlib date formatting.
A simple solution is to replace the pandas bar chart with Matplotlib's bar() function, which gives you complete control over the x-axis labels.
Why Does This Happen?
A pandas bar chart treats the x-axis as categorical positions (0, 1, 2, ...) rather than actual datetime values. If you later apply a Matplotlib date formatter or locator, those numeric positions are interpreted as Unix timestamps, resulting in dates beginning in 1970.
For example, bar position 0 becomes January 1, 1970, position 1 becomes the next day, and so on.
Problem Example
The following code creates a pandas bar chart:
counts.plot(kind="bar")
If you then use a date formatter such as:
import matplotlib.dates as mdates
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))
the x-axis may incorrectly display dates like:
- Jan 1970
- Feb 1970
- Mar 1970
instead of the actual dates in your DataFrame.
Solution: Use Matplotlib's bar()
Instead of relying on DataFrame.plot(), plot the bars directly using Matplotlib.
Example
import matplotlib.pyplot as plt
labels = counts.index.strftime("%b %Y")
fig, ax = plt.subplots(figsize=(18, 5))
ax.bar(labels, counts.values, color="steelblue")
step = 3
ax.set_xticks(range(0, len(labels), step))
ax.set_xticklabels(labels[::step], rotation=45)
ax.set_xlabel("Month")
ax.set_ylabel("Count")
plt.tight_layout()
plt.show()
Explanation of the Code
counts.index.strftime("%b %Y")converts the datetime index into readable month labels.ax.bar()creates a bar chart using the formatted labels as categorical values.counts.valuessupplies the height of each bar.set_xticks()controls which labels are displayed on the x-axis.set_xticklabels()rotates the labels to improve readability.tight_layout()adjusts the figure spacing automatically.
Since the x-axis labels are plain strings, Matplotlib no longer attempts to interpret them as Unix timestamps.
Output
The resulting chart displays:
- The correct month and year labels on the x-axis.
- Evenly spaced bars.
- No unexpected 1970 dates.
- Readable tick labels with proper spacing and rotation.
Why Use Matplotlib Instead of a Pandas Bar Chart?
Using Matplotlib's bar() function offers several benefits:
- Prevents incorrect 1970 date labels.
- Provides complete control over x-axis tick positions and labels.
- Makes it easier to customize colors, annotations, legends, and bar widths.
- Produces consistent results for monthly, quarterly, or yearly aggregated data.
Conclusion
If your bar chart unexpectedly shows dates from 1970, the problem is usually caused by applying date formatting to a pandas bar chart. Since pandas treats the x-axis as categorical positions rather than real dates, Matplotlib interprets those positions as Unix timestamps. Plotting the bars with Matplotlib's bar() function and supplying formatted string labels eliminates the issue and gives you full control over the chart's appearance.