matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, *, data=None, **kwargs)
x, y: represents the size (n,) array, that is, we are going to draw a scatter plot of the data points
s: is a real number or an array of size (n,), this is an optional parameter.
c: represents the color, is also an option. The default is blue 'b', the flag indicates the color, or may represent a character color, or a color representation of length n of the sequence and the like. But not c is a single RGB digital, it can not be a sequence of RGBA. They may be two-dimensional array (only one line).
marker: indicates the style of the marker, the default is the 'o'.
cmap: Colormap colormap entity or a name, cmap only used only when c is a floating-point array of time. If you do not declare that image.cmap
NORM: the Normalize entities luminance data conversion between 0-1, c is only when the array is used only a floating point number. If you do not affirm that the default is colors.Normalize.
Vmin: Vmax: real number, when there is a norm ignored. Used for the luminance data normalized.
alpha: real number between 0-1.
linewidths: i.e. the length of the marker.
Now we have a ex0.txt file, part of the data as follows:
1.000000 0.067732 3.176513 1.000000 0.427810 3.816464 1.000000 0.995731 4.550095 1.000000 0.738336 4.256571 1.000000 0.981083 4.560815 1.000000 0.526171 3.929515 1.000000 0.378887 3.526170 1.000000 0.033859 3.156393 1.000000 0.132791 3.110301 1.000000 0.138306 3.149813
Let's write a function for reading data:
from numpy import * import numpy as np def loadDataSet(fileName): #general function to parse tab -delimited floats numFeat = len(open(fileName).readline().split('\t')) - 1 #get number of fields dataMat = [] labelMat = [] fr = open(fileName) for line in fr.readlines(): curLine = line.strip().split('\t') for i in range(1,numFeat): dataMat.append(float(curLine[i])) labelMat.append(float(curLine[-1])) return dataMat,labelMat xArr,yArr=loadDataSet("ex0.txt")
Then we can start drawing a scatter plot.
1, the general drawing mode:
import matplotlib.pyplot as Acting plt.scatter (xArr, Yarr) plt.show ()
2, set the size of the scatter
plt.scatter(x, y, s)
By modifying the value of s can be, for example set to 5.
3, change the color of scatter
plt.scatter(x, y, s, 'r')
4, change the style of scatter
5, a different set of data patterns for various
length=len(yArr)//2 import matplotlib.pyplot as plt plt.scatter(xArr[:length],yArr[:length],5,'r','^') plt.scatter(xArr[length:],yArr[length:],5,'b',marker='o') plt.show()
6, the legend
reference: