Python利用matplotlib繪制約數(shù)個數(shù)統(tǒng)計圖示例
本文實例講述了Python利用matplotlib繪制約數(shù)個數(shù)統(tǒng)計圖。分享給大家供大家參考,具體如下:
利用Python計算1000以內(nèi)自然數(shù)的約數(shù)個數(shù),然后通過matplotlib繪制統(tǒng)計圖。
下圖為約數(shù)個數(shù)的散點圖及其分布情況的條形圖。
Python代碼:
import collections import matplotlib.pyplot as plt def countDivisors(num): ans = 1 x = 2 while x * x <= num: cnt = 1 while num % x == 0: cnt += 1 num /= x ans *= cnt x += 1 return ans * (1 + (num > 1)) MAXNUM = 1000 x = range(1, MAXNUM) y = map(countDivisors, x) plt.subplot(2, 1, 1) plt.title('Divisors Count') plt.xlim(0, MAXNUM) plt.ylim(0, max(y) + 1) plt.scatter(x, y) plt.grid(True) plt.subplot(2, 1, 2) plt.title('Statistics of Divisor Count') z = collections.Counter(y) plt.bar(z.keys(), z.values(), align = 'center') plt.grid(True) plt.show()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python?IDLE?Subprocess?Connection?Error的簡單解決方法
最近用要Python處理一點事,就打開Python IDLE,結(jié)果出現(xiàn)錯誤,下面這篇文章主要給大家介紹了關(guān)于Python?IDLE?Subprocess?Connection?Error的簡單解決方法,需要的朋友可以參考下2023-01-01OpenCV模板匹配matchTemplate的實現(xiàn)
這篇文章主要介紹了OpenCV模板匹配matchTemplate的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10