gpt4 book ai didi

python - 使用 Seaborn 的多条形图

转载 作者:行者123 更新时间:2023-12-04 07:43:15 24 4
gpt4 key购买 nike

我正在使用 seaborn 中的 3 个数据集制作条形图,但是每个数据点都会覆盖前一个数据点,无论它现在是否隐藏了前一个图。例如:

sns.barplot(x="Portfolio", y="Factor", data=d2,
label="Portfolio", color="g")

sns.barplot(x="Benchmark", y="Factor", data=d2,
label="Benchmark", color="b")

sns.barplot(x="Active Exposure", y="Factor", data=d2,
label="Active", color="r")

ax.legend(frameon=True)
ax.set(xlim=(-.1, .5), ylabel="", xlabel="Sector Decomposition")
sns.despine(left=True, bottom=True)

chart

但是,我希望它显示绿色,即使覆盖的蓝色更大。有什么想法吗?

最佳答案

由于无法看到您的数据,我只能猜测您的数据框不是长格式的。在 seaborn tutorial 上有一节关于 seaborn 期望的 DataFrames 的预期形状,我会在那里查看更多信息,特别是关于 messy data 的部分.


因为我看不到你的数据框,所以我对它的形状做了一些假设:

import numpy as np
import pandas as pd
import seaborn as sns

df = pd.DataFrame({
"Factor": list("ABC"),
"Portfolio": np.random.random(3),
"Benchmark": np.random.random(3),
"Active Exposure": np.random.random(3),
})
# Active Exposure Benchmark Factor Portfolio
# 0 0.140177 0.112653 A 0.669687
# 1 0.823740 0.078819 B 0.072474
# 2 0.450814 0.702114 C 0.039068

我们可以melt这个 DataFrame 用于获取 seaborn 想要的长格式数据:

d2 = df.melt(id_vars="Factor", var_name="exposure")
# Factor exposure value
# 0 A Active Exposure 0.140177
# 1 B Active Exposure 0.823740
# 2 C Active Exposure 0.450814
# 3 A Benchmark 0.112653
# 4 B Benchmark 0.078819
# 5 C Benchmark 0.702114
# 6 A Portfolio 0.669687
# 7 B Portfolio 0.072474
# 8 C Portfolio 0.039068

然后,最后我们可以使用 seaborn 的内置聚合绘制箱线图:

ax = sns.barplot(x="value", y="Factor", hue="exposure", data=d2)
ax.set(ylabel="", xlabel="Sector Decomposition")
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

产生:

Example barplot with categorical y axis


这是我用来制作这张图表的绘图参数:

import matplotlib as mpl

# Plot configuration
mpl.style.use("seaborn-pastel")
mpl.rcParams.update(
{
"font.size": 14,
"figure.facecolor": "w",
"axes.facecolor": "w",
"axes.spines.right": False,
"axes.spines.top": False,
"axes.spines.bottom": False,
"xtick.top": False,
"xtick.bottom": False,
"ytick.right": False,
"ytick.left": False,
}
)

如果不使用 seaborn 也没关系,您可以使用 pandas 绘图来创建堆叠水平条形图 (barh):

import pandas as pd
import matplotlib as mpl

# Plot configuration
mpl.style.use("seaborn-pastel")
mpl.rcParams.update(
{
"font.size": 14,
"figure.facecolor": "w",
"axes.facecolor": "w",
"axes.spines.right": False,
"axes.spines.top": False,
"axes.spines.bottom": False,
"xtick.top": False,
"xtick.bottom": False,
"ytick.right": False,
"ytick.left": False,
}
)

df = pd.DataFrame({
"Factor": list("ABC"),
"Portfolio": [0.669687, 0.072474, 0.039068],
"Benchmark": [0.112653, 0.078819, 0.702114],
"Active Exposure": [0.140177, 0.823740, 0.450814],
}).set_index("Factor")

ax = df.plot.barh(stacked=True)
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_ylabel("")
ax.set_xlabel("Sector Decomposition")

stacked horizontal bar chart

请注意,在上面的代码中,索引设置为 Factor,然后成为 y 轴。

如果您不设置 stacked=True,您将获得与 seaborn 生成的几乎相同的图表:

ax = df.plot.barh(stacked=False)
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_ylabel("")
ax.set_xlabel("Sector Decomposition")

horizontal bar chart

关于python - 使用 Seaborn 的多条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67337883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com