如何在 python seaborn 中制作多变量数据的直方图?
how to make histogram for multivariate data in python seaborn?
我有以下数据 我想要一个像下面这样的直方图,但我无法使用 python 来实现。谁能帮我在 python 中怎么做?
组
夏天
冬天
秋天
Spring
细菌
20
30
40
20
病毒
30
50
20
20
真菌
50
20
40
60
您可以将数据帧转换为 long form,然后用 weights=...
和 multiple='stack'
调用 sns.histplot()
。
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from io import StringIO
data_str = '''Group Summer Winter Autumn Spring
bacteria 20 30 40 20
virus 30 50 20 20
fungi 50 20 40 60'''
df = pd.read_csv(StringIO(data_str), delim_whitespace=True)
df_long = df.melt(id_vars='Group', var_name='Season')
hue_order = ['fungi', 'virus', 'bacteria']
sns.set()
ax = sns.histplot(data=df_long, x='Season', hue='Group', hue_order=hue_order,
weights='value', multiple='stack',
palette=['orange', 'gold', 'tomato'])
ax.legend(handles=ax.legend_.legendHandles, labels=hue_order, bbox_to_anchor=(1.02, 0.98), loc='upper left')
ax.set_ylabel('Percentage')
plt.tight_layout()
plt.show()
PS:如评论中所述,multiple='fill'
是另一种选择。在这种情况下,每个 x 值的条形都会拉伸到 填充 总高度。当数据框中的值是计数时(而不是百分比,例如示例数据的情况),这将特别有趣。
代码可能如下所示:
from matplotlib.ticker import PercentFormatter
# ... similar data preparation as in the other example
ax = sns.histplot(data=df_long, x='Season', hue='Group', hue_order=hue_order,
weights='value', multiple='fill',
palette=['orange', 'gold', 'tomato'])
ax.set_ylabel('Percentage')
ax.yaxis.set_major_formatter(PercentFormatter(1))
plt.tight_layout()
plt.show()
我有以下数据 我想要一个像下面这样的直方图,但我无法使用 python 来实现。谁能帮我在 python 中怎么做?
组 | 夏天 | 冬天 | 秋天 | Spring |
---|---|---|---|---|
细菌 | 20 | 30 | 40 | 20 |
病毒 | 30 | 50 | 20 | 20 |
真菌 | 50 | 20 | 40 | 60 |
您可以将数据帧转换为 long form,然后用 weights=...
和 multiple='stack'
调用 sns.histplot()
。
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from io import StringIO
data_str = '''Group Summer Winter Autumn Spring
bacteria 20 30 40 20
virus 30 50 20 20
fungi 50 20 40 60'''
df = pd.read_csv(StringIO(data_str), delim_whitespace=True)
df_long = df.melt(id_vars='Group', var_name='Season')
hue_order = ['fungi', 'virus', 'bacteria']
sns.set()
ax = sns.histplot(data=df_long, x='Season', hue='Group', hue_order=hue_order,
weights='value', multiple='stack',
palette=['orange', 'gold', 'tomato'])
ax.legend(handles=ax.legend_.legendHandles, labels=hue_order, bbox_to_anchor=(1.02, 0.98), loc='upper left')
ax.set_ylabel('Percentage')
plt.tight_layout()
plt.show()
PS:如评论中所述,multiple='fill'
是另一种选择。在这种情况下,每个 x 值的条形都会拉伸到 填充 总高度。当数据框中的值是计数时(而不是百分比,例如示例数据的情况),这将特别有趣。
代码可能如下所示:
from matplotlib.ticker import PercentFormatter
# ... similar data preparation as in the other example
ax = sns.histplot(data=df_long, x='Season', hue='Group', hue_order=hue_order,
weights='value', multiple='fill',
palette=['orange', 'gold', 'tomato'])
ax.set_ylabel('Percentage')
ax.yaxis.set_major_formatter(PercentFormatter(1))
plt.tight_layout()
plt.show()