Reputation: 147
Im learning matplotlib, and trying to draw a simple scatter plot that has dates on the x-axis and values on the y. There are 2200 dates, from 2004 to 2019, (format 2014-09-17 type O).
Heres my code:
x=df.DATE
y=df.CLOSE
plt.figure(figsize=(21,12))
plt.xticks(fontsize=8, rotation = 45)
plt.scatter(x,y)
The plot is great, but, obviously, the xaxis has 2000 entries on it! Ive checked the Matplotlib documentation (maybe its me but its not very noob-friendly, unlike the python documentation) and other posts on stackoverflow, as to how to reduce the number of labels (dates) written on the x-axis, and have found various commands: Axes.set_xticklabels(labels, *, fontdict=None, minor=False, **kwargs)set_xticks(), MaxNLocator(), autolocator, Axes.set_xticks(MaxNLocator(10)) and others. Ive tried all sorts of variations, but none of them work. And many require "ax." which I havent used and when I try axes it tells me its not defined.
Im stumped. Any simple way I reduce the number of entries on the x-axis to, say a maximum of "n" dates or one date every 10?
Thanks!
Upvotes: 2
Views: 3558
Reputation: 147
In case anyone else has the same problem. I found a 2 simple ways...
FIRST
plt.locator_params(axis="x", nbins=<number>)
Im not sure why or how it works, but my plot originally had 2204 dates in this format: 2014-09-17, which all printed out at 45 deg as a thick, black, unreadable line.
I used "number" = 3 and re-ran the code and it replotted with just the years on the xaxis. Format 2015. 7 numbers, 2015-2021. Did it again with "1" and "2" to see what happened and still got the 7 years, no change whatsoever.
Heres the original code and graph:
plt.figure(figsize=(21,12))
plt.xticks(fontsize=8, rotation = 45)
plt.scatter(df_btc_price.DATE, df_btc_price.VOLUME)
Heres the amended code with new graph.
plt.figure(figsize=(21,12))
plt.xticks(fontsize=8, rotation = 45)
plt.scatter(df_btc_price.DATE, df_btc_price.VOLUME)
plt.locator_params(axis="x", nbins=2)
I have absolutely no idea why this works, and why it prints just the years rather than the whole date.
If anyone can enlighten me, Id be very happy.
SECOND
Remembering the date is this format: 2020-09-24, add this code:
df_btc_price['DATE'] = pd.to_datetime(df_btc_price['DATE'])
So it now looks like this:
plt.figure(figsize=(21,12))
plt.xticks(fontsize=8, rotation = 45)
plt.scatter(df_btc_price.DATE, df_btc_price.VOLUME)
df_btc_price['DATE'] = pd.to_datetime(df_btc_price['DATE'])
And the graph looks like:
I don't know (yet) why it just prints the years, or how to change that, so, for example, it prints the months. But at least its solved the messy x-axis labels.
Upvotes: 1
Reputation: 48
You could use something like :
import matplotlib.dates as mdates
years = mdates.YearLocator()
ax.xaxis.set_major_locator(years) #Every year
Here is a detailed example : https://matplotlib.org/2.0.2/examples/api/date_demo.html
Upvotes: 1