繁体   English   中英

使用条形 plot 在 Matplotlib 中的 x 轴上显示刻度

[英]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.

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