Matplotlib:设置x限制还会强制打勾标签吗? [英] Matplotlib: setting x-limits also forces tick labels?

查看:86
本文介绍了Matplotlib:设置x限制还会强制打勾标签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级到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.

有什么想法吗?

对于将来搜索Internet的人们,我越来越相信这实际上是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()

下面的代码可能更直观,因为它没有将xticklabel设置为字符串,下面是我们使用FixedLocatorScalarFormatter的代码.
这段代码产生与上面相同的情节.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