• matplotlib 笔记: contourf & contour


    contour 和 contourf 分别绘制等高线和填充等高线

    1 基本使用方法

    matplotlib.pyplot.contourf([X, Y,] Z, [levels], **kwargs)

    2 主要参数 

    X, Y

    Z 中的值的坐标。

    X 和 Y 必须都是二维的,具有与 Z 相同的形状(例如,通过 numpy.meshgrid 创建),或者它们必须都是一维的,这样 len(X) == N ,len(Y ) == M 是 Z 中的行数和列数。

    X 和 Y 都必须单调排序。

    z绘制等高线的高度值。
    levels

    确定等高线/区域的数量和位置。

    如果是整数n,请使用 MaxNLocator,它会尝试在 vmin 和 vmax 之间自动选择不超过 n+1 个”好“的等高级别。

    如果是数组,则在指定级别绘制等高线。 这些值必须按升序排列。

    colors

    级别的颜色,即等高线轮廓的线条和等高线轮廓的区域。

    该序列按升序循环用于各个级别。 如果序列比级别数短,则重复。

    作为一种快捷方式,可以使用单一颜色字符串代替单元素列表,即“red”而不是 [“red”] 以用相同的颜色为所有级别着色。

    默认情况下(值 None),将使用 cmap 指定的颜色图。

    cmap
    alpha透明度,0~1之间的数
    extend

    确定级别范围之外的值的轮廓着色。

    如果“both”,则级别范围之外的值不着色。

    如果是“min”、“max”或“both”,则为低于、高于或低于和高于水平范围的值着色。 低于 min(levels) 和高于 max(levels) 的值被映射到颜色图的低于/高于值。 

     3 举例

    3.0 数据部分

    1. import numpy as np
    2. import matplotlib.pyplot as plt
    3. x = np.arange(1, 10)
    4. y = x.reshape(-1, 1)
    5. h = x * y
    6. print(x,'\n',y,'\n',h)
    7. '''
    8. [1 2 3 4 5 6 7 8 9]
    9. [[1]
    10. [2]
    11. [3]
    12. [4]
    13. [5]
    14. [6]
    15. [7]
    16. [8]
    17. [9]]
    18. [[ 1 2 3 4 5 6 7 8 9]
    19. [ 2 4 6 8 10 12 14 16 18]
    20. [ 3 6 9 12 15 18 21 24 27]
    21. [ 4 8 12 16 20 24 28 32 36]
    22. [ 5 10 15 20 25 30 35 40 45]
    23. [ 6 12 18 24 30 36 42 48 54]
    24. [ 7 14 21 28 35 42 49 56 63]
    25. [ 8 16 24 32 40 48 56 64 72]
    26. [ 9 18 27 36 45 54 63 72 81]]
    27. '''

    3.1 最基本

    1. plt.contourf(x.reshape(-1),
    2. y.reshape(-1),
    3. h)

    3.2 level 

    1. plt.contourf(x.reshape(-1),
    2. y.reshape(-1),
    3. h,
    4. levels=1)

    1. plt.contourf(x.reshape(-1),
    2. y.reshape(-1),
    3. h,
    4. levels=[1,3,15,35])

     3.3 extend

    1. plt.contourf(x.reshape(-1),
    2. y.reshape(-1),
    3. h,
    4. levels=[13,15,35],
    5. )

     

     

    1. plt.contourf(x.reshape(-1),
    2. y.reshape(-1),
    3. h,
    4. levels=[13,15,35],
    5. extend='both')

     

  • 相关阅读:
    jupyter中安装scala和spark内核详细教程
    普洱茶上市?澜沧古茶通过港股聆讯
    一段程序的穷途一生
    Java项目:汽车商城管理系统(java+SpringBoot+VUE+Maven+Mysql)
    RocketMQ消息生产者是如何选择Broker的
    中班防暴力安全教案
    kubernetes—Service介绍
    FPGA设计时序约束一、主时钟与生成时钟
    Python开发工具PyCharm全新版本V2022.2正式发布——支持 Python 3.11
    ZooKeeper 概述
  • 原文地址:https://blog.csdn.net/qq_40206371/article/details/125530475