Matplotlib:设置x限制也会强制使用记号标签?
我刚刚升级到matplotlib 2.0,我感觉自己像是在吃疯狂的药丸。我试着做一个对数线性图,y轴在线性刻度上,x轴在对数10刻度上。以前,以下代码允许我精确地指定记号的位置以及它们的标签:Matplotlib:设置x限制也会强制使用记号标签?,matplotlib,Matplotlib,我刚刚升级到matplotlib 2.0,我感觉自己像是在吃疯狂的药丸。我试着做一个对数线性图,y轴在线性刻度上,x轴在对数10刻度上。以前,以下代码允许我精确地指定记号的位置以及它们的标签: 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')
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显然希望在其中自动创建记号:
但如果我注释掉“plt.xlim(0.4,2.0)”行并让它自动确定轴限制,则没有重叠的记号标签,我只得到我想要的:
但这不起作用,因为我现在有无用的x轴限制
有什么想法吗
编辑:对于将来在互联网上搜索的人,我越来越相信这实际上是matplotlib本身的一个bug。我又回到v。1.5.3. 只是为了避免这个问题 重叠的附加标签来自一些次要标签,这些标签出现在绘图中。要消除它们,可以将次要格式化程序设置为
NullFormatter
:
plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
问题中的完整代码可能如下所示
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设置为字符串,其中我们使用FixedLocator
和ScalarFormatter
此代码生成与上述相同的绘图
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()
重叠的其他TickLabel源自一些次要的TickLabel,它们出现在绘图中。要消除它们,可以将次要格式化程序设置为
NullFormatter
:
plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
问题中的完整代码可能如下所示
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设置为字符串,其中我们使用FixedLocator
和ScalarFormatter
此代码生成与上述相同的绘图
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()