图像属性
接下来简单介绍几个常用的图形属性.
坐标轴上下限
设置坐标轴上下限有两种方法 lim 和
axis.
示例 1
效果图

代码
lim
1 2 3 4 5 6 7 8 9 10
| import matplotlib.pyplot as plt
x = [520, 521, 522, 523] y = [1, 2, 3, 4]
fig1 = plt.figure() plt.plot(x, y, color='#112d4e') plt.xlim(520, 523) plt.ylim(1, 4) plt.show()
|
axis
1 2 3 4 5 6 7 8 9
| import matplotlib.pyplot as plt
x = [520, 521, 522, 523] y = [1, 2, 3, 4]
fig1 = plt.figure() plt.plot(x, y, color='#112d4e') plt.axis([520, 523, 1, 4]) plt.show()
|
plt.axis('equal') 会变成 1 : 1 比例
标题 与 坐标轴标签
示例 2
用中心极限定理示例.

代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import numpy as np import matplotlib.pyplot as plt
np.random.seed(32)
population = np.random.uniform(0, 1, size=100000)
sample_size = 30
num_samples = 1000
sample_means = []
for _ in range(num_samples): sample = np.random.choice(population, size=sample_size, replace=True)
mean = np.mean(sample)
sample_means.append(mean)
plt.hist( sample_means, bins=30, density=True, histtype='stepfilled', alpha=0.5, color='#8c82fc', edgecolor='#b693fe', )
mean_of_means = np.mean(sample_means) std_of_means = np.std(sample_means, ddof=1) xmin, xmax = plt.xlim() x = np.linspace(xmin, xmax, 100) p = np.exp(-((x - mean_of_means) ** 2) / (2 * std_of_means**2)) / ( std_of_means * np.sqrt(2 * np.pi) ) plt.plot(x, p, color='#F0988C', linewidth=2, label='Normal Distribution')
plt.title('Central Limit Theorem') plt.xlabel('Sample Mean') plt.ylabel('Frequency') plt.legend(loc='upper right', frameon=False)
plt.show()
|