zoukankan      html  css  js  c++  java
  • matplotlib_cheat_sheet

    1 Prepare the Data

    import numpy as np
    x=np.linspace(0,10,100)
    y=np.cos(x)
    z=np.sin(x)
    

    Data or Images

    1. narray
    2. mgrid
    3. images
    data = 2 * np.random.random((10, 10))
    data2 = 3 * np.random.random((10, 10))
    Y, X = np.mgrid[-3:3:100j, -3:3:100j] 
    U = -1 - X**2 + Y
    V = 1 + X - Y**2 
    from matplotlib.cbook import get_sample_data 
    img = np.load(get_sample_data('axes_grid/bivariate_normal.npy'))

    2 Creat Plot

    • creat figure object
    • creat subplot object
    import matplotlib.pyplot as plt
    fig = plt.figure()
    fig2 = plt.figure(figsize=plt.figaspect(2.0))
    fig.add_axes()
    ax1 = fig.add_subplot(221) # row-col-num
    ax3 = fig.add_subplot(212)
    fig3, axes = plt.subplots(nrows=2,ncols=2)
    fig4, axes2 = plt.subplots(ncols=3)

    3 Plotting Routines

    • plot with line
    • draw unconnected point
    lines = ax.plot(x,y)# Draw points with lines or markers connecting them
    ax.scatter(x,y) #Draw unconnected points, scaled or colored
    axes[0,0].bar([1,2,3],[3,4,5])# Plot vertical rectangles (constant width)
    axes[1,0].barh([0.5,1,2.5],[0,1,2])# Plot horiontal rectangles (constant height)
    axes[1,1].axhline(0.45) #Draw a horizontal line across axes
    axes[0,1].axvline(0.65) #Draw a vertical line across axes
    ax.fill(x,y,color='blue') #Draw filled polygons
    ax.fill_between(x,y,color='yellow') #Fill between y-values and 0
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax3 = fig.add_subplot(223)
    lines = ax1.plot(x,y)
    ax3.scatter(x,y)
    ax3.set_xlim(0,10)    #set axis limits
    #plt.clf() clear figure
    ax2 = fig.add_subplot(222)
    ax4 = fig.add_subplot(224)
    ax2.fill(x,y,color='blue')
    ax4.fill_between(x,y,color='yellow')
    fig

    基本的画图

    axes[0,0].bar([1,2,3],[3,4,5]) # Plot vertical rectangles (constant width)
    axes[1,0].barh([0.5,1,2.5],[0,1,2])# Plot horiontal rectangles (constant height)
    axes[1,1].axhline(0.45) #Draw a horizontal line across axes
    axes[0,1].axvline(0.65) #Draw a vertical line across axes
    fig3

    这里写图片描述

    Vector Fields

    • object : subplot
    • method?func : arrow
    • attribution :
    fig4, axes2 = plt.subplots(nrows=2,ncols=1)    #axes is a array.
    axes2[0].arrow(0,0,0.5,0.5)
    axes2[1].quiver(y,z)
    fig4

    这里写图片描述

    ?plt.subplots().arrow
    fig5, axes3=plt.subplots()    # default : nrows=1,ncols=1
    Y, X = np.mgrid[-3:3:100j, -3:3:100j] 
    U = -1 - X**2 + Y
    V = 1 + X - Y**2 
    axes3.streamplot(X,Y,U,V)
    fig5

    这里写图片描述

    Data Distributions

    • Plot a histogram
    • Make a box and whisker plot
      • e.g. X~N(1,4)
    • Make a violin plot
    from numpy.random import randn
    fig6, axes4=plt.subplots(nrows=3,ncols=1)
    datay=randn(100)
    axes4[0].hist(datay,bins=20,color='k',alpha=0.3) #Plot a histogram
    axes4[1].boxplot(2*datay+1) #Make a box and whisker plot
    axes4[2].violinplot(datay)
    fig6

    这里写图片描述

  • 相关阅读:
    SpringBoot自动配置原理(七)
    SpringBoot 集成Shiro
    Shiro配置跳过权限验证
    rsync+ssh同步备份文件
    okhttp3设置代理(http/https)
    Spring RestTemplete支持Https安全请求
    电商课题V:分布式锁
    电商课题:对付秒杀器等恶意访问行为的简单梳理
    电商课题:客户端的IP地址伪造、CDN、反向代理、获取的那些事儿
    电商课题I:集群环境下业务限流
  • 原文地址:https://www.cnblogs.com/logmopeng/p/7439898.html
Copyright © 2011-2022 走看看