Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a dataset (as a .txt file) used to make a histogram, but a journal has asked me to normalize the data and plot a histogram of the normalized data instead. However, I'm getting a "AttributeError: max must be larger than min in range parameter." error when I'm trying to plot the normalized data. Basically, H1 is my list of data (some may include nan values which I'm attempting to remove), and I'm trying to normalize the remaining data

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

H1 = np.loadtxt('histogramrate25p10area30.txt') #Import data from txt file

newH1 = [x for x in H1 if x != 'nan'] #Remove nan values
norm1 = [float(i)/max(newH1) for i in newH1] #Normalize remaining values

nbins1 = 400

plt.figure()

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

plt.show()

From browsing this website, the error resulted from the presence of nan values, but I thought in my newH1 list above, I've removed all the nan values, so I'm unsure what is causing this error.

like image 427
Brenton Avatar asked Oct 18 '22 17:10

Brenton


1 Answers

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.figure()

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

plt.show()

Explanation:

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.

like image 140
Abdou Avatar answered Oct 20 '22 11:10

Abdou