在matplotlib中将单位设置为X轴

6

我使用此代码为轴添加单位,但是x轴看起来很糟糕,数字和单位互相重叠了。您有什么解决方法吗?

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

enter image description here


你可以尝试旋转刻度标签。 - M4rtini
2个回答

9

你需要旋转刻度的文本,可以尝试以下方法:

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

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接