Qasim Khan
Qasim Khan

Reputation: 154

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): enter image description here

enter image description here

Upvotes: 1

Views: 105

Answers (1)

Michael Mitter
Michael Mitter

Reputation: 575

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: enter image description here

Upvotes: 3

Related Questions