python示例1
# -*- coding:utf-8 -*-
import math
import matplotlib.pyplot as plt
if __name__ == '__main__':
x = [float(i) / 100.0 for i in range(1, 300)]
y = [math.log(i) for i in x]
plt.plot(x, y, 'r-', linewidth=3, label='log Curve')
a = [x[20], x[175]]
b = [y[20], y[175]]
plt.plot(a, b, 'g-', linewidth=2)
plt.plot(a, b, 'b*', markersize=15, alpha=0.75)
plt.legend(loc='upper left')
plt.grid(True) # 显示网格
plt.xlabel('x')
plt.ylabel('log(x)')
plt.show()
python示例2
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
u = np.random.uniform(0.0, 1.0, 10000) # 产生10000个[0,1)的数
plt.hist(u, 80, facecolor='g', alpha=0.2) # 对应x轴,指定bin(箱子)的个数,80条状图,颜色
plt.grid(True)
plt.show()
times = 10000
for time in range(times):
u += np.random.uniform(0.0, 1.0, 10000)
print(len(u))
u /= times
print(len(u))
plt.hist(u, 80, facecolor='g', alpha=0.75)
plt.grid(True)
plt.show()
# 对数函数的速度
import math
import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
x = np.arange(0.05, 3, 0.05)
y1 = [math.log(a, 1.5) for a in x]
plt.plot(x, y1, linewidth=2, color='r', label='log1.5(x)')
plt.plot([1, 1], [y1[0], y1[-1]], 'r--', linewidth=2)
y2 = [math.log(a, 2) for a in x]
plt.plot(x, y2, linewidth=2, color='g', label='log2(x)')
y3 = [math.log(a, 3) for a in x]
plt.plot(x, y3, linewidth=2, color='b', label='log3(x)')
plt.legend(loc='lower right')
plt.grid(True)
plt.show()
import matplotlib.pyplot as plt
def first_digital(x):
while x >= 10:
x /= 10
return x
if __name__ == '__main__':
n = 1
frequency = [0] * 9
for i in range(1,100):
n *= i
m = int(first_digital(n) - 1)
frequency[m] += 1
print(frequency)
plt.plot(frequency,'r-',linewidth=2)
plt.plot(frequency,'go',markersize=8)
plt.grid(True)
plt.show()
python
最新推荐文章于 2022-04-30 16:20:44 发布