python如何繪制極坐標輪廓圖contourf
python繪制極坐標輪廓圖contourf
任務:將每個象限的風速,距離,角度繪制成極坐標輪廓圖。
使用極坐標畫等直線圖,可以用極地圖的 ax.contour 或 ax.contourf 。
1.變量計算
每個象限的風速,距離就不再說怎么畫了,這里說下角度的計算。
兩個經(jīng)緯度之間的夾角(與正北方向的夾角):

# 點1到點2方向沿逆時針方向轉到正北方向的夾角
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四個象限的距離,風速,角度一一對應。
2.極坐標繪制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)建極坐標軸,繪制輪廓圖。
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ù)組,需要通過列表轉化為列。
相當于需要先構造網(wǎng)格矩陣,對于坐標軸,一般使用
r, theta = np.meshgrid(zeniths, azimuths)
np.meshgrid函數(shù)去構造。
一般情況下,theta, r, values的對應關系是:

可以通過reshape將值轉化為二維數(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))
但當theta, r, values一一對應時,需要插值實現(xià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構造網(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度調整為正北方向。
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)運行結果:

可以看到四個象限的風速就用極坐標輪廓圖描述出來了。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
如何使用Python的Requests包實現(xiàn)模擬登陸
這篇文章主要為大家詳細介紹了使用Python的Requests包模擬登陸,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
python讀取TXT到數(shù)組及列表去重后按原來順序排序的方法
這篇文章主要介紹了python讀取TXT到數(shù)組及列表去重后按原來順序排序的方法,涉及Python操作txt文件、列表去重及排序的相關技巧,需要的朋友可以參考下2015-06-06
Python使用pytest-playwright的原因分析
pytest-playwright 是一個 Python 包,它允許您使用 Microsoft 的 Playwright 庫在 Python 項目中進行端到端測試,這篇文章主要介紹了Python為什么使用pytest-playwright,需要的朋友可以參考下2023-03-03
Python/MySQL實現(xiàn)Excel文件自動處理數(shù)據(jù)功能
在沒有服務器存儲數(shù)據(jù),只有excel文件的情況下,如何利用SQL和python實現(xiàn)數(shù)據(jù)分析和數(shù)據(jù)自動處理的功能?本文就來和大家聊聊解決辦法2023-02-02
PyCharm中鼠標懸停在函數(shù)上時顯示函數(shù)和幫助的解決方法
這篇文章主要介紹了PyCharm中鼠標懸停在函數(shù)上時顯示函數(shù)和幫助,本文給大家分享問題解決方法,對PyCharm鼠標懸停函數(shù)上顯示函數(shù)的解決方法感興趣的朋友跟隨小編一起看看吧2022-11-11
python不到50行代碼完成了多張excel合并的實現(xiàn)示例
這篇文章主要介紹了python不到50行代碼完成了多張excel合并的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05

