2019年1月26日 星期六

[ Python 常見問題 ] Plot a histogram using Matplotlib in Python with a list of data?

Source From Here 
Question 
I am trying to plot a histogram using the matplotlib.hist() function but I am not sure how to do it. I have a list: 
  1. probability = [0.36021505376344090.42028985507246375,   
  2.   0.3731170336037080.368131868131868160.32517482517482516,   
  3.   0.41752577319587630.410256410256410240.39408866995073893,   
  4.   0.41432225063938620.340.3910256410256410.3130841121495327,   
  5.   0.35398230088495575]  
and a list of names(strings). 

How do I make the probability as my y-value of each bar and names as x-values? 

How-To 
If you want a histogram, you don't need to attach any 'names' to x-values, as on x-axis you would have bins
  1. import matplotlib.pyplot as plt  
  2. import numpy as np  
  3. %matplotlib inline  
  4.   
  5. x = np.random.normal(size = 1000)  
  6. # density: If True, the first element of the return tuple will be the counts normalized to form a probability density  
  7. plt.hist(x, density=True, bins=30)  
  8. plt.ylabel('Probability');  
 

However, if you have limited number of data points, and you want a bar plot, then you may attach labels to x-axis: 
  1. x = np.arange(3)  
  2. plt.bar(x, height= [1,2,3])  
  3. plt.xticks(x, ['a','b','c'])  
 

Supplement 
Generate data and plot a simple histogram

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...