python - 使用 Seaborn 的多条形图
问题描述
我在 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)
但是,我希望它显示为绿色,即使覆盖的蓝色更大。有任何想法吗?
解决方案
由于无法查看您的数据,我只能猜测您的数据框不是长格式的。seaborn 教程中有一个部分是关于seaborn期望的 DataFrames 的预期形状,我会在那里查看更多信息,特别是关于混乱数据的部分。
因为我看不到你的 DataFrame 我对它的形状做了一些假设:
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
我们可以融合这个 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))
产生:
这是我用来制作此图表的绘图参数:
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")
请注意,在上面的代码中,索引设置为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")
推荐阅读
- java - 调用子类构造函数时来自子类或超类的数据类型?
- twitter-bootstrap - Yii2:Yii 升级/移动后,Glyphicons 没有出现在 gridview 中
- html - 是否可以使用 .pug 文件作为 index.html 中某个部分的包含?
- node.js - 在 Google Chrome 上加载块和意外令牌“<”时出错
- python - 如何在没有 smtp python 的情况下发送电子邮件
- python - 尝试将 csv 文件读入笔记本时出现 ParserError
- r - 如何在轴下使用比例和堆叠表进行多标准绘图?
- java - Spring MVC,为什么会出现 404 错误?
- python - selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:
- python - 在python中将数据插入oracle数据库