如何调整xaxis宽度以适应和显示绘图中的每个单词字符串?

2024-06-01 09:50:52 发布

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

我正在尝试设置宽度自动调整,以适应x轴上的每个字符串。这些词是“舰队”,“服务”,“信息技术”。。。等(共约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轴上的红色框。我需要把这些话说清楚。你知道吗


Tags:
字符串df宽度nppltamountmarkerlabel
1条回答
网友
1楼 · 发布于 2024-06-01 09:50:52

我的建议是(如上所述)简单地交换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()

相关问题 更多 >