D. RoDe
D. RoDe

Reputation: 33

How can i add a legend to multiple pyplot histogram?

I would like to identify the next combined histogram with a legend

import matplotlib.pyplot as plt

nbins=10

plt.title('Gaussian random numbers B-M')
plt.axis([-3, 3, 1, 25])
plotcos = plt.hist(coseno, nbins, alpha=.8, edgecolor = 'black',  linewidth=1)
plotsen = plt.hist(seno, nbins, alpha=.8, edgecolor = 'black',  linewidth=1)
plt.show()

Tnx enter image description here

Upvotes: 1

Views: 3008

Answers (1)

Sheldore
Sheldore

Reputation: 39052

I assume by "next combined histogram" you mean individual legends for each histogram. Just use the label parameter in both your plot commands and then show the legend using plt.legend() as

plotcos = plt.hist(coseno, nbins, alpha=.8, edgecolor = 'black',  linewidth=1, label='coseno')
plotsen = plt.hist(seno, nbins, alpha=.8, edgecolor = 'black',  linewidth=1, label='seno')
plt.legend()

Upvotes: 1

Related Questions