python中28種極坐標(biāo)繪圖函數(shù)總結(jié)
參考
python35種繪圖函數(shù)總結(jié),3D、統(tǒng)計(jì)、流場(chǎng),實(shí)用性拉滿(mǎn)
matplotlib中的畫(huà)圖函數(shù),大部分情況下只要聲明坐標(biāo)映射是polar,就都可以畫(huà)出對(duì)應(yīng)的極坐標(biāo)圖。但極坐標(biāo)和直角坐標(biāo)的坐標(biāo)區(qū)間不同,所以有些數(shù)據(jù)和函數(shù)關(guān)系適合在直角坐標(biāo)系中展示,而有些則適合在及坐標(biāo)中展示。
基礎(chǔ)圖
| 函數(shù) | 坐標(biāo)參數(shù) | 圖形類(lèi)別 |
|---|---|---|
| plot | x,y | 曲線(xiàn)圖 |
| stackplot | x,y | 散點(diǎn)圖 |
| stem | x,y | 莖葉圖 |
| scatter | x,y | 散點(diǎn)圖 |
| polar | x,y | 極坐標(biāo)圖 |
| step | x,y | 步階圖 |
| bar | x,y | 條形圖 |
| barh | x,y | 橫向條形圖 |

bar和barh的對(duì)偶關(guān)系稍微有些抽象,可以理解為前者是以角度方向?yàn)閤軸;而barh則是以半徑方向?yàn)閤軸。
代碼如下
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(20)/2
y = x
fDct = {"plot" : plt.plot, "stackplot": plt.stackplot,
"stem" : plt.stem, "scatter" : plt.scatter,
"polar": plt.polar, "step" : plt.step,
"bar" : plt.bar, "barh" : plt.barh, }
fig = plt.figure(figsize=(14,6))
for i,key in enumerate(fDct, 1):
ax = fig.add_subplot(2,4,i, projection="polar")
fDct[key](x, y)
plt.title(key)
plt.tight_layout()
plt.show()誤差線(xiàn)
| 函數(shù) | 坐標(biāo) | 圖形類(lèi)別 |
|---|---|---|
| errorbar | x,y,xerr,yerr | 誤差線(xiàn) |
| fill_between | x,y1,y2 | 縱向區(qū)間圖 |
| fill_betweenx | y, x1, x2 | 橫向區(qū)間圖 |

代碼如下
x = np.arange(20)/2
y = x
y1, y2 = 0.9*y, 1.1*y
x1, x2 = 0.9*x, 1.1*x
xerr = np.abs([x1, x2])/10
yerr = np.abs([y1, y2])/10
fig = plt.figure(figsize=(12,4))
ax = fig.add_subplot(141, projection='polar')
ax.errorbar(x, y, yerr=yerr)
plt.title("errorbar with yerr")
ax = fig.add_subplot(142, projection='polar')
ax.errorbar(x, y, xerr=xerr)
plt.title("errorbar with xerr")
ax = fig.add_subplot(143, projection='polar')
ax.fill_between(x, y1, y2)
plt.title("fill_between")
ax = fig.add_subplot(144, projection='polar')
ax.fill_betweenx(y, x1, x2)
plt.title("fill_betweenx")
plt.tight_layout()
plt.show()等高線(xiàn)polar
| 繪圖函數(shù) | 坐標(biāo) | 說(shuō)明 |
|---|---|---|
| contour | [x,y,]z | 等高線(xiàn) |
| contourf | [x,y,]z | 填充等高線(xiàn) |
| pcolormesh | [x,y,]z | 偽彩圖 |
由于imshow默認(rèn)其繪圖坐標(biāo)是標(biāo)準(zhǔn)的1x1網(wǎng)格,而在極坐標(biāo)種,這種網(wǎng)格的尺寸會(huì)隨著r的增大而增大,從而變得極其不實(shí)用,所以下面對(duì)極坐標(biāo)圖的演示,就不包含imshow了。

