最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python利用matplotlib绘制约数个数统计图代码实例
时间:2019-11-26 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Python利用matplotlib绘制约数个数统计图代码实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
利用Python计算1000以内自然数的约数个数,然后通过matplotlib绘制统计图。
下图为约数个数的散点图及其分布情况的条形图。
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()
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27