简体   繁体   中英

Is there a way to plot this using seaborn?

I'm trying to visualize this using seaborn but unable to find a way.

My dataframe looks like this:

Type    Vmax    Km
W1H     1.1031  34.976545
W1R     1.9014  200.745433
W1X     1.0447  20.796225

What I have tried so far:

sns.barplot(x='Vmax',y='Type',hue='Km',data=df,orient='h')

1 Results I got:

2 What I would like to have (Something like this): 在此处输入图像描述

在此处输入图像描述

I think you need the melt function from pandas . This allows you to obtain a variable that indicates the type of measurment (Km or Vmax) to pass as the hue parameter of seaborn (you can check out this paper to read more about this type of "tidy" data representation):

df = pd.DataFrame({"Type": ["W1H", "W1R", "W1X"], "Vmax": [1.1031, 1.9014, 1.0447],
                  "Km": [34.976545, 200.745433, 20.796225]})

molten = pd.melt(df, id_vars="Type")

# >>> molten
#   Type variable       value
# 0  W1H     Vmax    1.103100
# 1  W1R     Vmax    1.901400
# 2  W1X     Vmax    1.044700
# 3  W1H       Km   34.976545
# 4  W1R       Km  200.745433
# 5  W1X       Km   20.796225

f, ax = plt.subplots()
sbn.barplot(y="Type", x="value", hue="variable", orient="h", data=molten)

Output:

d

However, the scale of the two measurments is rather different, so you can also think about plotting them on two subplots:

f, ax = plt.subplots(1, 2)
sbn.barplot(y="Type", x="value", orient="h",
            data=molten.loc[molten["variable"] == "Vmax", :], ax=ax[0], color="black")
sbn.barplot(y="Type", x="value", orient="h",
            data=molten.loc[molten["variable"] == "Km", :], ax=ax[1], color="black")
ax[0].set(xlabel="Vmax", ylabel="")
ax[1].set(xlabel="Km", ylabel="")
f.set_size_inches(10, 5)

Output: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM