经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
如何利用python绘制等高线图
来源:jb51  时间:2022/6/20 8:40:28  对本文有异议

使用方法

  1. matplotlib.pyplot.contour(*args, data=None, **kwargs)

参数介绍:

参数X,YZ(M,N)类数组level

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. X, Y = np.meshgrid(np.linspace(-3,3,256), np.linspace(-3,3,256))
  5. Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
  6. levels = np.linspace(np.min(Z), np.max(Z), 7)
  7. fig, ax = plt.subplots()
  8. ax.contour(X, Y, Z, levels=levels)
  9. plt.show()

添加label的

需要住的是inline参数.默认是inline=True

  1. import numpy as np
  2. import matplotlib.cm as cm
  3. import matplotlib.pyplot as plt
  4.  
  5. delta = 0.025
  6. x = np.arange(-3.0, 3.0, delta)
  7. y = np.arange(-2.0, 2.0, delta)
  8. X, Y = np.meshgrid(x, y)
  9. Z1 = np.exp(-X**2 - Y**2)
  10. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  11. Z = (Z1 - Z2) * 2
  12. fig, ax = plt.subplots()
  13. CS = ax.contour(X, Y, Z)
  14. ax.clabel(CS, inline=True, fontsize=10)
  15. ax.set_title('Simplest default with labels')
  16. plt.show()

绘制虚线

  1. import numpy as np
  2. import matplotlib.cm as cm
  3. import matplotlib.pyplot as plt
  4.  
  5. delta = 0.025
  6. x = np.arange(-3.0, 3.0, delta)
  7. y = np.arange(-2.0, 2.0, delta)
  8. X, Y = np.meshgrid(x, y)
  9. Z1 = np.exp(-X**2 - Y**2)
  10. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  11. Z = (Z1 - Z2) * 2
  12. fig, ax = plt.subplots()
  13. CS = ax.contour(X, Y, Z, 6, colors='k')
  14. ax.clabel(CS, fontsize=9, inline=True)
  15. ax.set_title('Single color - negative contours dashed')
  16. plt.show()

level

确定等高线数量/位置,选择不超过n+1个"良好"轮廓级别

  1. import numpy as np
  2. import matplotlib.cm as cm
  3. import matplotlib.pyplot as plt
  4.  
  5. delta = 0.025
  6. x = np.arange(-3.0, 3.0, delta)
  7. y = np.arange(-2.0, 2.0, delta)
  8. X, Y = np.meshgrid(x, y)
  9. Z1 = np.exp(-X**2 - Y**2)
  10. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  11. Z = (Z1 - Z2) * 2
  12. fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(14,7))
  13. axs[0].set_title('levels=6')
  14. CS = axs[0].contour(X, Y, Z, 6, colors='k')
  15. axs[0].clabel(CS, fontsize=9, inline=True)
  16.  
  17. axs[1].set_title('levels=10')
  18. CS1 = axs[1].contour(X, Y, Z, 10, colors='k')
  19. axs[1].clabel(CS1, fontsize=9, inline=True)
  20. plt.show()

设置颜色和线条宽度

  1. import numpy as np
  2. import matplotlib.cm as cm
  3. import matplotlib.pyplot as plt
  4.  
  5. delta = 0.025
  6. x = np.arange(-3.0, 3.0, delta)
  7. y = np.arange(-2.0, 2.0, delta)
  8. X, Y = np.meshgrid(x, y)
  9. Z1 = np.exp(-X**2 - Y**2)
  10. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  11. Z = (Z1 - Z2) * 2
  12.  
  13. fig, ax = plt.subplots()
  14. CS = ax.contour(X, Y, Z, 6,
  15. linewidths=np.arange(.5, 4, .5),
  16. colors=('r', 'cyan', 'blue', (1, 1, 0), '#afeeee', '0.5'),
  17. )
  18. ax.clabel(CS, fontsize=9, inline=True)
  19. ax.set_title('Crazy lines')
  20. plt.show()

其他设置

  1. import numpy as np
  2. import matplotlib.cm as cm
  3. import matplotlib.pyplot as plt
  4.  
  5. delta = 0.025
  6. x = np.arange(-3.0, 3.0, delta)
  7. y = np.arange(-2.0, 2.0, delta)
  8. X, Y = np.meshgrid(x, y)
  9. Z1 = np.exp(-X**2 - Y**2)
  10. Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
  11. Z = (Z1 - Z2) * 2
  12. fig, ax = plt.subplots()
  13. im = ax.imshow(Z, interpolation='bilinear', origin='lower',
  14. cmap=cm.gray, extent=(-3, 3, -2, 2))
  15. levels = np.arange(-1.2, 1.6, 0.2)
  16. CS = ax.contour(Z, levels, origin='lower', cmap='flag', extend='both',
  17. linewidths=2, extent=(-3, 3, -2, 2))
  18. CS.collections[6].set_linewidth(4)
  19. ax.clabel(CS, levels[1::2], # label every second level
  20. inline=True, fmt='%1.1f', fontsize=14)
  21. CB = fig.colorbar(CS, shrink=0.8)
  22. ax.set_title('Lines with colorbar')
  23. CBI = fig.colorbar(im, orientation='horizontal', shrink=0.8)
  24. l, b, w, h = ax.get_position().bounds
  25. ll, bb, ww, hh = CB.ax.get_position().bounds
  26. CB.ax.set_position([ll, b + 0.1*h, ww, h*0.8])
  27. plt.show()

到此这篇关于如何利用python绘制等高线图的文章就介绍到这了,更多相关python绘制等高线图内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号