I wrote a Python script that uses matplotlib twinx to combine a histogram and some line functions plot as can see in the figure. However I was not capable of combining both legends (nb of points and lines).
How can I do that? I tryed get_legend_handles_labels() to combine the hist and lines legend labels, also tryedto use fig.legend() with no success.
import matplotlib.pyplot as plt
import numpy
numpy.random.seed(19680801)
# example data
mu = 5000 # mean of distribution
sigma = 1500 # standard deviation of distribution
r1 = mu + sigma * numpy.random.randn(437)
sug_spec = numpy.arange(0, 10001, 100)
accuracy = sug_spec/2
precision = sug_spec/3
uncertainty = sug_spec/4
num_bins = 100
bins = numpy.arange(0, 10100, num_bins)
fig, ax = plt.subplots()
ax.set_xlabel('Surface Reflectance Truth')
# Plot lines
color = 'tab:pink'
ax.plot(bins, sug_spec, color=color)
color = 'tab:red'
ax.plot(bins, accuracy, color=color)
color = 'tab:green'
ax.plot(bins, precision, color=color)
color = 'tab:blue'
ax.plot(bins, uncertainty, color=color)
# Instantiate a second axes that shares the same x-axis
ax2 = ax.twinx()
# Order of each plot (Front, Back)
ax.set_zorder(2)
ax2.set_zorder(1)
ax.patch.set_visible(False)
# Remove scientific notation
ax2.ticklabel_format(style='plain')
# the histogram of the data
n, bins, patches = ax2.hist(r1, num_bins, edgecolor="k")
ax.legend(['suggested specs','accuracy','precision','uncertainty'], frameon=False, loc=1)
ax2.legend(['nb of points'], frameon=False, loc=5)
plt.show()