Use scatter to draw and style a scatterplot
plt.scatter(2, 4, s=200)
#设置图表标题并给坐标轴加上标签
plt.title("Square Number", 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()
Use scatter to plot a series of points
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 Number", 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()
Automatically calculate data
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, s=40)
#设置图表标题并给坐标轴加上标签
plt.title("Square Number", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
#设置每个坐标轴的取值范围
plt.axis([0, 1100, 0, 1100000])
plt.show()
Remove the outline of the data points
matplotlib allows to specify colors for individual points in the scatterplot, the default is blue points and black outlines. Works well when the scatterplot contains few data points, but when plotting many points, the black outlines may stick together. To remove the outline of the data points, pass the argument edgecolor='none' when calling scatter()
plt.scatter(x_values, y_values, edgecolors='none', s=40)
custom color
To modify the color of the data points, pass the parameter c to scatter() and set it to the name of the color to use, such as:
plt.scatter(x_values, y_values, c='red', edgecolors='none', s=40)
You can also use the rgb mode to customize the color. You can pass the parameter c and set it as a tuple, which contains three decimal values between 0 and 1, representing the red, green, and blue components respectively. For example, create a A scatterplot of blue points:
use colormap
A colormap is a sequence of colors that fades from an initial color to an end color.
In visualizations, colormaps are used to highlight regularities in data, for example, you might show smaller values with lighter colors and larger values with darker colors.
For example, set c to a list of y values, and use the parameter camp to tell pyplot which color map to use, and display points with smaller y values in light blue and larger points in dark blue, as shown in the following figure:
Automatically save diagrams
The program automatically saves the diagram to a file, and the call to plt.show() can be replaced by a call to plt.savefig():
plt.savefig('squares_plot.png', bbox_inches = 'tight')
The first actual parameter specifies the file name, and stores the picture in the directory where the current py file is located
The second actual parameter can be ignored, specifying to cut off the extra blank area of the chart
The saved picture is as shown in the figure: