我在这段代码上遇到了麻烦:
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
majorLocator = MultipleLocator(0.1)
majorFormatter = FormatStrFormatter('%2.1f')
fig = plt.figure()
axes = []
for i in range(4):
axes.append(fig.add_subplot(2,2,i+1))
for ax in axes:
ax.yaxis.set_major_locator(majorLocator)
ax.yaxis.set_major_formatter(majorFormatter)
ax.set_ylim(0,1)
axes[-1].set_ylim(1,2) #If you comment this line all works fine.
plt.show()
在我的屏幕上出现一个刻度问题。
但是,如果我在一行中添加注释,则axes[-1].set_ylim(1,2)
所有刻度线都会正确显示。这是一个错误吗?还是我做错了?
(matplotlib'1.3.0')
这是因为您要在多个y轴对象之间共享同一个定位器对象。
这不是一个错误,但它是一个细微的问题,可能引起很多混乱。有关此文档的文档可能会更清楚,但是定位符应该属于一个axis
。
实际上,您可以共享同一Formatter
实例,但是最好不要共享,除非您知道后果(更改为一个实例会影响所有实例)。
相反,回收相同的Locator
和Formatter
的情况下,为每个轴新的:
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
fig, axes = plt.subplots(2, 2)
for ax in axes.flat:
ax.yaxis.set(major_locator=MultipleLocator(0.1),
major_formatter=FormatStrFormatter('%2.1f'))
ax.set_ylim(0, 1)
axes[-1, -1].set_ylim(1, 2)
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句