Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图隐藏 y 轴上小于 0 的任何值。我看到要隐藏 y 轴上的标签,我必须使用这样的东西:
make_invisible = True ax4.set_yticks(minor_ticks) if (make_invisible): yticks=ax4.yaxis.get_major_ticks() yticks[0].label1.set_visible(False)
我该如何调整它,以便如果 ytick 标签为负数,它将被隐藏?
您可以使用该set_xticks()方法简单地在 x 轴上设置您想要的刻度。
set_xticks()
import matplotlib.pyplot as plt plt.figure(figsize=(7,3)) plt.plot([-2,-1,0,1,2],[4,6,2,7,1]) ticks = [tick for tick in plt.gca().get_xticks() if tick >=0] plt.gca().set_xticks(ticks) plt.show()
将 every 替换x为 ay将为您提供 y 轴上的相应行为。
x
y