簡體   English   中英

多條 Plot 使用 Seaborn

[英]Multiple Bar Plot using Seaborn

我正在使用 seaborn 中的 3 個數據集制作條形圖,但是每個數據點都覆蓋了前一個數據點,無論它現在是否隱藏了前一個 plot。 例如:

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)

圖表

但是,我希望它顯示為綠色,即使覆蓋的藍色更大。 有任何想法嗎?

由於無法查看您的數據,我只能猜測您的 dataframe 不是長格式。 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 的內置聚合 plot 出箱 plot :

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))

產生:

帶有分類 y 軸的示例條形圖


這是我用來制作此圖表的 plot 參數:

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")

水平條形圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM