散点图

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. n = 1024
    4. X = np.random.normal(0,1,n)
    5. Y = np.random.normal(0,1,n)
    6. T = np.arctan2(Y,X) ## 反正切, for color value
    7. plt.scatter(X,Y,s=75,c=T,alpha=0.5) ## c:color s:size,alpha:透明度
    8. plt.xlim((-1.5,1.5))
    9. plt.ylim((-1.5,1.5))
    10. plt.xticks(()) ## 隐藏x刻度
    11. plt.yticks(()) ## 隐藏y刻度
    12. plt.show()

    Screen Shot 2021-11-28 at 10.04.25 PM.png
    漂亮的小点点

    柱状图

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. n = 12
    4. X = np.arange(n)
    5. Y1 = (1-X/float(n))*np.random.uniform(0.5,1.0,n)
    6. Y2 = (1-X/float(n))*np.random.uniform(0.5,1.0,n)
    7. plt.bar(X,+Y1,facecolor='#9999ff',edgecolor='white')
    8. plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white')
    9. for x,y in zip(X,Y1):
    10. #ha:horizontal alignment
    11. plt.text(x+0.1,y+0.05,'%.2f'%y,ha='center',va='bottom')
    12. for x, y in zip(X, Y2):
    13. # ha:horizontal alignment
    14. plt.text(x + 0.1, -y - 0.05, '-%.2f' % y, ha='center', va='top')
    15. plt.xlim(-.5,n)
    16. plt.xticks(())
    17. plt.ylim(-1.25,1.25)
    18. plt.yticks(())
    19. plt.show()

    酷炫的条形图Screen Shot 2021-11-28 at 10.17.55 PM.png
    等高线图

    1. def f(x,y):
    2. ## the height function
    3. return (1-x/2 + x**5 + y**3)*np.exp(-x**2-y**2)
    4. n = 256
    5. x = np.linspace(-3,3,n)
    6. y = np.linspace(-3,3,n)
    7. X,Y = np.meshgrid(x,y) ## 生成一个网格
    8. ## use plt.contourf to filling contours
    9. ## X,Y and value for (X,Y) point
    10. plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot) ## cmap:颜色map value
    11. ## 不要把countourf写成countour,就没有颜色了
    12. # 8是等高线把图像分成了多少部分,等高线的稠密程度
    13. ## use plt.contour to add contour lines
    14. C = plt.contour(X,Y,f(X,Y),8,colors = 'black',linewidths=.5)
    15. ## adding label
    16. plt.clabel(C,inline=True,fontsize=10)
    17. plt.xticks(())
    18. plt.yticks(())
    19. plt.show()

    Screen Shot 2021-11-28 at 11.54.06 PM.png
    火热的等高线
    keypoint是理解栅格
    类似热图的图片(从array生成)

    1. ##
    2. import matplotlib.pyplot as plt
    3. import numpy as np
    4. # image data
    5. ## 每个点是一个像素点
    6. a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
    7. 0.365348418405, 0.439599930621, 0.525083754405,
    8. 0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
    9. """
    10. for the value of "interpolation", check this:
    11. http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
    12. for the value of "origin"= ['upper', 'lower'], check this:
    13. http://matplotlib.org/examples/pylab_examples/image_origin.html
    14. """
    15. plt.imshow(a,interpolation='nearest',cmap='bone',origin='lower')
    16. plt.colorbar(shrink=0.9) ## p旁边的彩条
    17. plt.xticks(())
    18. plt.yticks(())
    19. plt.show()

    Screen Shot 2021-11-29 at 11.26.47 AM.png
    来个3D

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. from mpl_toolkits.mplot3d import Axes3D
    4. fig = plt.figure()
    5. ax = Axes3D(fig) ## 添加3d坐标
    6. ## X,Y value
    7. X =np.arange(-4,4,0.25)
    8. Y =np.arange(-4,4,0.25)
    9. X,Y = np.meshgrid(X,Y) # mesh底面
    10. R = np.sqrt(X**2+Y**2)
    11. # height value
    12. Z = np.sin(R)
    13. # print(R)
    14. # print(Z)
    15. ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'),
    16. edgecolor='black') # rstride cstride 行、列跨度
    17. ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow') ## zdir 从哪个轴压扁
    18. ## offset 压到该轴的哪个平面
    19. ax.set_zlim(-2,2)
    20. plt.show()

    Screen Shot 2021-11-29 at 11.46.42 AM.png