【发布时间】:2021-06-25 23:05:27
【问题描述】:
我有一个如下所示的数据集:
import pandas as pd, seaborn as sns, matplotlib.pyplot as plt, numpy as np
data = {"country": ["USA", "USA", "USA", "GBR", "GBR", "GBR", "IND", "IND", "IND"],
"sector": ["Others", "Sec1", "Sec2", "Others", "Sec2", "Sec1", "Others", "Sec1", "Sec3"],
"counts": [8763, 8121, 7822, 580, 481, 460, 332, 193, 154]}
df = pd.DataFrame.from_dict(data)
df['counts_log'] = df['counts'].apply(lambda x: np.log10(x))
当我使用以下代码绘制此数据时:
plt.figure(figsize=(18, 6))
sns.barplot(x='country', y='counts_log', hue='sector', data=df, palette='tab10')
plt.legend([],[], frameon=False)
plt.show()
我遇到以下问题(IND 的条之间总是有一些空间):
无论我尝试过什么,它都不会消失。如何解决这个问题?
【问题讨论】:
-
你有没有试过弄乱
figsize? -
在我们继续之前,您的 Sec2 数据是否真的缺少 IND 或者这是一个错字?在您的字典中,IND 有 Sec3,其他国家有 Sec2。
-
@ObjectJosh - 不,我对此非常小心...... :)
-
@pakpe - 没有错字,数据集是这样的......谢谢。:)
标签: python pandas seaborn bar-chart