'drawing bars for time series with years as bins in matplotlib
so i have data as readings recorded as monthly averages over time. I have already drawn it but the problem is the graph is kinda messed up and the X axis is unreadable, i have been trying for two days to draw it without a success, any help would be appreciated. i was able to get the image i want from plotly using the exact same code just instead drawing using "iplot" instead of "plot", you can see the result in the second image. but the problem is that i have no way of exporting it as SVG, besides i really like matplotlib so i want to use it. the code is at the end, i should also note that all the commented out lines are failed attempts.
lastly here is the code:
fig, ax= plt.subplots(1,1)
# ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
ax=hist.count().plot(kind='bar', color='brown', legend=True)
# ax.xaxis.set_major_locator(matplotlib.dates.YearLocator(10))
# ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("\n%Y"))
# plt.setp(ax.get_xticklabels(), rotation=0, ha="center")
# ax.xaxis.set_major_formatter(DateFormatter("%Y"))
# fig.autofmt_xdate()
# plt.xticks(hist['Date'], rotation=90)
plt.show()
This is the previous state of the data (grouped by WellCode): enter image description here
this is after a group them annually to basically make a histogram for the occurrence distribution:
{Timestamp('1888-12-31 00:00:00', freq='A-DEC'): 1,
Timestamp('1889-12-31 00:00:00', freq='A-DEC'): 1,
Timestamp('1890-12-31 00:00:00', freq='A-DEC'): 1,
Timestamp('1891-12-31 00:00:00', freq='A-DEC'): 1,
Timestamp('1892-12-31 00:00:00', freq='A-DEC'): 1,
....................
Timestamp('2000-12-31 00:00:00', freq='A-DEC'): 411670,
Timestamp('2001-12-31 00:00:00', freq='A-DEC'): 423100,
Timestamp('2002-12-31 00:00:00', freq='A-DEC'): 434910,
Timestamp('2003-12-31 00:00:00', freq='A-DEC'): 447776,
Timestamp('2004-12-31 00:00:00', freq='A-DEC'): 460983,
Timestamp('2005-12-31 00:00:00', freq='A-DEC'): 475415,
Timestamp('2006-12-31 00:00:00', freq='A-DEC'): 490284,
Timestamp('2007-12-31 00:00:00', freq='A-DEC'): 505549}
Solution 1:[1]
You have this small data and you want to get the histogram to this data as follows:
import matplotlib.pyplot as plt
lst = ['2010-01-01','2011-01-01','2011-01-01','2012-01-01','2012-01-01','2012-01-01',
'2013-01-01','2013-01-01','2013-01-01','2013-01-01','2014-01-01','2014-01-01',
'2014-01-01','2015-01-01','2015-01-01','2015-01-01','2016-01-01','2016-01-01',
'2017-01-01','2018-01-01','2019-01-01']
df = pd.DataFrame(lst, columns=['Date'])
plt.hist(df["Date"]) # daily data
plt.show()
To solve this problem, you should convert your date column from str to datetime and see how the plot looks like:
df = pd.DataFrame(lst, columns=['Date'])
df["Date"] =pd.to_datetime(df["Date"]) # add this line before plotting
plt.hist(df["Date"])
plt.show()
If you want to save your histogram plot in .svg format, you should add this line directly after the plot in this way:
plt.hist(df["Date"])
plt.savefig("test.svg") # add your line directly after plot.hist()
plt.show()
Final remark: now you can use the additional settings smoothly to set up the x-axis as you prefer, this is an example:
ax.xaxis.set_major_locator(mdates.YearLocator(2)) # show every 2 years on the x-axis
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b'))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |





