python - 在 matplotlib 中将单位设置为 X 轴

标签 python matplotlib

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

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

enter image description here

最佳答案

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

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/

相关文章:

python - 使用 Exchangelib 连接到 Exchange 时出现授权错误

python - Pandas :如何对行上的多个级别进行计数分组?

python - 分组后用均值和标准差绘制误差线

python - 比较文件中的值 python

python - numpy:比较两个日期值返回 TypeError: '>=' 和 'int' 的实例之间不支持 'datetime.datetime'

python - 如何使用 Matplotlib 绘制 3D 扇区?

python - 在绘图旁边显示(离散)颜色条作为(自动选择的)线条颜色的图例

image - 翻转 Matplotlib imshow() 的 x 和 y 轴

python - 如何使用 sklearn 预处理器

python - 无法将 numpy 数组 reshape 为 RGB 图像