Python – Controlling Histogram Axis and Legend with Show_Hist in Rasterio

histogrammatplotlibpythonrasterio

I have load raster (Sentinel 2) with Rasterio and I would like to create histogram of the pixels.
The problem is that I beleive the graph gives automatically the X label and also the legend labels.

This is how I create the histogram:

his_data=src.read([4,3,2])

from rasterio.plot import show_hist
plt.xlim(xmin=0, xmax = 1)
plt.xlabel('Reflectance')
show_hist(his_data, bins=200, lw=0.0, stacked=False, alpha=0.8,histtype='stepfilled', title="Histogram")

and this is what I get- the legend labels are not like the bands and the x axis called DN even though my image is reflectance and I defined it as reflectance:
enter image description here

is there any way to control this ? or I need to change it into array and then plot it

Best Answer

Try creating the figure and axis explicitly:

fig, axhist = plt.subplots(1, 1)
show_hist(hist_data, bins=200, histtype='stepfilled',
          lw=0.0, stacked=False, alpha=0.8, ax=axhist)
axhist.set_xlabel('X')
axhist.set_ylabel('Y')
axhist.set_title('Title')
Related Question