代碼如下
X, Y = np.indices([100,100])
X = X/100*np.pi*2
Y = Y/25 - 2
Z = (1 - np.sin(X) + np.cos(X)**5 + Y**3) * np.exp(-Y**2)
fDct = {"contour": plt.contour, "contourf":plt.contourf,
"pcolormesh" : plt.pcolormesh}
fig = plt.figure(figsize=(9,3))
for i,key in enumerate(fDct, 1):
ax = fig.add_subplot(1,3,i, projection='polar')
fDct[key](X,Y,Z)
plt.title(key)
plt.tight_layout()
plt.show()場(chǎng)圖polar
| 繪圖函數(shù) | 坐標(biāo) | 說(shuō)明 |
|---|---|---|
| quiver | x,y,u,v | 向量場(chǎng)圖 |
| streamplot | x,y,u,v | 流場(chǎng)圖 |
| barbs | x,y,u,v | 風(fēng)場(chǎng)圖 |

代碼如下
Y, X = np.indices([10,10])
X = X/10*np.pi*2.5
Y = Y
#Y, X = np.indices([6,6])/0.75 - 4
U = 6*np.sin(X) + Y
V = Y - 6*np.sin(X)
dct = {"quiver":plt.quiver, "streamplot":plt.streamplot,
"barbs" :plt.barbs}
fig = plt.figure(figsize=(12,4))
for i,key in enumerate(dct, 1):
ax = fig.add_subplot(1,3,i,projection='polar')
dct[key](X,Y,U,V)
plt.title(key)
plt.tight_layout()
plt.show()統(tǒng)計(jì)圖
| 繪圖函數(shù) | 坐標(biāo) | 說(shuō)明 |
|---|---|---|
| hist | x | 數(shù)據(jù)直方圖 |
| boxplot | x | 箱線(xiàn)圖 |
| violinplot | x | 小提琴圖 |
| enventplot | x | 平行線(xiàn)疏密圖 |
| hist2d | x,y | 二維直方圖 |
| hexbin | x,y | 鉆石圖 |
| pie | x | 餅圖 |
極坐標(biāo)在繪制直方圖的時(shí)候,需要注意其橫坐標(biāo)是以2π為周期的,也就是說(shuō)隨機(jī)變量的最大值和最小值不得相差2π,否則會(huì)導(dǎo)致重疊。

由于極坐標(biāo)繪圖本質(zhì)上是一種坐標(biāo)映射,所以并不會(huì)把0和360°真正地等同起來(lái),所以在hist2d中,整個(gè)圖像并沒(méi)有閉合。而最有意思的是餅圖,直接給壓扁了,讓人很難一下子看出不同組分的比例關(guān)系。
代碼如下
x = np.random.standard_normal(size=1000)
dct = {"hist" : plt.hist, "violinplot" : plt.violinplot,
"boxplot": plt.boxplot}
fig = plt.figure(figsize=(10,6))
for i,key in enumerate(dct, 1):
ax = fig.add_subplot(2,3,i, projection='polar')
dct[key](x)
plt.title(key)
ax = fig.add_subplot(234, projection='polar')
ax.eventplot(x)
plt.title("eventplot")
x = np.random.randn(5000)
y = 1.2 * x + np.random.randn(5000) / 3
ax = fig.add_subplot(235, projection='polar')
ax.hist2d(x, y, bins=[np.arange(-3,3,0.1)] * 2)
plt.title("hist2d")
ax = fig.add_subplot(236, projection='polar')
ax.pie([1,2,3,4,5])
plt.title("pie")
plt.tight_layout()
plt.show()非結(jié)構(gòu)坐標(biāo)圖
| 繪圖函數(shù) | 坐標(biāo) | 說(shuō)明 |
|---|---|---|
| tricontour | x,y,z | 非結(jié)構(gòu)等高線(xiàn) |
| tricontourf | x,y,z | 非結(jié)構(gòu)化填充等高線(xiàn) |
| tricolor | x,y,z | 非結(jié)構(gòu)化偽彩圖 |
| triplot | x,y | 三角連線(xiàn)圖 |

代碼如下
x = np.random.uniform(0, np.pi*2, 256)
y = np.random.uniform(-2, 2, 256)
z = (1 - np.sin(x) + np.cos(x)**5 + y**3) * np.exp(-y**2)
levels = np.linspace(z.min(), z.max(), 7)
fig = plt.figure(figsize=(12,4))
ax = fig.add_subplot(141, projection='polar')
ax.plot(x, y, 'o', markersize=1, color='lightgrey', alpha=0.5)
ax.tricontour(x, y, z, levels=levels)
plt.title("tricontour")
ax = fig.add_subplot(142, projection='polar')
ax.plot(x, y, 'o', markersize=1, color='lightgrey', alpha=0.5)
ax.tricontourf(x, y, z, levels=levels)
plt.title("tricontourf")
ax = fig.add_subplot(143, projection='polar')
ax.plot(x, y, 'o', markersize=1, color='lightgrey', alpha=0.5)
ax.tripcolor(x, y, z)
plt.title("tripcolor")
ax = fig.add_subplot(144, projection='polar')
ax.triplot(x,y)
plt.title("triplot")
plt.tight_layout()
plt.show()以上就是python中28種極坐標(biāo)繪圖函數(shù)總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于python極坐標(biāo)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
利于python腳本編寫(xiě)可視化nmap和masscan的方法
這篇文章主要介紹了利于python腳本編寫(xiě)可視化nmap和masscan的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
python使用matplotlib繪制圖片時(shí)x軸的刻度處理
在使用matplotlib繪制圖片時(shí),x軸的刻度可能比較密集,特別是以日期作為x軸時(shí),則最后會(huì)顯示不出來(lái)。這篇文章主要介紹了python使用matplotlib繪制圖片時(shí)x軸的刻度處理,需要的朋友可以參考下2021-08-08
Python 實(shí)現(xiàn)RSA加解密文本文件
這篇文章主要介紹了Python 實(shí)現(xiàn)RSA加解密文本文件的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
python讀取mnist數(shù)據(jù)集方法案例詳解
這篇文章主要介紹了python讀取mnist數(shù)據(jù)集方法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Python連接MySQL報(bào)錯(cuò):缺少cryptography庫(kù)的解決辦法
這篇文章主要介紹了Python連接MySQL報(bào)錯(cuò):缺少cryptography庫(kù)的兩種解決辦法,分別是安裝cryptography庫(kù)和修改MySQL用戶(hù)認(rèn)證方式,兩種方法各有適用場(chǎng)景,需要的朋友可以參考下2026-06-06
python中for循環(huán)的多種使用實(shí)例
for語(yǔ)句是Python中執(zhí)行迭代的兩個(gè)語(yǔ)句之一,另一個(gè)語(yǔ)句是while,下面這篇文章主要給大家介紹了關(guān)于python中for循環(huán)的多種使用方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
基于python編寫(xiě)一個(gè)簡(jiǎn)單的壓力測(cè)試(DDoS)腳本
這篇文章主要為大家詳細(xì)介紹了如何基于python編寫(xiě)一個(gè)簡(jiǎn)單的壓力測(cè)試(DDoS)腳本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-12-12
Python一鍵實(shí)現(xiàn)統(tǒng)計(jì)本地文件或壓縮包的代碼行數(shù)
本文主要介紹了一個(gè)Python腳本,用于一鍵統(tǒng)計(jì)本地文件夾或壓縮包的代碼行數(shù),包括代碼行數(shù)、空白行數(shù)、注釋行數(shù)、總行數(shù)等,文中的示例代碼講解詳細(xì),希望對(duì)大家有所幫助2026-05-05

