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

python35種繪圖函數(shù)詳細(xì)總結(jié)

 更新時(shí)間:2023年08月22日 09:58:22   作者:微小冷  
Python有許多用于繪圖的函數(shù)和庫(kù),比如Matplotlib,Plotly,Bokeh,Seaborn等,這只是一些常用的繪圖函數(shù)和庫(kù),Python還有其他繪圖工具,如Pandas、ggplot等,選擇適合你需求的庫(kù),可以根據(jù)你的數(shù)據(jù)類型、圖形需求和個(gè)人偏好來(lái)決定,本文給大家總結(jié)了python35種繪圖函數(shù)

基礎(chǔ)圖

下面這8種圖像一般只有兩組坐標(biāo),直觀容易理解。

函數(shù)坐標(biāo)參數(shù)圖形類別
plotx,y曲線圖
stackplotx,y散點(diǎn)圖
stemx,y莖葉圖
scatterx,y散點(diǎn)圖
polarx,y極坐標(biāo)圖
stepx,y步階圖
barx,y條形圖
barhx,y橫向條形圖

其中,除了極坐標(biāo)需要添加一個(gè)極坐標(biāo)映射之外,其他函數(shù)均在直角坐標(biāo)系中繪制,效果如下

在這里插入圖片描述

繪圖代碼如下

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(25)/3
y = np.sin(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):
    p = "polar" if key=="polar" else None
    ax = fig.add_subplot(2,4,i, projection=p)
    fDct[key](x, y)
    plt.title(key)
plt.tight_layout()
plt.show()

誤差線

實(shí)際繪圖時(shí),誤差線這種需求十分常見(jiàn),尤其是在做擬合的時(shí)候,除了要畫出趨勢(shì)線之外,還可能要畫出其抖動(dòng)的范圍,下面三種函數(shù)主要實(shí)現(xiàn)這個(gè)功能。

函數(shù)坐標(biāo)圖形類別
errorbarx,y,xerr,yerr誤差線
fill_betweenx,y1,y2縱向區(qū)間圖
fill_betweenxy, x1, x2橫向區(qū)間圖

圖像效果為

在這里插入圖片描述

繪圖代碼如下,errorbar函數(shù)的誤差方向,與輸入的參數(shù)有關(guān)。

x = np.arange(25)/3
y = np.sin(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,6))
ax = fig.add_subplot(221)
ax.errorbar(x, y, yerr=yerr)
plt.title("errorbar with yerr")
ax = fig.add_subplot(222)
ax.errorbar(x, y, xerr=xerr)
plt.title("errorbar with xerr")
ax = fig.add_subplot(223)
ax.fill_between(x, y1, y2)
plt.title("fill_between")
ax = fig.add_subplot(224)
ax.fill_betweenx(y, x1, x2)
plt.title("fill_betweenx")
plt.tight_layout()
plt.show()

三維圖

繪圖函數(shù)坐標(biāo)繪圖類型坐標(biāo)說(shuō)明
plotx,y,z三維曲線圖
scatterx,y,z三維散點(diǎn)圖
plot_surfacex,y,z三維曲面圖x,y必須是網(wǎng)格
plot_wireframex,y,z三維網(wǎng)格圖x,y必須是網(wǎng)格
plot_trisurfx,y,z三角曲面圖x,y,z是一維數(shù)組

plot和scatter雖然是二維繪圖函數(shù),但如果新增一個(gè)三維坐標(biāo),就可以搖身一變,成為三維曲線圖或者三維散點(diǎn)圖

在這里插入圖片描述

繪圖代碼如下

x = np.arange(100)/10
y,z = np.sin(x), np.cos(x)
fig = plt.figure(figsize=(8,4))
ax = fig.add_subplot(121, projection='3d')
ax.plot(x,y,z)
plt.title("plot")
ax = fig.add_subplot(122, projection='3d')
ax.scatter(x,y,z)
plt.title("scatter")
plt.tight_layout()
plt.show()

真正專業(yè)的三維圖是plot_surface, plot_wireframe和plot_trisurf

在這里插入圖片描述

如果仔細(xì)看就會(huì)發(fā)現(xiàn)plot_trisurf的紋理和前兩者不同,相比之下,前兩者必須要求輸入規(guī)整的數(shù)據(jù)。繪圖代碼如下

X, Y = np.indices([30, 30])/3 - 5
Z = np.sin(np.sqrt(X**2 + Y**2))
fig = plt.figure(figsize=(12,5))
ax = fig.add_subplot(131, projection='3d')
ax.plot_surface(X, Y, Z)
plt.title("plot_surface")
ax = fig.add_subplot(132, projection='3d')
ax.plot_wireframe(X, Y, Z)
plt.title("plot_wireframe")
ax = fig.add_subplot(133, projection='3d')
ax.plot_trisurf(X.reshape(-1), Y.reshape(-1), Z.reshape(-1))
plt.title("plot_trisurf")
plt.tight_layout()
plt.show()

等高線圖

繪圖函數(shù)坐標(biāo)說(shuō)明
contour[x,y,]z等高線
contourf[x,y,]z填充等高線
pcolormesh[x,y,]z偽彩圖
imshowz圖像

其中,imshow就是正常的圖片展示函數(shù),這幾個(gè)函數(shù)可以只指定z軸然后繪圖

X, Y = np.indices([100,100])/30 - 1.5
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
fDct = {"contour": plt.contour, "contourf":plt.contourf, 
    "pcolormesh" : plt.pcolormesh, "imshow":plt.imshow}
fig = plt.figure(figsize=(9,6))
for i,key in enumerate(fDct, 1):
    ax = fig.add_subplot(2,2,i)
    fDct[key](Z)
    plt.title(key)
plt.tight_layout()
plt.show()

繪圖結(jié)果如下

在這里插入圖片描述

可以看到,imshow和另外三種函數(shù)的區(qū)別是,其橫坐標(biāo)和縱坐標(biāo)之間的比例始終是1:1,并不隨著圖像的拉伸而放大或者縮小。

除了imshow之外,另外三種函數(shù)還支持輸入x,y,z三個(gè)坐標(biāo)軸的數(shù)據(jù)來(lái)繪圖,效果如下

在這里插入圖片描述

繪圖代碼如下

X, Y = np.indices([100,100])/30 - 1.5
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - 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)
    fDct[key](X,Y,Z)
    plt.title(key)
plt.tight_layout()
plt.show()

場(chǎng)圖

繪圖函數(shù)坐標(biāo)說(shuō)明
quiverx,y,u,v向量場(chǎng)圖
streamplotx,y,u,v流場(chǎng)圖
barbsx,y,u,v風(fēng)場(chǎng)圖

quiver以單點(diǎn)為單位,繪制出某點(diǎn)處向量的方向;streamplot則在此之上,將每個(gè)點(diǎn)銜接到一起,顯得更加有流動(dòng)性;barbs則是以風(fēng)向標(biāo)志取代了向量,這個(gè)圖過(guò)于專業(yè),我應(yīng)該沒(méi)啥機(jī)會(huì)用到。

Y, X = np.indices([6,6])/0.75 - 4
U = X + Y
V = Y - 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)
    dct[key](X,Y,U,V)
    plt.title(key)
plt.tight_layout()
plt.show()

在這里插入圖片描述

統(tǒng)計(jì)圖

繪圖函數(shù)坐標(biāo)說(shuō)明
histx數(shù)據(jù)直方圖
boxplotx箱線圖
violinplotx小提琴圖
enventplotx平行線疏密圖
hist2dx,y二維直方圖
hexbinx,y鉆石圖
piex餅圖

其中hist, boxplot, violinplot, enventplot是統(tǒng)計(jì)一維數(shù)據(jù)的,可直接輸入隨機(jī)數(shù),繪圖函數(shù)會(huì)自行統(tǒng)計(jì)其區(qū)間

在這里插入圖片描述

繪圖代碼如下

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,2,i)
    dct[key](x)
    plt.title(key)
ax = fig.add_subplot(224)
ax.eventplot(x)
plt.title("eventplot")
plt.tight_layout()
plt.show()

hist2d和hexbin用于統(tǒng)計(jì)二維數(shù)據(jù),最終以圖像的形式展示出來(lái),二者在觀感上的主要區(qū)別是,hist2d的“像素”是方形的,而hexbin則是六邊形的。

在這里插入圖片描述

繪圖代碼如下

x = np.random.randn(5000)
y = 1.2 * x + np.random.randn(5000) / 3
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(121)
ax.hist2d(x, y, bins=[np.arange(-3,3,0.1)] * 2)
plt.title("hist2d")
ax = fig.add_subplot(122)
ax.hexbin(x, y, gridsize=20)
plt.title("hexbin")
plt.tight_layout()
plt.show()

最后還有一個(gè)餅圖,餅圖要求輸入坐標(biāo)必須都大于0,繪圖代碼如下

plt.pie([1,2,3,4,5])
plt.tight_layout()
plt.show()

在這里插入圖片描述

非結(jié)構(gòu)坐標(biāo)圖

