python - Matplotlib tweek 标签算法?
问题描述
Matplotlib 有一些非常复杂的代码来确定如何显示标签,但有时它会限制它的标签而不是在演示中看起来很好。有没有办法tweek它?
例如,假设我们正在根据日期绘制一些东西:
figure = plt.figure(figsize=(8,1))
ax = plt.gca()
ax.set_xlim(xmin=np.datetime64('2010'), xmax=np.datetime64('2020-04-01'))
我们得到一个像这样的x轴:
但假设我们希望它显示更多间隔年份,如下所示:
我们可以在任何给定的情况下通过“机械地”编辑标签来组合它。例如:
ax.set_xticks([tick for i, tick in enumerate(ax.get_xticks()) if i%2==0]) # Every other year.
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%Y"))
但这很脆弱,只要 x 限制发生变化,它就会中断。
有没有办法在刻度设置算法中强制增加间距?
解决方案
哦!找到了 matplotlib 源代码,它把我带到了AutoDateLocator:
ax.xaxis.set_major_locator(matplotlib.dates.AutoDateLocator(maxticks=8))
非日期的对应定位器是MaxNLocator。
推荐阅读
- python - 有没有更好的方法以更短的行抓取 MSFT 数据?我已经尝试过了,但得到的问题是“模块”对象不可调用
- python-3.x - 单击按钮时的Python PyQt5终端命令执行问题
- postgresql - postgreSQL & PL/pgSQL:varchar(36) 的值太长,但我昨天将字段扩大到 60
- android - RecyclerView 中的 NullPointerException
- powershell - powershell管道如何在本机程序之间传输Unicode?
- excel - 使用 VBA 运行存储过程
- php - 删除括号前后的html
- matlab - 如何使用 Raspberry 在 Matlab 中读取 MMA8542q 加速度计传感器
- react-native - 反应本机黑屏没有错误?
- objective-c - 使用 OCMock 模拟内部对象并更改实例选择器的行为