Matplotlib:设置 x 限制也会强制刻度标签? [英] Matplotlib: setting x-limits also forces tick labels?
问题描述
我刚刚升级到 matplotlib 2.0,我觉得我吃疯了.我正在尝试绘制一个对数线性图,y 轴在线性刻度上,x 轴在 log10 刻度上.以前,下面的代码可以让我准确地指定我想要我的刻度的位置,以及我想要它们的标签是什么:
I just upgraded to matplotlib 2.0, and I feel like I'm on crazy pills. I'm trying to make a log-linear plot, with the y-axis on a linear scale and the x-axis on a log10 scale. Previously, the following code would have allowed me to specify exactly where I want my ticks, and what I want their labels to be:
import matplotlib.pyplot as plt
plt.plot([0.0,5.0], [1.0, 1.0], '--', color='k', zorder=1, lw=2)
plt.xlim(0.4,2.0)
plt.ylim(0.0,2.0)
plt.xscale('log')
plt.tick_params(axis='x',which='minor',bottom='off',top='off')
xticks = [0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
ticklabels = ['0.4', '0.6', '0.8', '1.0', '1.2', '1.4', '1.6', '1.8', '2.0']
plt.xticks(xticks, ticklabels)
plt.show()
但是在 matplotlib 2.0 中,这现在导致我得到一组重叠的刻度标签,其中 matplotlib 显然想要自动创建刻度:
But in matplotlib 2.0, this now causes me to get a set of overlapping tick labels where matplotlib apparently wants to auto-create ticks:
但是,如果我注释掉plt.xlim(0.4,2.0)"行并让它自动确定轴限制,则没有重叠的刻度标签,我只得到我想要的:
But if I comment out the "plt.xlim(0.4,2.0)" line and let it automatically determine the axis limits, there are no overlapping tick labels and I just get the ones I want:
但这不起作用,因为我现在有无用的 x 轴限制.
But that doesn't work because I now have useless x-axis limits.
有什么想法吗?
对于将来在互联网上搜索的人,我越来越相信这实际上是 matplotlib 本身的一个错误.我又回到了 1.5.3 版.只是为了避免这个问题.
for people searching the internet in the future, I'm becoming more convinced that this is actually a bug in matplotlib itself. I reverted back to v. 1.5.3. to just avoid the issue.
推荐答案
重叠的附加刻度标签源自一些小刻度标签,它们出现在绘图中.要摆脱它们,可以将次要格式化程序设置为 NullFormatter
:
The additional ticklabels that overlap originate from some minor ticklabels, which are present in the plot. To get rid of them, one can set the minor formatter to the NullFormatter
:
plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
问题中的完整代码可能看起来像
The complete code from the question might then look like
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np
x = np.linspace(0,2.5)
y = np.sin(x*6)
plt.plot(x,y, '--', color='k', zorder=1, lw=2)
plt.xlim(0.4,2.0)
plt.xscale('log')
xticks = [0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
ticklabels = ['0.4', '0.6', '0.8', '1.0', '1.2', '1.4', '1.6', '1.8', '2.0']
plt.xticks(xticks, ticklabels)
plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
plt.show()
以下代码可能更直观,因为它没有将 xticklabels 设置为字符串,其中我们使用了 FixedLocator
和 ScalarFormatter
.
此代码生成与上述相同的图.
A code that may be more intuitive as it is not setting the xticklabels as strings would be the following, where we use a FixedLocator
and a ScalarFormatter
.
This code produces the identical plot as the above.
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np
x = np.linspace(0,2.5)
y = np.sin(x*6)
plt.plot(x,y, '--', color='k', zorder=1, lw=2)
plt.xlim(0.4,2.0)
plt.xscale('log')
xticks = [0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
xmajorLocator = matplotlib.ticker.FixedLocator(locs=xticks)
xmajorFormatter = matplotlib.ticker.ScalarFormatter()
plt.gca().xaxis.set_major_locator( xmajorLocator )
plt.gca().xaxis.set_major_formatter( xmajorFormatter )
plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
plt.show()
这篇关于Matplotlib:设置 x 限制也会强制刻度标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!