21.等值线图(Counter Plot)、Contour Demo、Creating a “meshgrid”、Calculation of the Values、等
21.等值線圖(Counter Plot)
21.1.Contour Demo
21.2.Creating a “meshgrid”
21.3.Calculation of the Values
21.4.Changing the Colours and the Line Style
21.5.Filled Contours
21.6.Individual Colours
21.等值線圖(Counter Plot)
兩個變量函數(shù)的等值線(或等高線)是函數(shù)具有常數(shù)值的曲線。它是平行于x,y平面的函數(shù)f(x,y)的三維圖的橫截面。
例如在地理和氣象學使用登高線。在制圖中,等高線連接高于給定水平的相等高度的點,例如平均海平面。
我們還可以更一般地說,具有兩個變量的函數(shù)的等值線是連接具有相同值的點的曲線。
contour和contourf都是畫三維等高線圖的,不同點在于contour() 是繪制輪廓線,contourf()會填充輪廓。
21.1.Contour Demo
舉例說明簡單的等值線繪圖: 圖像上的等值線, 以及用于等值線的色條(colorbar)以及標記的等高線。
import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.pyplot as pltdelta = 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使用默認顏色創(chuàng)建帶有標簽的簡單等值線圖。 clabel的inline參數(shù)將控制是否在等值線的線段上繪制標簽,從而去除標簽下方的線。
fig, ax = plt.subplots() CS = ax.contour(X, Y, Z) ax.clabel(CS, inline=1, fontsize=10) ax.set_title('Simplest default with labels')即:
import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.pyplot as pltdelta = 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) * 2fig, ax = plt.subplots() CS = ax.contour(X, Y, Z) ax.clabel(CS, inline=1, fontsize=10) ax.set_title('Simplest default with labels')plt.show()通過提供的數(shù)據(jù)坐標中的位置列表可以手動放置等值線標簽。
fig, ax = plt.subplots() CS = ax.contour(X, Y, Z) manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)] ax.clabel(CS, inline=1, fontsize=10, manual=manual_locations) ax.set_title('labels at selected locations')
你可以強制所有等值線為相同顏色。
可以將負等值線設(shè)置為實線而不是虛線:
可以手動指定等值線的顏色:
import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.pyplot as pltdelta = 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) * 2matplotlib.rcParams['contour.negative_linestyle'] = 'solid' fig, ax = plt.subplots() CS = ax.contour(X, Y, Z, 6,linewidths=np.arange(.5, 4, .5),colors=('r', 'green', 'blue', (1, 1, 0), '#afeeee', '0.5'), # negative contours will be dashed by default) ax.clabel(CS, fontsize=9, inline=1) ax.set_title('Single color - negative contours solid')plt.show()可以使用顏色圖(colormap)來指定顏色。 默認的顏色圖(colormap)將用于等值線。
import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.pyplot as pltdelta = 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) * 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',linewidths=2, extent=(-3, 3, -2, 2))# Thicken the zero contour. zc = CS.collections[6] plt.setp(zc, linewidth=4)ax.clabel(CS, levels[1::2], # label every second levelinline=1, fmt='%1.1f', fontsize=14)# make a colorbar for the contour lines CB = fig.colorbar(CS, shrink=0.8, extend='both')ax.set_title('Lines with colorbar')# We can still add a colorbar for the image, too. CBI = fig.colorbar(im, orientation='horizontal', shrink=0.8)# This makes the original colorbar look a bit out of place, # so let's improve its position.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()21.2.Creating a “meshgrid”
import numpy as npxlist = np.linspace(-3.0, 3.0, 3) ylist = np.linspace(-3.0, 3.0, 4) X, Y = np.meshgrid(xlist, ylist) print(xlist) print(ylist) print(X) print(Y) """ 輸出結(jié)果: [-3. 0. 3.] [-3. -1. 1. 3.] [[-3. 0. 3.][-3. 0. 3.][-3. 0. 3.][-3. 0. 3.]] [[-3. -3. -3.][-1. -1. -1.][ 1. 1. 1.][ 3. 3. 3.]] """21.3.Calculation of the Values
import numpy as np import matplotlib.pyplot as pltxlist = np.linspace(-3.0, 3.0, 3) ylist = np.linspace(-3.0, 3.0, 4) X, Y = np.meshgrid(xlist, ylist) Z = np.sqrt(X**2 + Y**2) print(Z) """ 輸出結(jié)果: [[4.24264069 3. 4.24264069][3.16227766 1. 3.16227766][3.16227766 1. 3.16227766][4.24264069 3. 4.24264069]] """plt.figure() cp = plt.contour(X, Y, Z) plt.clabel(cp, inline=True,fontsize=10) plt.title('Contour Plot') plt.xlabel('x (cm)') plt.ylabel('y (cm)') plt.show()21.4.Changing the Colours and the Line Style
import numpy as np import matplotlib.pyplot as pltxlist = np.linspace(-3.0, 3.0, 3) ylist = np.linspace(-3.0, 3.0, 4) X, Y = np.meshgrid(xlist, ylist) Z = np.sqrt(X**2 + Y**2) print(Z) """ 輸出結(jié)果: [[4.24264069 3. 4.24264069][3.16227766 1. 3.16227766][3.16227766 1. 3.16227766][4.24264069 3. 4.24264069]] """plt.figure() cp = plt.contour(X, Y, Z, colors='black', linestyles='dashed') plt.clabel(cp, inline=True,fontsize=10) plt.title('Contour Plot') plt.xlabel('x (cm)') plt.ylabel('y (cm)') plt.show()21.5.Filled Contours
import numpy as np import matplotlib.pyplot as plt xlist = np.linspace(-3.0, 3.0, 100) ylist = np.linspace(-3.0, 3.0, 100) X, Y = np.meshgrid(xlist, ylist) Z = np.sqrt(X**2 + Y**2) plt.figure() cp = plt.contourf(X, Y, Z) plt.colorbar(cp) plt.title('Filled Contours Plot') plt.xlabel('x (cm)') plt.ylabel('y (cm)') plt.show()21.6.Individual Colours
import numpy as np import matplotlib.pyplot as plt xlist = np.linspace(-3.0, 3.0, 100) ylist = np.linspace(-3.0, 3.0, 100) X, Y = np.meshgrid(xlist, ylist) Z = np.sqrt(X**2 + Y**2) plt.figure() contour = plt.contourf(X, Y, Z) plt.clabel(contour, colors = 'k', fmt = '%2.1f', fontsize=12) c = ('#ff0000', '#ffff00', '#0000FF', '0.6', 'c', 'm') contour_filled = plt.contourf(X, Y, Z, colors=c) plt.colorbar(contour) plt.title('Filled Contours Plot') plt.xlabel('x (cm)') plt.ylabel('y (cm)') plt.show()21.7.Levels
到目前為止,levels是由contour和contourf自動確定的。 通過提供一個levels列表作為第四個參數(shù),可以手動定義它們。 如果使用等值線,則會為列表中的每個值繪制等值線。 對于contourf,列表中的值之間將填充彩色區(qū)域。
import numpy as np import matplotlib.pyplot as plt xlist = np.linspace(-3.0, 3.0, 100) ylist = np.linspace(-3.0, 3.0, 100) X, Y = np.meshgrid(xlist, ylist) Z = np.sqrt(X ** 2 + Y ** 2 ) plt.figure() levels = [0.0, 0.2, 0.5, 0.9, 1.5, 2.5, 3.5] contour = plt.contour(X, Y, Z, levels, colors='k') plt.clabel(contour, colors = 'k', fmt = '%2.1f', fontsize=12) contour_filled = plt.contourf(X, Y, Z, levels) plt.colorbar(contour_filled) plt.title('Plot from level list') plt.xlabel('x (cm)') plt.ylabel('y (cm)') plt.show()總結(jié)
以上是生活随笔為你收集整理的21.等值线图(Counter Plot)、Contour Demo、Creating a “meshgrid”、Calculation of the Values、等的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么挑选电动车?
- 下一篇: 26.27.28.29.极区图(南丁格尔