Plot over Seaborn plot and add legend

Viewed 80

See the code below. I create a plot with seaborn then I plot some other values over the same figure with plt.plot, works fine, but the plt.legend method apparently is not additive, that is, I cannot use it to label both lines in the chart, which would be 'from seaborn' and 'from plt', both associated with the proper line color.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
mean, cov = [0, 2], [(1, .5), (.5, 1)]
data_sample, y = np.random.multivariate_normal(mean, cov, size=100).T
data_min = min(data_sample)
data_max = max(data_sample)
plot_points = [(data_min + (i/100) * (data_max-data_min)) for i in range(0, 100)]
sns.set(color_codes=True)
plt.figure()
ax = sns.kdeplot(data_sample, legend='from seaborn')
ax.plot(plot_points, y)
plt.legend(['from plt'])
plt.show()

How can I address this?

1 Answers

Change legend to label and add label to plot:

ax = sns.kdeplot(data_sample, label='from seaborn')
ax.plot(plot_points, y, label='from plt')
ax.legend()

Output:

enter image description here

Related