I have a pandas dataframe with a ‘frequency_mhz’ variable and a ‘type’ variable. I want to create a dist plot using seaborne that overlays all of the frequencys but changes the colour based on the ‘type’.
JavaScript
x
13
13
1
small_df = df[df.small_airport.isin(['Y'])]
2
medium_df = df[df.medium_airport.isin(['Y'])]
3
large_df = df[df.large_airport.isin(['Y'])]
4
5
plt.figure()
6
sns.distplot(small_df['frequency_mhz'], color='red')
7
8
plt.figure()
9
sns.distplot(medium_df['frequency_mhz'], color='green')
10
11
plt.figure()
12
sns.distplot(large_df['frequency_mhz'])
13
Is there a way I can overlay the 3 into one plot? or a way ive missed to change the colour of the bars based on another variable as you can with ‘hue=’ in other plots?
Advertisement
Answer
You can specify ax
as kwarg to superimpose your plots:
JavaScript
1
9
1
small_df = df[df.small_airport.isin(['Y'])]
2
medium_df = df[df.medium_airport.isin(['Y'])]
3
large_df = df[df.large_airport.isin(['Y'])]
4
5
ax = sns.distplot(small_df['frequency_mhz'], color='red')
6
sns.distplot(medium_df['frequency_mhz'], color='green', ax=ax)
7
sns.distplot(large_df['frequency_mhz'], ax=ax)
8
plt.show()
9