Python Data visualization
Pip Install Matplotlib
Introduced:
Import Matplotlib.pyplot as PLT (a large number of interfaces are here side)
Draw a line chart:
x=[1,2,3,4] (Specify X-axis)
y=[4,5,6,7] (Specify y-axis)
Plt.plot (x, y) (passing an array, drawing a line chart)
Plt.plot (the name of the x,y,label= ' line ') (if you want to display the name of the line, pass in the function directly)
Plt.legend (loc=0) (number 1-10 to specify the location to display)
Plt.show () (shown)
(You want to display multiple line graphs at once, you only need to specify Plt.plot (X1,Y1) multiple times)
Plt.xlabel (' x-axis name ') (Specify x-axis name)
Plt.ylabel (' Y-axis name ') (Specify y-axis name)
Plt.title (' name of this line chart ') (Designation of the line chart name)
To draw a bar chart:
x=[1,2,3,4,5] (Specify X-axis)
y=[4,5,6,7,9] (Specify y-axis)
Plt.bar (x, y) (Draw a bar chart)
Plt.show () (shown)
Plt.axis ([0,12,0,7]) (Specify the x-axis and y-axis range, four parameters are x-axis from 0 to 12,y axis from 0 to 7)
or use the Xlim () and Ylim () functions as
Draw a bar chart based on how much data is in the volume
Import NumPy as NP
X=np.random.randint (1,100,100) (generates 100 random integers from 1 to 100)
BINS=[0,10,20,30,40,50,60,70,80,90,100] (Specify the range of divisions)
Plt.hist (X,bins) (the number of conforming data in this range according to the specified range)
Plt.hist (x,bins,rwidth=0.7) (Make bar chart spacing)
Plt.show ()
To plot a scatter plot:
X=np.random.randint (1,10,50) (generates random numbers)
Y=np.random.randint (1,10,50)
Plt.scatter (x, y) (scatter plot, or multiple sets of data simultaneously)
Plt.scatter (X,y,color = ' R ') (Specify color)
Plt.show ()
Object-oriented:
To draw a scatter point:
Fig,ax=plt.subplots () (Initialize canvas and image)
Ax.scatter (x, y)
Plt.show ()
To draw a pie chart:
Label= ' A ', ' B ', ' C ', ' D ' (set module tag)
SIZE=[12,30,45,10] (Set scale)
Fig,ax=plt.subplots ()
Ax.pie (size,labels = label) (Draw pie chart)
Ax.pie (size,labels = label,autopct= '%1.1f%% ') (percent displayed)
Ax.pie (size,labels = Label,shadow=true) (Show Shadows)
Ax.pie (size,labels = label,startangle=90) (Set start angle)
Ax.pie (size,labels = Label,explode=explode) (highlighting an element)
To use this option, you first define the explode variable
Explode= (0,0.1,0,0)
Ax.axis (' equal ') (Let the pie chart show up)
Plt.show ()
Python feature notes-data visualization