[Python] Plotting, frequency histogram

Frequency histogram, data interval division and distribution density
% matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')

data = np.random.randn(1000) #The
most basic frequency histogram command
plt.hist(data) #Adjust

specific parameters#
bins adjust the number of abscissa partitions, the alpha parameter is used to set the transparency
plt.hist(data, bins =30, normed=True, alpha=0.5, histtype='stepfilled',
         color='steelblue', edgecolor='none'
 


#When comparing samples with different distribution characteristics, it is effective to use histtype='stepfilled' with the transparency setting parameter alpha
x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal( -2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)
kwargs = dict(histtype='stepfilled', alpha=0.3, normed=True, bins=40)
plt.hist(x1, * *kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3 , **kwargs) #If

you only need to simply calculate the number of samples in each interval, and don't want to draw a graph to show them, you can use np directly. histogram()
counts, bin_edges = np.histogram(data, bins=50)
print(counts)

#Output result:
[1 1 2 4 6 9 16 16 28 34 52 42 61 85 135 172 188 231
 295 315 343 386 383 400 401 364 325 319 279 240 195 147 136 100 80 69
  40 28 23 11 9 9 7 3 4 2 1 1 1 1]

 


Two-dimensional frequency histogram and data interval division
plt.hist2d: Two-dimensional frequency histogram
mean = [0, 0]
cov = [[1,1], [1,2]]
x, y = np.random.multivariate_normal( mean, cov, 10000).T
plt.hist2d(x, y, bins=30, cmap='Blues')
cb = plt.colorbar()
cb.set_label('counts in bin')
 


#If you only want to calculate the number of samples in each interval, you can use np.histogram2d()
counts, xedges, yedges = np.histogram2d(x, y, bins=30)

plt.hexbin: Hexagon interval division
plt.hexbin(x , y, gridsize=30, cmap='Blues')
cb = plt.colorbar(label='count in bin')

 

Original: https://blog.csdn.net/jasonzhoujx/article/details/81772846 
 

Guess you like

Origin blog.csdn.net/m0_37362454/article/details/90479417