- 15.2.1 绘制简单图形
画一个1-5的平方
# 导入模块
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
# 将列表传给它
plt.plot(squares)
# 打开matplotlib查看器,并显示所绘制的图形
plt.show()
- 15.2.1 修改标签文字和线条粗细
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
plt.plot(squares, linewidth=5) # 决定了plot()绘制出的线条粗细
# 设置图标提标,并给坐标轴加上标签 # 参数fontsize 指定文字大小
plt.title("Square Numbers", fontsize=24) # 设置图表标题
plt.xlabel("Value", fontsize=14) # 设置x轴标题
plt.ylabel("Square of Value", fontsize=14) # 设置y轴标题
# 设置刻度标记的大小
plt.tick_params(axis='both', labelsize=14) # 设置刻度的样式,其中指定的实参影响x,y轴上的刻度,并将刻度标记的字号设置为 14
plt.show()- 15.2.2 校正图形
此时的图形x轴 4 对应的是 25 因为plot()是从 0 开始算的
input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.plot(input_values, squares, linewidth=5)
- 15.2.3 使用scatter()绘制散点图并设置其样式
import matplotlib.pyplot as plt
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
# 参数edgecolor=‘none’将散点改为实心点
plt.scatter(x_values, y_values, c=(0, 0, 0.8), edgecolor='none', s=40) # 调用scatter函数使用实参s设置了绘制图形时使用的点的尺寸
# 给点上色可以传递参数'C(0, 1, 0.5)'分别是红,绿,蓝
# 设置图标标题并给坐标轴加上标签
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
# 设置刻度标记大小
plt.tick_params(axis='both', which='major', labelsize=14)
# 设置每个坐标轴的取值范围
plt.axis([0, 1100, 0, 1100000]) # axis([x.min, x.max, y.min, y.max])
plt.show()