import matplotlib.pyplot as plt
plt.scatter(2, 4)
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200409192856535.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4OTAyOA==,size_16,color_FFFFFF,t_70#pic_center)
plt.scatter(2, 4, s = 100)
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.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200409192915414.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4OTAyOA==,size_16,color_FFFFFF,t_70#pic_center)
- plt.tick_params()函数中的可选参数which有三个值,分别是’major’, ‘minor’, ‘both’,分别表示修改主刻度尺、副刻度尺和所有刻度尺。这里的主副刻度尺类似于尺子上CM和MM的刻度。默认是which = ‘major’
x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]
plt.scatter(x_values, y_values, s = 100)
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.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200409192930553.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4OTAyOA==,size_16,color_FFFFFF,t_70#pic_center)
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, s = 4)
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])
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200409192941864.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4OTAyOA==,size_16,color_FFFFFF,t_70#pic_center)
- plt.axis()是设置坐标轴取值范围,函数要求提供四个值:x和y坐标轴的最小值、最大值。
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, c = 'red', edgecolor = 'none', s = 4)
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])
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200409192954591.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4OTAyOA==,size_16,color_FFFFFF,t_70#pic_center)
- 绘制的数据点默认是蓝色点和黑色轮廓
- 调用scatter()时传入实参edgecolor = ‘none’,可以去除数据点的黑色轮廓。
- 调用scatter()时传入实参c = ‘red’,可以修改数据点的颜色。c的取值也可以是RGB元组。
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, c = y_values, cmap = plt.cm.Blues, edgecolor = 'none', s = 4)
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])
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200409193009555.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg4OTAyOA==,size_16,color_FFFFFF,t_70#pic_center)
- 调用scatter()时传入实参
c = y_values
,将参数c设置成一个y值列表,然后使用参数cmap告诉pyplot使用哪个颜色的映射。于是值小的使用浅色,值大的使用深色,构成了一条渐变色曲线 - 最后可以调用
plt.savefig('squares_plot.png', bbox_inches = 'tight')
来存储图像文件。第一个参数给出文件名,第二个参数指定将图表多余的空白区域裁减掉,如果要保留多余空白区域可以省略这个实参。