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

bar和barh的對偶關系稍微有些抽象,可以理解為前者是以角度方向為x軸;而barh則是以半徑方向為x軸。
代碼如下
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()誤差線
| 函數(shù) | 坐標 | 圖形類別 |
|---|---|---|
| errorbar | x,y,xerr,yerr | 誤差線 |
| 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()等高線polar
| 繪圖函數(shù) | 坐標 | 說明 |
|---|---|---|
| contour | [x,y,]z | 等高線 |
| contourf | [x,y,]z | 填充等高線 |
| pcolormesh | [x,y,]z | 偽彩圖 |
由于imshow默認其繪圖坐標是標準的1x1網格,而在極坐標種,這種網格的尺寸會隨著r的增大而增大,從而變得極其不實用,所以下面對極坐標圖的演示,就不包含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()場圖polar
| 繪圖函數(shù) | 坐標 | 說明 |
|---|---|---|
| quiver | x,y,u,v | 向量場圖 |
| streamplot | x,y,u,v | 流場圖 |
| barbs | x,y,u,v | 風場圖 |

代碼如下
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)計圖
| 繪圖函數(shù) | 坐標 | 說明 |
|---|---|---|
| hist | x | 數(shù)據(jù)直方圖 |
| boxplot | x | 箱線圖 |
| violinplot | x | 小提琴圖 |
| enventplot | x | 平行線疏密圖 |
| hist2d | x,y | 二維直方圖 |
| hexbin | x,y | 鉆石圖 |
| pie | x | 餅圖 |
極坐標在繪制直方圖的時候,需要注意其橫坐標是以2π為周期的,也就是說隨機變量的最大值和最小值不得相差2π,否則會導致重疊。

由于極坐標繪圖本質上是一種坐標映射,所以并不會把0和360°真正地等同起來,所以在hist2d中,整個圖像并沒有閉合。而最有意思的是餅圖,直接給壓扁了,讓人很難一下子看出不同組分的比例關系。
代碼如下
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()非結構坐標圖
| 繪圖函數(shù) | 坐標 | 說明 |
|---|---|---|
| tricontour | x,y,z | 非結構等高線 |
| tricontourf | x,y,z | 非結構化填充等高線 |
| tricolor | x,y,z | 非結構化偽彩圖 |
| triplot | x,y | 三角連線圖 |

代碼如下
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種極坐標繪圖函數(shù)總結的詳細內容,更多關于python極坐標的資料請關注腳本之家其它相關文章!
相關文章
利于python腳本編寫可視化nmap和masscan的方法
這篇文章主要介紹了利于python腳本編寫可視化nmap和masscan的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
python使用matplotlib繪制圖片時x軸的刻度處理
在使用matplotlib繪制圖片時,x軸的刻度可能比較密集,特別是以日期作為x軸時,則最后會顯示不出來。這篇文章主要介紹了python使用matplotlib繪制圖片時x軸的刻度處理,需要的朋友可以參考下2021-08-08
python讀取mnist數(shù)據(jù)集方法案例詳解
這篇文章主要介紹了python讀取mnist數(shù)據(jù)集方法案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-09-09
Python連接MySQL報錯:缺少cryptography庫的解決辦法
這篇文章主要介紹了Python連接MySQL報錯:缺少cryptography庫的兩種解決辦法,分別是安裝cryptography庫和修改MySQL用戶認證方式,兩種方法各有適用場景,需要的朋友可以參考下2026-06-06
Python一鍵實現(xiàn)統(tǒng)計本地文件或壓縮包的代碼行數(shù)
本文主要介紹了一個Python腳本,用于一鍵統(tǒng)計本地文件夾或壓縮包的代碼行數(shù),包括代碼行數(shù)、空白行數(shù)、注釋行數(shù)、總行數(shù)等,文中的示例代碼講解詳細,希望對大家有所幫助2026-05-05

