Reputation: 51
I used seaborn to plot two poinplot. when I use plt.legend, the legend 's color is the same and it is not distinguished. The problem is the color of so2 and no2 both are blue in the legend. I think it just pick the color of second line of query.
ax=plt.subplots(figsize=(15,5))
sns.pointplot(x='Year', y='no2', data=AP_trend)
sns.pointplot(x='Year', y='so2', data=AP_trend, color = 'r')
plt.legend(labels=['no2', 'so2'])
Upvotes: 2
Views: 516
Reputation: 1599
I think the hue
parameter is what you are looking for, with some data manipulation beforehand.
I would use only 2 columns for x and y axis and a third column for the legend.
So something like this should work :
# data manipulation
x = df['Year'].values
x = np.hstack((x, x)) # stacking 2 times the same x as no2 and so2 vectors share the same Year vector
y = df['no2'].values
y = np.hstack((y, df['so2'].values)) # stacking so2 and no2 values
z1 = ['no2' for i in range(len(df))]
z2 = ['so2' for i in range(len(df))]
z = np.hstack((z1, z2)) # first part of the dataframe correspond to no2 values, the remaining correspond to so2 values
df2 = pd.DataFrame(data= {'Year': x, 'y' : y, 'legend' : z})
sns.pointplot(x='Year', y='y', hue='legend', data=df2)
EDIT : In seaborn there is no reason to make 2 plots like you are doing. The hue parameter is the way to do that.
Upvotes: 1
Reputation: 1350
Try adding frameon=True
in your legend call?
plt.legend(labels=['no2', 'so2'], frameon=True)
If the legend doesn't look good enough,
There are more options to try out when making the plt
object.
I'd test it out, but those 4 lines aren't enough to run a minimum example.
Upvotes: 0