简体   繁体   中英

Plotting a histogram with data intervals

I tried to import a data file and then plot a histogram with this data. I was able to read the data file however I cannot turn this into a proper histogram with intervals. I imported the data with the following code:

data = np.loadtxt("data.dat", delimiter =';', dtype = str)

I am sharing the few lines of data to provide an insight:

pup1;20.0;
pup2;40.0;
pup3;70.0;
pup4;90.0;

The problem is I want to create a histogram for 2 intervals respectively 0-50 and 51-100.

I used the following code:

students = d[:,1]
grades = d[:,0]
bins = [0, 50, 100]
plt.hist(d, bins=bins, edgecolor='black')
plt.ylabel('Grades')
plt.xlabel('Students')

Bins takes the number of intervals into which you want to evenly divide your data. So in your case, bins=2. You can use the range argument to indicates you want the range between 0 and 100 to be divided into 2 bins. Also that data for the histogram would be just the grades. Here is an example:

grades = np.random.randint(0,100,30)
print(grades)

plt.hist(grades, bins=2, edgecolor = 'k', range = (0,100))
plt.xlabel('grades')
plt.ylabel('distribution of grades')
plt.show()

#grades
[22 48 18 11 12 29 37 91 48 85 83 12 36 78 83 84 87 89 20 17 79 73 36 74
 92 51 93 51 40 65]

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM