Article Directory
There are some differences in the data visualization of python under different tools. But the principle of data construction is the same. The data construction of this part can refer to the previous method.
Drawing mode:
pyplot: Classic level package (hereinafter pyplot manner)
pylab: Matplotlib and the combined Numpy module, programming environment Matlab simulation
object-oriented (Object-Oriented): base layer and others based approach
// An highlighted block
import numpy as np
import matplotlib.pyplot as plt
Scatter plot
Correlation between two variables
Drawing
// An highlighted block
height=[160,170,180,190,200]
weight=[50,51,52,53,54]
plt.scatter(height,weight)
plt.show()
Appearance adjustment
c, point size: s (area), transparency: alpha, point shape: marker
// An highlighted block
height=[160,170,180,190,200]
weight=[50,51,52,53,54]
plt.scatter(height,weight,s=300,c='r',marker='<',alpha=0.5)
plt.show( )
line chart
Observe the trend of data over time
Drawing
// An highlighted block
x=np.linspace(-10,10,5)
y=x**2
plt.plot(x,y)
plt.show()
Appearance adjustment
Realize the linear drawing of two lines at the same time
: linestyle color: color point shape: marker
// An highlighted block
x=np.linspace(-10,10,5)
y=x**2
y1=x*2
plt.plot(x,y,linestyle='-',c='r',marker='o')
plt.plot(x,y1,linestyle='--',c='g',marker='<')
plt.show()
Bar graph
Compare the data size of multiple item classifications and use smaller data sets for analysis
Drawing
// An highlighted block
y=[50,30,40,20,60]
index=np.arange(5)
pl=plt.bar(x=index,height=y)
plt.show()
Appearance adjustment
Bandwidth width color color
// An highlighted block
y=[50,30,40,20,60]
index=np.arange(5)
pl=plt.bar(x=index,height=y,width=0.5,color='r')
plt.show()
Drawing of two histograms
// An highlighted block
y1=[50,30,40,20,60]
y2=[55,35,45,25,65]
index=np.arange(5)
p1=plt.bar(x=index,height=y1,width=0.3,color='r')
p2=plt.bar(x=index+0.3,height=y2,width=0.3,color='g')
plt.show()
Superimpose
// An highlighted block
y1=[50,30,40,20,60]
y2=[55,35,45,25,65]
index=np.arange(5)
p1=plt.bar(x=index,height=y1,width=0.5,color='r')
p2=plt.bar(x=index,height=y2,width=0.5,color='g',bottom=y1)
plt.show()
Histogram
Distribution of data
Drawing
// An highlighted block
mu=100
sigma=20
x=mu+sigma*np.random.randn(2000)
#normed 标准化
plt.hist(x,bins=20)
plt.show()
Appearance adjustment
normed color
// An highlighted block
mu=100
sigma=20
x=mu+sigma*np.random.randn(2000)
#normed 标准化
plt.hist(x,bins=20,color='green',normed=True)
plt.show()
Bivariate histogram
// An highlighted block
x=np.random.randn(1000)+2
y=np.random.randn(1000)+3
plt.hist2d(x,y,bins=40)
plt.show()
Pie chart
The data points in the pie chart are displayed as a percentage of the entire pie chart
Drawing
// An highlighted block
labels='A','B','C','D'
fracs=[15,30,10,45]
plt.axes(aspect=1)
plt.pie(x=fracs,labels=labels,autopct='%0.2f')
plt.show()
highlight
Highlight the'B','C' in the graph, explode controls the distance from the graph to the center of the circle, and shadow increases the shadow
// An highlighted block
labels='A','B','C','D'
fracs=[15,30,10,45]
explode=[0,0.08,0.08,0]
plt.axes(aspect=1)
plt.pie(x=fracs,labels=labels,autopct='%0.2f',explode=explode,shadow=True)
plt.show()
Box plot
The dispersion of the displayed data is
composed of the upper edge, upper quartile, median, lower quartile, lower edge, and outliers
Drawing
// An highlighted block
np.random.seed(100)
data=np.random.normal(size=1000,loc=0,scale=1)
plt.boxplot(data)
plt.show()
Appearance adjustment
Shape of outlier points, length of whis imaginary value: adjust the length of outlier
// An highlighted block
np.random.seed(100)
data=np.random.normal(size=1000,loc=0,scale=1)
plt.boxplot(data,sym='o',whis=0.5)
plt.show()
Plot multiple sets of data at the same time
// An highlighted block
np.random.seed(100)
data=np.random.normal(size=(1000,4),loc=0,scale=1)
labels=['A','B','C','D']
plt.boxplot(data,labels=labels,sym='o',whis=0.5)
plt.show()
Style string
It is very convenient when drawing color, point type, and line type as a string.
// An highlighted block
x=np.linspace(-10,10,5)
y=x**2
y1=2*x
plt.plot(x,y,'cx--')
plt.plot(x,y1,'mo:')
plt.show()