Matplotlib-- drawing (scattergram, histogram, contour drawings, 3D FIG)

1. Scatter painting

First, the first introduction matplotlib.pyplot abbreviated PLT, reintroduction numpy module to generate some random data. It was 100 generates two-dimensional data set of standard normal distribution (mean 0, variance 1) as a data set, and the image of the data set. A color value for each point is represented by T:

import matplotlib.pyplot as plt
import numpy as np

X = np.random.normal(0,1,100)   #高斯分布(均值为0,方差为1,100个样本)
Y = np.random.normal(0,1,100)   
T = np.arctan2(Y,X)    #反正切函数,返回的是弧度

plt.scatter(X,Y,s=50,c=T,alpha=.8)   #alpha为透明度

plt.xlim(-3,3)
plt.ylim(-3,3)
plt.xticks(())     #参数为空代表隐藏x轴坐标
plt.yticks(())
plt.show()

X and Y inputs as location, size = 50, color is T, color map with default values, the transparency is 80% alpha. positioning the x-axis display range (3,3), and washed with XTICK () function to hide the x-coordinate axis, y-axis empathy. Results as shown:
Here Insert Picture Description

2. Draw a bar graph

Generating data 10, X is an integer from 0 to 9, Y is the corresponding uniformly distributed random data. Function uses plt.bar, the parameters X and Y,

Here we will optimize the color and value. Facecolor body provided with color, edgecolor set the border color to white

Next we add a column respectively above plt.text function values, with two decimals% .2f, transversely centered ha = 'center', aligned with the longitudinal bottom va = 'bottom':

import matplotlib.pyplot as plt
import numpy as np

X = np.arange(10)
Y = (1-X/float(10))*np.random.uniform(0.5,1.0,10)   #从均匀分布中随机采样(上界、下界、样本输出数目)

plt.bar(X,Y,facecolor='pink',edgecolor='white')

for x,y in zip(X,Y):
    plt.text(x+0.05,y+0.02,'%.2f' % y,ha='center',va='bottom')
    
plt.xlim(-.5,10)
plt.ylim(0,1.0)
plt.xticks(())
plt.yticks(())
plt.show()

Here Insert Picture Description

3. FIG contour

A set of data three-dimensional point (x, y) and the corresponding height value, a total of 100 points. Height value using a function f (x, y) generated. x, y are in the interval [3,3] is uniformly distributed 100 value, and associating with each meshgrid each x and each y in the two-dimensional plane, woven grid:

Next, fill color. Plt.contourf function using the color added to the list, the position parameters are: X, Y, f (X, Y). Transparency 0.75, and f (X, Y) corresponding to the value set to the warm color map to find the corresponding color.

Next, contour drawing performed. Use plt.contour scribing function. Location parameters: X, Y, f (X, Y). Black color is selected, the line width from 0.5. Wherein 8 represents the intensity of the contour lines, here it is divided into 10 parts. If it is 0, the image is divided into two.

Finally join Label, inline controls whether Label inside the painting online, font size 10. And hide the coordinate axes:

import matplotlib.pyplot as plt
import numpy as np

def f(x,y):
    return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 -y**2)
    
x = np.linspace(-3,3,100)
y = np.linspace(-3,3,100)
X,Y = np.meshgrid(x,y)

plt.contourf(X,Y,f(X,Y),6,alpha=.75,cmap=plt.cm.cool)  #添加颜色进去
C = plt.contour(X,Y,f(X,Y),6,colors='black',linewidth=.5) #画轮廓

plt.clabel(C, inline=True, fontsize=10)
plt.xticks(())
plt.yticks(())
plt.show()

Here Insert Picture Description

4.image 图

import matplotlib.pyplot as plt
import numpy as np

a = np.arange(9).reshape((3,3))   #构造3x3矩阵
plt.imshow(a, interpolation='nearest',cmap='bone',origin='upper') #升序打印不同值的颜色
plt.colorbar(shrink=.95)  #颜色棒缩放为原来的95%

plt.xticks(())    #隐藏x坐标轴
plt.yticks(())    #隐藏y坐标轴
plt.show()

Here Insert Picture Description

5. FIG. 3D Videos

In addition to introducing matplotlib, but also add an additional first module performing 3D Plot, i.e. Axes 3D 3D display axis.

Next feed X and Y values, and X and Y are woven in a lattice. Each height value (X, Y) corresponding to the point we calculated using the following function.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D     #导入3D显示模块

fig = plt.figure()   #定义一个图像窗口
ax = Axes3D(fig)   #在窗口上添加3D坐标轴
X = np.arange(-3,3,0.1)
Y = np.arange(-3,3,0.1)
X,Y = np.meshgrid(X,Y)    #将 X 和 Y 编织成栅格
R = np.sqrt(X**2 + Y**2)    
Z = np.sin(R)    #z轴

ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))  #横纵坐标跨度为1
ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap=plt.get_cmap('rainbow'))   #投影到z=-2处
ax.set_zlim(-2,2)

plt.show()

Here Insert Picture Description

He published 199 original articles · won praise 566 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_37763870/article/details/105344164