[英]Displayed ticks on x-axis in Matplotlib using the bar plot
我正在嘗試使用 pandas 和 matplotlib 的 plot 直方圖數據。 我正在使用dataframe.plot()
function。 問題是,這會生成一個在 x 軸上顯示大量刻度的圖表。 直方圖 plot
dataframe 的索引用作刻度。 這是代碼:
fig, axes = plt.subplots(figsize=(12,12))
PLOT_CONFIG_HISTOGRAM={
"subplots":True,
"title":"cyclictest wakeup latency",
"legend":True,
"xlabel":"latency/us",
"ylabel":"cycles",
"logy":True,
"kind":"bar",
}
my_dataframe.plot(ax=axes,**PLOT_CONFIG_HISTOGRAM)
matplotlib 或 pandas 有沒有辦法減少顯示的刻度? 還是顯示直方圖數據的更好方法?
最后應該是這樣的:
| | | | |
| |
1 5
或像這樣:
| |
1 5
提前感謝您的任何意見。
您可以為 plot 使用固定數量的箱,例如20
:
PLOT_CONFIG_HISTOGRAM={
[...]
"bins": 20,
[...]
}
或者,如果您想保持直方圖分辨率但想降低 x 軸標簽的分辨率,例如僅每50
次刻度顯示:
div = 50
locs, labels = plt.xticks()
plt.xticks(locs[::div], labels[::div])
您可以通過在 locator_params 中指定 bin 的數量來設置所需的刻度數:
plt.locator_params(axis='x', nbins=10)
要進一步清理 plot,您還可以旋轉 xticks 並設置 xlimits,如下所示:
plt.xticks(rotation = '90')
plt.xlim(a,b) #where a and b represent the minimum and maximum of your x values
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.