我使用此代码向轴添加单位,但 x 轴看起来很糟糕,数字和单位被覆盖。你知道如何解决吗?
plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%d cm'))
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%d cm'))
最佳答案
你需要旋转刻度的文本,试试这个:
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
plt.plot(range(1000,11000,1000),range(10))
plt.gca().xaxis.set_major_formatter(mticker.FormatStrFormatter('%d cm'))
plt.gca().yaxis.set_major_formatter(mticker.FormatStrFormatter('%d cm'))
for txt in plt.gca().xaxis.get_majorticklabels():
txt.set_rotation(90)
plt.tight_layout()
plt.show()
关于python - 在 matplotlib 中将单位设置为 X 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709427/