Histogram plotting “AttributeError: max must be larger than min in range parameter.”

The following should do:

import numpy as np
from numpy import array
import matplotlib.pyplot as plt

H1 = np.loadtxt('histogramrate25p10area30.txt')

newH1 = H1[~np.isnan(H1)]
norm1 = np.apply_along_axis(func1d=lambda x: x/np.max(newH1), arr=newH1, axis=0)
nbins1 = 400
plt.hist(norm1, nbins1, color='purple', alpha=0.5)


plt.hist(norm1, nbins1, color='purple', alpha=0.5)
plt.ylabel('Frequency', fontsize=20)



The script above loads the data with the help of the np.loadtxt function and subsequently removes the rows that contain null values. The latter is done by indexing the imported array with the boolean array ~np.isnan(H1). Here, np.isnan finds the rows where the values are null or nan and the ~ sign negates that; changing the True values to False, and vice-versa. Once that’s done, it moves on to applying a function to each value of the new array. The function here is lambda x: x/np.max(newH1); which basically divides each value of the array by the maximum value present in the new array.

The next step is to plot the histogram. We set the number of bins needed to 400 and use plt.hist to plot the histogram. There is also an added bonus to create a figure and then add a subplot to our figure. Subsequently, we use the subplot to draw the histogram.

I hope this proves useful.

Leave a Comment