python - 在matplotlib中自动缩写长度字符串

标签 python matplotlib

如果有与相邻文本冲突的情况,有没有一种方法可以自动减少matplotlib中字符串的长度,例如:
enter image description here

我要寻找的是如果matplotlib检测到与另一文本的冲突,它将自动减少这些文本,以使所有文本都可读。

请注意,当然可以使用rotate the labelsmake the fontsize smaller来防止重叠。但是在这里,我想问如何在重叠的情况下剪切文本。

最佳答案

您可以旋转刻度标签。

%matplotlib inline 
import matplotlib.pyplot as plt

countries = ["United Arab Emirates", "Central African Republic", "Bosnia and Herzegovina"]
population = [9770529, 4745185, 3301000]


x = plt.bar(range(len(countries)), population)

plt.title("Countries in the world by population (2019)")     
plt.ylabel("population")   

plt.xticks(range(len(countries)), countries, rotation='40')
plt.yscale('log')

plt.show()


enter image description here

关于python - 在matplotlib中自动缩写长度字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57824734/

相关文章:

python - 无法从 pyplot 子图中删除 yticks

python - 为什么这个程序中索引会越界?

python - 如何将文件转换为字典?

python - 所有 Python 类都应该扩展对象吗?

python - 在 Python 中获取客户端配置文件 Azure 的访问 token

python - Matplotlib:如何根据列值为散点图圆圈赋予颜色

python - 何时使用 matplotlib.pyplot 类以及何时使用绘图对象 (matplotlib.collections.PathCollection)

python - 为什么从 RGB 到 HSV 的颜色转换会产生损坏的图像?

Python tkinter Canvas 动态创建.Line

Python绘制两个不同长度的列表