这篇文章主要介绍“怎么利用python绘制等高线图”,在日常操作中,相信很多人在怎么利用python绘制等高线图问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么利用python绘制等高线图”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
matplotlib.pyplot.contour(*args, data=None, **kwargs)
参数介绍:
参数X,YZ(M,N)类数组level
import numpy as np import matplotlib.pyplot as plt X, 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()
需要住的是inline参数.默认是inline=True
import numpy as np import matplotlib.cm as cm import matplotlib.pyplot as plt delta = 0.025 x = 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) * 2 fig, 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 np import matplotlib.cm as cm import matplotlib.pyplot as plt delta = 0.025 x = 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) * 2 fig, 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()
确定等高线数量/位置,选择不超过n+1个"良好"轮廓级别
import numpy as np import matplotlib.cm as cm import matplotlib.pyplot as plt delta = 0.025 x = 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) * 2 fig, 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 np import matplotlib.cm as cm import matplotlib.pyplot as plt delta = 0.025 x = 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) * 2 fig, 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 np import matplotlib.cm as cm import matplotlib.pyplot as plt delta = 0.025 x = 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) * 2 fig, 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 level inline=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().bounds ll, bb, ww, hh = CB.ax.get_position().bounds CB.ax.set_position([ll, b + 0.1*h, ww, h*0.8]) plt.show()
到此,关于“怎么利用python绘制等高线图”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。