• 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')

     

  • 相关阅读:
    什么是指标体系,怎么搭建一套完整的指标体系?(附PDF素材)
    【web知识清单】你想要的都有:网络、HTTP、会话保持、认证授权......持续更新中
    6.jQuery中的Ajax上传文件
    java毕业设计心理咨询管理系统mybatis+源码+调试部署+系统+数据库+lw
    【Redis】Hash 哈希内部编码方式
    Tomcat和HPPT协议
    第12章 - 执行引擎
    2023-2028年中国硫酸铝钾市场发展态势及未来发展趋势报告
    【数据库原理与应用】数据库应用实例— 教学管理系统
    专业软件测评中心:关于软件性能测试的实用建议
  • 原文地址:https://blog.csdn.net/qq_40206371/article/details/125530475