简体   繁体   English

Python Mathplotlib-在Xticks中重叠文本并在Yticks中添加百分比符号

[英]Python mathplotlib - overlapping texts in xticks and adding percentage symbol in yticks

The following is my Python code for generating a bar chart: 以下是我的用于生成条形图的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()

In the output the xticks are getting overlapped, is there any way to overcome it. 在输出中,xticks变得重叠了,有什么方法可以克服它。 My second doubt is in the yticks the values are coming from 0 to 60 with a interval of 10, is there any way to add a '%' symbol along with the number like 0%, 10%, ..., 60% rather than 0, 10, ..., 60. 我的第二个疑问是在yticks中,值从0到60的间隔为10,是否有办法添加'%'符号以及数字,例如0%,10%,...,60%大于0、10,...,60。

Thank you for the help, I am new to mathplotlib 谢谢您的帮助,我是mathplotlib的新手

you would have found answers to your questions with a simple search... 您将可以通过简单的搜索找到问题的答案...

you can rotate the x-axis labels using plt.gcf().autofmt_xdate() 您可以使用plt.gcf().autofmt_xdate()旋转x轴标签

for the percent signs on the y-axis, use 对于y轴上的百分号,请使用

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

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

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