如何调整x轴宽度以适合并显示图中的单词的每个字符串?

时间:2019-05-08 09:44:58

标签: python string matplotlib width

我试图将宽度设置为自动调整以适合x轴上的每个字符串。这些词是“ Fleet”,“ Services”,“ Information Technology”等(总共约10个)

x = df.Category.astype(str)
y = df.Amount
plt.scatter(x,np.e**y,label ="$ Amount", color = "k", marker = "*", s = 3)

enter image description here

请参阅x轴上的红色框。需要那些词清楚地散开。

1 个答案:

答案 0 :(得分:0)

我的建议是(如上所述)简单地将x和y交换为在y轴上具有可读的类别名称:

enter image description here


# Code to reproduce image above:

# import numpy as np
# import pandas as pd
# import matplotlib.pyplot as plt

# np.random.seed(42)
# cats = ['Facility', 'Fleet', 'Services', 'Information Technology', 'Facility2', 'Fleet2', 'Services2', 'Information Technology2']
# df = pd.DataFrame({'Category': np.random.choice(cats, 100000), 'Amount': np.random.random(100000)*750})
# x = df.Category.astype(str)
# y = df.Amount
plt.scatter(np.e**y, x, label ="$ Amount", color = "k", marker = "*", s = 3)
# plt.tight_layout()