Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

python - Merge Count Plot and Mean in same plot SNS

I am trying to create a count plot and also add another plot on it which would actually be the mean of the other columns.

The sample data is in the below link: Sample Data

I have used the below code to create the sns count plot:

df = pd.read_csv("latestfile.csv")
df.sort_values(by=["Business"],inplace=True)
sns.countplot(data=df,x=df["Business"],hue="location")

and I generate the below:

enter image description here

Now I use the groupby and use the below code to get the desired data:

dfg = df.groupby(["Business","location"])['Ageing'].mean().reset_index()
dfg.set_index("Business",inplace=True)

but how do I plot this on the same count plot on the different y axis.

Unable to think of a way to do it.

Below is what I am finally looking for:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Of course, you can squeeze another bar plot into the countplot graph:

import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

df = pd.read_csv("test.csv")
df.sort_values(by=["Business"],inplace=True)
ax1 = sns.countplot(data=df, x="Business", hue="location", palette="muted", edgecolor="black")
for patch in ax1.patches:
    patch.set_x(patch.get_x() + 0.3 * patch.get_width())
ax1.legend(title="Count")

ax2 = ax1.twinx()
sns.barplot(data=df, x="Business", y="Ageing", hue="location", palette="bright", ci=None, ax=ax2, edgecolor="white")
ax2.legend(title="Ageing")

ax1.autoscale_view()
plt.show()

enter image description here

However, I would definitely prefer two subplots:

import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

df = pd.read_csv("test.csv")
df.sort_values(by=["Business"],inplace=True)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

sns.countplot(data=df, x="Business", hue="location", ax=ax1)
ax1.legend(title="Count")

sns.barplot(data=df, x="Business", y="Ageing", hue="location", ci=None, ax=ax2)
ax2.legend(title="Ageing")

plt.show()

enter image description here

Since you prefer now the distribution, you can combine the countplot with a stripplot:

import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

df = pd.read_csv("test.csv")
df.sort_values(by=["Business"],inplace=True)

ax1 = sns.countplot(data=df, x="Business", hue="location")

ax2 = ax1.twinx()
sns.stripplot(data=df, x="Business", y="Ageing", hue="location", jitter=True, dodge=True, ax=ax2, linewidth=1)

ax1.legend(title="", loc="upper center")
ax2.legend_.remove()
plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...