zoukankan      html  css  js  c++  java
  • matplotlib画散点图和柱状图,等高线图,image图

    一:散点图:

    1. scatter函数原型
       
    2. 其中散点的形状参数marker如下:
       
    3. 其中颜色参数c如下:
       
       
           n = 1024
          # 均值是0, 方差是1, 取1024个数
          x = np.random.normal(0, 1, n)
          y = np.random.normal(0, 1, n)
          # 设置颜色值
          T = np.arctan2(y, x)
          bar = plt.scatter(x, y, s=10, c=T, alpha=0.5, cmap='hot')
          # plt.xticks(())
          # plt.yticks(())
          plt.colorbar(bar)
          plt.savefig('./image_dir/scatter.png')
          plt.show()

      二  柱状图

       n = 12
          x = np.arange(n)
          print(x)
          # np.random.uniform(0.5, 1.0, n)  去、取0.5 到 1 之间12个数
          y1 = (1-x/float(n)) * np.random.uniform(0.5, 1.0, n)
          y2 = (1 - x / float(n)) * np.random.uniform(0.5, 1.0, n)
          plt.bar(x, +y1, facecolor='#9999ff', edgecolor='white')
          plt.bar(x, -y2, facecolor='#ff9999', edgecolor='white')
          plt.xticks(())
          plt.yticks(())
          for x, y, y2 in zip(x, y1, y2):
              # 给每根柱子加上标识
              plt.text(x, y+0.05, '%.2f'%y, ha='center', va='bottom')
              plt.text(x, -y2 - 0.05, f'{round(y2, 2)}', ha='center', va='top')
      
          plt.savefig('./image_dir/bar.png')
          plt.show()

      三:等高线图

          def f(x, y):
              return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
          n = 256
          x = np.linspace(-3, 3, n)
          y = np.linspace(-3, 3, n)
          '''
          meshgrid函数就是用两个坐标轴上的点在平面上画网格(当然这里传入的参数是两个的时候)。
          当然我们可以指定多个参数,比如三个参数,
          那么我们的就可以用三个一维的坐标轴上的点在三维平面上画网格。
          '''
          X, Y = np.meshgrid(x, y)
          # use plt.contourf to filling contours
          # X, Y and value for (X, Y)point
          plt.contourf(X, Y, f(X, Y), 8, alpha=0.75, cmap='hot')
          # plt.xticks(())
          # plt.yticks(())
      
          # use plt.contour to add contour lines   8表示分成10份, 0分成2份
          C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=.5)
          # adding label
          plt.clabel(C, inline=True, fontsize=10)
          plt.savefig('./image_dir/contourf.png')
          plt.show()

      四 image图像:

       a = np.random.rand(9).reshape(3, 3)
      
          plt.imshow(a, interpolation='nearest', cmap='bone', origin='upper')
          plt.colorbar()
          plt.xticks(())
          plt.yticks(())
          plt.savefig('./image_dir/imshow.png')
          plt.show()

  • 相关阅读:
    2018-2-13-安装-aria2
    ..USERstm32f10x.h(428): error: #67: expected a "}" ADC1_2_IRQn = 18, /*!
    zubax_gnss移植到STM32F407
    ChibiOS/RT移植到STM32F407
    arm-none-eabi/bin/ld: build/com.zubax.gnss.elf section `.text' will not fit in region `flash'
    Traceback (most recent call last): File "../zubax_chibios/tools/make_boot_descriptor.py", line 251
    Eclipse 交叉编译环境
    PX4/Pixhawk uORB
    FreeRTOS 任务创建和删除(静态)
    FreeRTOS 任务创建和删除(动态)
  • 原文地址:https://www.cnblogs.com/heguihui/p/12182677.html
Copyright © 2011-2022 走看看