下面這四個(gè)繪圖函數(shù)有一個(gè)特點(diǎn),即其繪圖坐標(biāo)并不是格式化的,而支持隨機(jī)坐標(biāo)進(jìn)行繪圖,這一點(diǎn)和plot_trisurf比較相似

繪圖函數(shù)坐標(biāo)說(shuō)明
tricontourx,y,z非結(jié)構(gòu)等高線
tricontourfx,y,z非結(jié)構(gòu)化填充等高線
tricolorx,y,z非結(jié)構(gòu)化偽彩圖
triplotx,y三角連線圖

在這里插入圖片描述

繪圖代碼如下

x = np.random.uniform(-4, 4, 256)
y = np.random.uniform(-2, 2, 256)
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
levels = np.linspace(z.min(), z.max(), 7)
fig = plt.figure(figsize=(12,6))
ax = fig.add_subplot(221)
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(222)
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(223)
ax.plot(x, y, 'o', markersize=1, color='lightgrey', alpha=0.5)
ax.tripcolor(x, y, z)
plt.title("tripcolor")
ax = fig.add_subplot(224)
ax.triplot(x,y)
plt.title("triplot")
plt.tight_layout()
plt.show()

以上就是python35種繪圖函數(shù)詳細(xì)總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于python繪圖函數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 利用Python操作MongoDB數(shù)據(jù)庫(kù)的詳細(xì)指南

    利用Python操作MongoDB數(shù)據(jù)庫(kù)的詳細(xì)指南

    MongoDB是由C++語(yǔ)言編寫的非關(guān)系型數(shù)據(jù)庫(kù),是一個(gè)基于分布式文件存儲(chǔ)的開源數(shù)據(jù)庫(kù)系統(tǒng),其內(nèi)容存儲(chǔ)形式類似JSON對(duì)象,下面這篇文章主要給大家介紹了關(guān)于利用Python操作MongoDB數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • idea2020手動(dòng)安裝python插件的實(shí)現(xiàn)方法

    idea2020手動(dòng)安裝python插件的實(shí)現(xiàn)方法

    這篇文章主要介紹了idea2020手動(dòng)安裝python插件的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Python鏈?zhǔn)秸{(diào)用數(shù)據(jù)處理實(shí)際應(yīng)用實(shí)例探究

    Python鏈?zhǔn)秸{(diào)用數(shù)據(jù)處理實(shí)際應(yīng)用實(shí)例探究

    本文將深入介紹Python鏈?zhǔn)秸{(diào)用的概念、原理以及實(shí)際應(yīng)用,通過(guò)豐富的示例代碼,幫助讀者更全面地理解和應(yīng)用這一編程技巧
    2024-01-01
  • Pandas數(shù)據(jù)離散化原理及實(shí)例解析

    Pandas數(shù)據(jù)離散化原理及實(shí)例解析

    這篇文章主要介紹了Pandas數(shù)據(jù)離散化原理及實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • python實(shí)現(xiàn)簡(jiǎn)易通訊錄修改版

    python實(shí)現(xiàn)簡(jiǎn)易通訊錄修改版

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易通訊錄的修改版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Python 多進(jìn)程并發(fā)操作中進(jìn)程池Pool的實(shí)例

    Python 多進(jìn)程并發(fā)操作中進(jìn)程池Pool的實(shí)例

    下面小編就為大家?guī)?lái)一篇Python 多進(jìn)程并發(fā)操作中進(jìn)程池Pool的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • python反編譯學(xué)習(xí)之字節(jié)碼詳解

    python反編譯學(xué)習(xí)之字節(jié)碼詳解

    這篇文章主要給大家介紹了關(guān)于python反編譯學(xué)習(xí)之字節(jié)碼的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 詳解Pandas之容易讓人混淆的行選擇和列選擇

    詳解Pandas之容易讓人混淆的行選擇和列選擇

    這篇文章主要介紹了詳解Pandas之容易讓人混淆的行選擇和列選擇,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python?requirements.txt的具體使用

    Python?requirements.txt的具體使用

    requirements.txt文件是項(xiàng)目的依賴包及其對(duì)應(yīng)版本號(hào)的信息列表,本文主要介紹了Python?requirements.txt的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • python缺失值填充方法示例代碼

    python缺失值填充方法示例代碼

    常見(jiàn)的數(shù)據(jù)缺失填充方式分為很多種,比如刪除法、均值法、回歸法、KNN、MICE、EM等,下面這篇文章主要給大家介紹了關(guān)于python缺失值填充方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12

最新評(píng)論