欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python如何繪制極坐標(biāo)輪廓圖contourf

 更新時間:2023年08月16日 09:34:44   作者:小朱小朱絕不服輸  
這篇文章主要介紹了python如何繪制極坐標(biāo)輪廓圖contourf問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

python繪制極坐標(biāo)輪廓圖contourf

任務(wù):將每個象限的風(fēng)速,距離,角度繪制成極坐標(biāo)輪廓圖。

使用極坐標(biāo)畫等直線圖,可以用極地圖的 ax.contour ax.contourf 。

1.變量計算

每個象限的風(fēng)速,距離就不再說怎么畫了,這里說下角度的計算。

兩個經(jīng)緯度之間的夾角(與正北方向的夾角):

# 點1到點2方向沿逆時針方向轉(zhuǎn)到正北方向的夾角
def LatLng2Degree(LatZero,LngZero,Lat,Lng):
    """
    Args:
        point p1(latA, lonA)
        point p2(latB, lonB)
    Returns:
        bearing between the two GPS points,
        default: the basis of heading direction is north
    """
    radLatA = math.radians(LatZero)
    radLonA = math.radians(LngZero)
    radLatB = math.radians(Lat)
    radLonB = math.radians(Lng)
    dLon = radLonB - radLonA
    y = math.sin(dLon) * math.cos(radLatB)
    x = math.cos(radLatA) * math.sin(radLatB) - math.sin(radLatA) * math.cos(radLatB) * math.cos(dLon)
    brng = math.degrees(math.atan2(y, x))
    brng = (brng + 360) % 360
    return brng

四個象限的距離,風(fēng)速,角度一一對應(yīng)。

2.極坐標(biāo)繪制ax.contourf

import numpy as np
import matplotlib.pyplot as plt
#-- Generate Data -----------------------------------------
# Using linspace so that the endpoint of 360 is included...
azimuths = np.radians(np.linspace(0, 360, 20))
zeniths = np.arange(0, 70, 10)
r, theta = np.meshgrid(zeniths, azimuths)
values = np.random.random((azimuths.size, zeniths.size))
#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)
plt.show()

首先,創(chuàng)建極坐標(biāo)軸,繪制輪廓圖。

fig, ax = subplots(subplot_kw=dict(projection='polar'))
cax = ax.contourf(theta, r, values, nlevels)

參數(shù)分別為theta:角度,r:半徑,values:輪廓的實際值,nlevels:要繪制的輪廓圖的層數(shù)。

這里theta, r, values 都需要是二維數(shù)組,需要通過列表轉(zhuǎn)化為列。

相當(dāng)于需要先構(gòu)造網(wǎng)格矩陣,對于坐標(biāo)軸,一般使用

r, theta = np.meshgrid(zeniths, azimuths)

np.meshgrid函數(shù)去構(gòu)造。

一般情況下,theta, r, values的對應(yīng)關(guān)系是:

可以通過reshape將值轉(zhuǎn)化為二維數(shù)組

這里可以使用:

theta = np.radians(azimuths)
zeniths = np.array(zeniths)
values = np.array(values)
values = values.reshape(len(azimuths), len(zeniths))
r, theta = np.meshgrid(zeniths, np.radians(azimuths))

但當(dāng)theta, r, values一一對應(yīng)時,需要插值實現(xiàn)。

這里記得將角度轉(zhuǎn)化為弧度

r = np.array(r)
v = np.array(v)
angle = np.array(angle)
angle = np.radians(angle)
angle1, r1 = np.meshgrid(angle, r)  # r和angle構(gòu)造網(wǎng)格矩陣
v_new = griddata((angle, r), v, (angle1, r1), method='linear')
cax = ax.contourf(angle1, r1, v_new, 20, cmap='jet')

這里python插值可以參考:python插值(scipy.interpolate模塊的griddata和Rbf)

可以解釋一下使用Rbf插值會報錯的原因,是因為有的方位角相同,比如多行是0度,這樣在使用Rbf插值的時候,要求矩陣可逆,則會出現(xiàn)報錯的情況。

插值完以后,這里還有一個點,就是把0度調(diào)整為正北方向。

ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)

運行結(jié)果:

可以看到四個象限的風(fēng)速就用極坐標(biāo)輪廓圖描述出來了。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論