[英]Python and plot() : how to limit the number of x-axis ticks (labels)
[英]Reducing the number of labels on x-axis of plot
我正在學習 matplotlib,並嘗試繪制一個簡單的散點圖 plot,它在 x 軸上有日期,在 y 軸上有值。 有 2200 個日期,從 2004 年到 2019 年,(格式 2014-09-17 類型 O)。
這是我的代碼:
x=df.DATE
y=df.CLOSE
plt.figure(figsize=(21,12))
plt.xticks(fontsize=8, rotation = 45)
plt.scatter(x,y)
plot 很棒,但是,顯然,xaxis 上有 2000 個條目,我檢查了 Matplotlib 文檔(可能是我的,但它不是很適合新手,不像 Z23EEEB4347BDD26BDD26B7EE9A3 上的其他帖子) x軸上寫的標簽(日期)的數量:並找到了各種命令。 軸,set_xticklabels(labels,*,fontdict=None,minor=False,**kwargs)set_xticks(),MaxNLocator(),自動定位器。 Axes.set_xticks(MaxNLocator(10)) 等,我嘗試了各種變化。 但它們都不起作用。 許多人需要“斧頭”。 我沒有使用過,當我嘗試軸時,它告訴我它沒有定義。
我難住了。 有什么簡單的方法可以將 x 軸上的條目數減少到,比如最多“n”個日期或每 10 個日期一個日期?
謝謝!
你可以使用類似的東西:
import matplotlib.dates as mdates
years = mdates.YearLocator()
ax.xaxis.set_major_locator(years) #Every year
這是一個詳細的示例: https://matplotlib.org/2.0.2/examples/api/date_demo.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.