顯示具有 [ Python 範例代碼 ] 標籤的文章。 顯示所有文章
顯示具有 [ Python 範例代碼 ] 標籤的文章。 顯示所有文章

2019年7月19日 星期五

[ Python 範例代碼 ] Matplotlib - 產生 normal distribution 的 Histogram

使用 np.random.normal 產生 random 的 normal distribution 數字, 接著使用 np.histogram 計算繪製 histogram 所需要的資料: 
  1. import numpy as np  
  2. import random as rd  
  3. from matplotlib import pyplot as plt   
  4.   
  5. # 產生 normal distribution 的數字  
  6. random_nums = np.random.normal(scale=20, size=10000)  
  7. random_ints = np.round(random_nums)  
  8.   
  9. # 計算 histogram 的相關數字  
  10. hist, bins = np.histogram(random_ints, bins=20)  
  11.   
  12. # 繪圖  
  13. plt.rcParams['figure.figsize'] = [157]  
  14. plt.hist(random_ints, bins)   
  15. plt.title("histogram")  
  16. plt.xticks(bins)  
  17. plt.show()  

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

[ Python 範例代碼 ] Metplotlib - Bar chart

Source From Here 
Preface 
Matplotlib may be used to create bar charts. You might like the Matplotlib gallery. The course below is all about data visualization: 
Data Visualization with Python and Matplotlib


Bar chart code 
The code below creates a bar chart: 
  1. %matplotlib inline  
  2. import matplotlib.pyplot as plt; plt.rcdefaults()  
  3. import numpy as np  
  4. import matplotlib.pyplot as plt  
  5.   
  6. objects = ('Python''C++''Java''Perl''Scala''Lisp')  
  7. y_pos = np.arange(len(objects))  
  8. performance = [10,8,6,4,2,1]  
  9.   
  10. plt.bar(y_pos, performance, align='center', alpha=0.5)  
  11. plt.xticks(y_pos, objects)  
  12. plt.ylabel('Usage')  
  13. plt.title('Programming language usage')  
  14.   
  15. plt.show()  
 

Matplotlib charts can be horizontal, to create a horizontal bar chart: 
  1. import matplotlib.pyplot as plt; plt.rcdefaults()  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4.   
  5. objects = ('Python''C++''Java''Perl''Scala''Lisp')  
  6. y_pos = np.arange(len(objects))  
  7. performance = [10,8,6,4,2,1]  
  8.   
  9. plt.barh(y_pos, performance, align='center', alpha=0.5)  
  10. plt.yticks(y_pos, objects)  
  11. plt.xlabel('Usage')  
  12. plt.title('Programming language usage')  
  13.   
  14. plt.show()  
 

More on bar charts 
You can compare two data series using this Matplotlib code: 
  1. import numpy as np  
  2. import matplotlib.pyplot as plt  
  3.   
  4. # data to plot  
  5. n_groups = 4  
  6. means_frank = (90554065)  
  7. means_guido = (85625420)  
  8.   
  9. # create plot  
  10. fig, ax = plt.subplots()  
  11. index = np.arange(n_groups)  
  12. bar_width = 0.35  
  13. opacity = 0.8  
  14.   
  15. rects1 = plt.bar(index, means_frank, bar_width,  
  16.                  alpha=opacity,  
  17.                  color='b',  
  18.                  label='Frank')  
  19.   
  20. rects2 = plt.bar(index + bar_width, means_guido, bar_width,  
  21.                  alpha=opacity,  
  22.                  color='g',  
  23.                  label='Guido')  
  24.   
  25. plt.xlabel('Person')  
  26. plt.ylabel('Scores')  
  27. plt.title('Scores by person')  
  28. plt.xticks(index + bar_width, ('A''B''C''D'))  
  29. plt.legend()  
  30.   
  31. plt.tight_layout()  
  32. plt.show()  
 

範例代碼 matplotlib_barchart.ipynb

[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...