简体   繁体   中英

Converting Histogram Values to Int Array in Python

everyone. I created an histogram with by using an array with a size of 1x1000 and its values range from 0 to 99. I want to store histograms values as an int array. However, when I run the program I got the following results for the program(the numeric values are different for everyone since its random):

(array([ 93., 101., 119., 91., 97., 110., 102., 111., 85., 91.]), array([ 0. , 9.9, 19.8, 29.7, 39.6, 49.5, 59.4, 69.3, 79.2, 89.1, 99. ]), )

I want to get a result like this:

[ 93 101 119 91 97 110 102 111 85 91]

Can you help? Also what kind of array I am getting as a result? It doesn't look like anything I have ever seen before

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(100, size=1000)   
y = plt.hist(x,10)  

print(y)

When dealing with random numbers you can use np.random.seed(n) to ensure that others who run your code have the same numbers.

According to the docs you get both the bin values and the bin edges back from plt.hist() .

Take a look at the following:

print(y[0])
 [109. 109.  82. 117.  84. 101.  80. 108. 110. 100.]

[type(i) for i in y[0]]
 [<class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>]

You can assign y[0] to whatever you'd like and you should be good to go.

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