Python 和 Matplotlib : reduce number of x tick marks and remove zero-padding

标签 python matplotlib

我是 matplotlib 和 pyplot 的新手,正在尝试绘制大型数据集。以下是一个小片段。

情节有效,但 xtick 标记很拥挤。

如何减少刻度线的数量?

使用 plt.locator_params(nbins=4) 返回错误:

AttributeError: 'FixedLocator' object has no attribute 'set_params'

有没有办法从 pyplot 中的日期标签中删除 0 填充?

import matplotlib.pyplot as plt


x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
xticks = ['01/01', '01/02', '01/03', '01/04', '01/05', '01/06', '01/07', '01/08', '01/09', '01/10', '01/11', '01/12', '01/13', '01/14', '01/15', '01/16', '01/17', '01/18', '01/19', '01/20', '01/21', '01/22', '01/23', '01/24', '01/25', '01/26', '01/27', '01/28', '01/29', '01/30']
y = [80, 80, 60, 30, 90, 50, 200, 300, 200, 150, 10, 80, 20, 30, 40, 150, 160, 170, 180, 190, 20, 210, 220, 20, 20, 20, 200, 270, 280, 90, 00]
y2 = [100, 100, 200, 300, 40, 50, 60, 70, 80, 90, 100, 110, 12, 13, 10, 110, 16, 170, 80, 90, 20, 89, 28, 20, 20, 28, 60, 70, 80, 90, 30]


plt.plot(x, y)
plt.plot(x, y2)
plt.xticks(x, xticks, rotation=90)
plt.show()

enter image description here

最佳答案

由于 matplotlib 有一些非常好的日期工具,我认为将日期字符串转换为 datetime.datetime 对象是有意义的。

然后您可以使用一个方便的日期定位器;在这种情况下,DayLocator 最有意义。要让它跳过一些标签,您可以使用 interval kwarg。

然后要从您的 xticklabels 中删除前导零,您需要一个自定义格式化函数。

import datetime as dt

import matplotlib.pyplot as plt
import matplotlib.dates as mdates 
import matplotlib.ticker as tkr

def xfmt(x,pos=None):
    ''' custom date formatting '''
    x = mdates.num2date(x)
    label = x.strftime('%m/%d')
    label = label.lstrip('0')
    return label

x = ['01/01', '01/02', '01/03', '01/04', '01/05', '01/06', '01/07', '01/08', '01/09', '01/10', '01/11', '01/12', '01/13', '01/14', '01/15', '01/16', '01/17', '01/18', '01/19', '01/20', '01/21', '01/22', '01/23', '01/24', '01/25', '01/26', '01/27', '01/28', '01/29', '01/30', '01/31']
xdates = [dt.datetime.strptime(i,'%m/%d') for i in x]
y = [80, 80, 60, 30, 90, 50, 200, 300, 200, 150, 10, 80, 20, 30, 40, 150, 160, 170, 180, 190, 20, 210, 220, 20, 20, 20, 200, 270, 280, 90, 00]
y2 = [100, 100, 200, 300, 40, 50, 60, 70, 80, 90, 100, 110, 12, 13, 10, 110, 16, 170, 80, 90, 20, 89, 28, 20, 20, 28, 60, 70, 80, 90, 30]

plt.plot(xdates,y)
plt.plot(xdates,y2)
plt.setp(plt.gca().xaxis.get_majorticklabels(),rotation=90)
plt.gca().xaxis.set_major_formatter(tkr.FuncFormatter(xfmt))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=4))
plt.gca().xaxis.set_minor_locator(mdates.DayLocator())
plt.show()

上面的代码产生如下图:

_sompl.png

关于Python 和 Matplotlib : reduce number of x tick marks and remove zero-padding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26719149/

相关文章:

python - python audiolab中的蜂鸣声

python - matplotlib 不支持生成器作为输入

javascript - mpld3 生成的 html 图例中缺少 xticklabels 和颜色

python - plot中打印字符串,字符间出现空格

Python Seaborn jointplot 不在图表上显示相关系数和p值

Python 分组条形图。计数不起作用

python - 在代码更改时自动重新加载 Dask 工作器容器

python - 如何自动执行.py文件

python - mustache 、通天塔和 gettext

python - sh.cd 使用上下文管理器