Python mathplotlib在xticks中重叠文本并在yticks中添加百分比符号

2024-10-06 12:18:27 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我生成条形图的Python代码:

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

objects = ('Increasing operational efficiency',
       'Informing strategic direction',
       'Better customer service',
       'Identifying and developing new products',
       'Enhanced customer experience',
       'Identifying new markets',
       'Faster go to market',
       'Complying with regulations',
       'Other')
y_pos = np.arange(len(objects))
performance = [51, 36, 27, 24, 20, 11, 8, 6, 3]

plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)

plt.show()

在输出中,xtick被重叠了,有什么方法可以克服它吗。我的第二个疑问是,从0到60的值之间的间隔是10,有没有办法在0%、10%、…、60%等数字的同时加上“%”符号,而不是0、10、…、60%。在

谢谢你的帮助,我是mathplotlib新手


Tags: 代码posimportnewobjectsmatplotlibasperformance
1条回答
网友
1楼 · 发布于 2024-10-06 12:18:27

你只要简单的搜索就能找到问题的答案。。。在

可以使用plt.gcf().autofmt_xdate()旋转x轴标签

对于y轴上的百分比符号,使用

ax = plt.gca()
vals = ax.get_yticks()
ax.set_yticklabels(['{:.0f}%'.format(x) for x in vals])

相关问题 更多 >