繁体   English   中英

在matplotlib中将单位设置为X轴

[英]Set units to X-axis in matplotlib

我使用此代码将单位添加到轴,但x轴看起来很糟糕,数字和单位被覆盖。 您知道如何解决吗?

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

在此输入图像描述

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

http://matplotlib.org/examples/text_labels_and_annotations/text_demo_fontdict.html

您需要旋转刻度线的文本,试试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM