简体   繁体   English

减少 plot x 轴上的标签数量

[英]Reducing the number of labels on x-axis of plot

Im learning matplotlib, and trying to draw a simple scatter plot that has dates on the x-axis and values on the y.我正在学习 matplotlib,并尝试绘制一个简单的散点图 plot,它在 x 轴上有日期,在 y 轴上有值。 There are 2200 dates, from 2004 to 2019, (format 2014-09-17 type O).有 2200 个日期,从 2004 年到 2019 年,(格式 2014-09-17 类型 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. plot 很棒,但是,显然,xaxis 上有 2000 个条目,我检查了 Matplotlib 文档(可能是我的,但它不是很适合新手,不像 Z23EEEB4347BDD26BDD26B7EE9A3 上的其他帖子) x轴上写的标签(日期)的数量:并找到了各种命令。 Axes,set_xticklabels(labels, *, fontdict=None, minor=False, **kwargs)set_xticks(), MaxNLocator(), autolocator.轴,set_xticklabels(labels,*,fontdict=None,minor=False,**kwargs)set_xticks(),MaxNLocator(),自动定位器。 Axes.set_xticks(MaxNLocator(10)) and others, Ive tried all sorts of variations. Axes.set_xticks(MaxNLocator(10)) 等,我尝试了各种变化。 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?有什么简单的方法可以将 x 轴上的条目数减少到,比如最多“n”个日期或每 10 个日期一个日期?

Thanks!谢谢!

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这是一个详细的示例: https://matplotlib.org/2.0.2/examples/api/date_demo.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM