Python 在matplotlib中将单位设置为X轴

Python 在matplotlib中将单位设置为X轴,python,matplotlib,Python,Matplotlib,我使用此代码向轴添加单位,但x轴看起来很糟糕,数字和单位被覆盖。你知道怎么解决吗 plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%d cm')) plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%d cm')) 无需指定每个刻度中使用的单位,因为它在该特定轴上都是相同的。我建议您使用xlabel()和ylabel()方法,就像matplotlib文档中的示例一样:

我使用此代码向轴添加单位,但x轴看起来很糟糕,数字和单位被覆盖。你知道怎么解决吗

plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%d cm'))
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%d cm'))

无需指定每个刻度中使用的单位,因为它在该特定轴上都是相同的。我建议您使用
xlabel()
ylabel()
方法,就像matplotlib文档中的示例一样:


您需要旋转记号的文本,请尝试以下操作:

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()

您可以尝试旋转刻度标签