Plot a simple scatter plot
To draw a single point, use the scatter () function and pass a pair of x and Y coordinates to it, which draws a point at the specified position
Import Matplotlib.pyplot as Pltplt.scatter (2,4) plt.show ()
Operation Result:
Graphic Landscaping
The following sets the output style to make it more interesting: add headings, label axes
ImportMatplotlib.pyplot as Pltplt.scatter (2,4,s=200)#set the title and add axis labelsPlt.title ("Squares Numbers", fontsize=24) Plt.xlabel ("Value", fontsize=14) Plt.xlabel ("Square of Value", fontsize=14)#set the size of the tick markPlt.tick_params (axis='both', which='Major', labelsize=14) plt.show ()
Operation Result:
Draw a series of scatter points
To draw a series of scatter points, pass a list of 2 x and Y values to scatter ()
The coordinates plotted were (2,4), (3,9), (4,16), (5,25)
Automatically generate data plot scatter plots
ImportMatplotlib.pyplot as Pltx= List (range (1,1001)) Y= [x**2 forXinchX]plt.scatter (X,y,s=200)#set the title and add axis labelsPlt.title ("Squares Numbers", fontsize=24) Plt.xlabel ("Value", fontsize=14) Plt.xlabel ("Square of Value", fontsize=14)#set the size of the tick markPlt.tick_params (axis='both', which='Major', labelsize=14)#set the range of values for each coordinatePlt.axis ([0,1100,0,1100000]) plt.show ()
Operation Result:
Delete the outline of a data point
As can be seen, when drawing many points, the contour will be joined together, to delete the contour of the data point can call scatter (), pass argument edgecolor= ' None '
Custom Colors
To modify a color, simply pass the parameter C to scatter () and set it to the name of the color you want to use
Operation Result:
Using color maps
Module Pyplot has a set of color mappings built in to use these color mappings, you need to tell Pyplot how to set the color of each point in the dataset
Operation Result:
Auto-Save Scatter plots
Do I need to save the scatter plot automatically after the above graphic is finished? If necessary, proceed as follows:
Note: You must note the Plt.show () code when you save the graphic, otherwise the graphic is blank after the save is complete
After clicking Run, you can see that the picture is saved in the directory where the program is located.
First argument: Specifies the saved picture name
Second argument: Cuts out the specified white space
"Python" uses scatter () to plot a scatter plot.