如何利用python绘制等高线图
目录
- 使用方法
- 添加label的
- 绘制虚线
- level
- 设置颜色和线条宽度
- 其他设置
使用方法
matplotlib.pyplot.contour(*args, data=https://www.it610.com/article/None, **kwargs)
参数介绍:
文章图片
参数X,YZ(M,N)类数组level
import numpy as npimport matplotlib.pyplot as pltX, Y = np.meshgrid(np.linspace(-3,3,256), np.linspace(-3,3,256))Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)levels = np.linspace(np.min(Z), np.max(Z), 7)fig, ax = plt.subplots()ax.contour(X, Y, Z, levels=levels)plt.show()
文章图片
添加label的 需要住的是inline参数.默认是inline=True
import numpy as npimport matplotlib.cm as cmimport matplotlib.pyplot as pltdelta = 0.025x = np.arange(-3.0, 3.0, delta)y = np.arange(-2.0, 2.0, delta)X, Y = np.meshgrid(x, y)Z1 = np.exp(-X**2 - Y**2)Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)Z = (Z1 - Z2) * 2fig, ax = plt.subplots()CS = ax.contour(X, Y, Z)ax.clabel(CS, inline=True, fontsize=10)ax.set_title('Simplest default with labels')plt.show()
文章图片
文章图片
绘制虚线
import numpy as npimport matplotlib.cm as cmimport matplotlib.pyplot as pltdelta = 0.025x = np.arange(-3.0, 3.0, delta)y = np.arange(-2.0, 2.0, delta)X, Y = np.meshgrid(x, y)Z1 = np.exp(-X**2 - Y**2)Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)Z = (Z1 - Z2) * 2fig, ax = plt.subplots()CS = ax.contour(X, Y, Z, 6, colors='k')ax.clabel(CS, fontsize=9, inline=True)ax.set_title('Single color - negative contours dashed')plt.show()
【如何利用python绘制等高线图】
文章图片
level 确定等高线数量/位置,选择不超过n+1个"良好"轮廓级别
import numpy as npimport matplotlib.cm as cmimport matplotlib.pyplot as pltdelta = 0.025x = np.arange(-3.0, 3.0, delta)y = np.arange(-2.0, 2.0, delta)X, Y = np.meshgrid(x, y)Z1 = np.exp(-X**2 - Y**2)Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)Z = (Z1 - Z2) * 2fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(14,7))axs[0].set_title('levels=6')CS = axs[0].contour(X, Y, Z, 6, colors='k')axs[0].clabel(CS, fontsize=9, inline=True)axs[1].set_title('levels=10')CS1 = axs[1].contour(X, Y, Z, 10, colors='k')axs[1].clabel(CS1, fontsize=9, inline=True)plt.show()
文章图片
设置颜色和线条宽度
import numpy as npimport matplotlib.cm as cmimport matplotlib.pyplot as pltdelta = 0.025x = np.arange(-3.0, 3.0, delta)y = np.arange(-2.0, 2.0, delta)X, Y = np.meshgrid(x, y)Z1 = np.exp(-X**2 - Y**2)Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)Z = (Z1 - Z2) * 2fig, ax = plt.subplots()CS = ax.contour(X, Y, Z, 6,linewidths=np.arange(.5, 4, .5),colors=('r', 'cyan', 'blue', (1, 1, 0), '#afeeee', '0.5'),)ax.clabel(CS, fontsize=9, inline=True)ax.set_title('Crazy lines')plt.show()
文章图片
其他设置
import numpy as npimport matplotlib.cm as cmimport matplotlib.pyplot as pltdelta = 0.025x = np.arange(-3.0, 3.0, delta)y = np.arange(-2.0, 2.0, delta)X, Y = np.meshgrid(x, y)Z1 = np.exp(-X**2 - Y**2)Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)Z = (Z1 - Z2) * 2fig, ax = plt.subplots()im = ax.imshow(Z, interpolation='bilinear', origin='lower',cmap=cm.gray, extent=(-3, 3, -2, 2))levels = np.arange(-1.2, 1.6, 0.2)CS = ax.contour(Z, levels, origin='lower', cmap='flag', extend='both',linewidths=2, extent=(-3, 3, -2, 2))CS.collections[6].set_linewidth(4)ax.clabel(CS, levels[1::2],# label every second levelinline=True, fmt='%1.1f', fontsize=14)CB = fig.colorbar(CS, shrink=0.8)ax.set_title('Lines with colorbar')CBI = fig.colorbar(im, orientation='horizontal', shrink=0.8)l, b, w, h = ax.get_position().boundsll, bb, ww, hh = CB.ax.get_position().boundsCB.ax.set_position([ll, b + 0.1*h, ww, h*0.8])plt.show()
文章图片
到此这篇关于如何利用python绘制等高线图的文章就介绍到这了,更多相关python绘制等高线图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- python使用prettytable内置库美化输出表格
- python实现对excel中需要的数据的单元格填充颜色
- python|python groupby函数实现分组后选取最值
- python面积图之曲线图的填充
- 如何优雅的使用MyBatis()
- 结合项目带你了解,Vue组件重复利用,及父组件传递数据给子组件
- 中秋将至利用python画一些月饼从天而降不用买了
- Python中函数的创建及调用
- python中@Property属性使用方法
- Python类的定义和使用详情