日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python绘图3d_超好看的3D绘图方式,Python厉害了!

發(fā)布時(shí)間:2023/12/14 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python绘图3d_超好看的3D绘图方式,Python厉害了! 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【01x01】Axes3D 對(duì)象創(chuàng)建方法一:Axes3D(fig)

在 Matplotlib 1.0.0 版本中,繪制 3D 圖需要先導(dǎo)入 Axes3D 包,獲取 figure 畫(huà)布對(duì)象 fig 后,通過(guò) Axes3D(fig) 方法來(lái)創(chuàng)建 Axes3D 對(duì)象,具體方法如下:import?numpy?as?np

import?matplotlib.pyplot?as?plt

from?mpl_toolkits.mplot3d?import?Axes3D

#?獲取?figure?畫(huà)布并創(chuàng)建?Axes3D?對(duì)象

fig?=?plt.figure()

ax?=?Axes3D(fig)

#?數(shù)據(jù)坐標(biāo)

z?=?np.linspace(0,?15,?1000)

x?=?np.sin(z)

y?=?np.cos(z)

#?繪制線性圖

ax.plot(x,?y,?z)

plt.show()

【01x02】Axes3D 對(duì)象創(chuàng)建方法二:add_subplot

在 Matplotlib 3.2.0 版本中,繪制 3D 圖可以通過(guò)創(chuàng)建子圖,然后指定 projection 參數(shù) 為 3d 即可,返回的 ax 為 Axes3D 對(duì)象,以下兩種方法均可:import?numpy?as?np

import?matplotlib.pyplot?as?plt

#?獲取?figure?畫(huà)布并通過(guò)子圖創(chuàng)建?Axes3D?對(duì)象

fig?=?plt.figure()

ax?=?fig.add_subplot(111,?projection='3d')

#?數(shù)據(jù)坐標(biāo)

z?=?np.linspace(0,?15,?1000)

x?=?np.sin(z)

y?=?np.cos(z)

#?繪制線性圖

ax.plot(x,?y,?z)

plt.show()import?numpy?as?np

import?matplotlib.pyplot?as?plt

#?通過(guò)子圖創(chuàng)建?Axes3D?對(duì)象

ax?=?plt.subplot(111,?projection='3d')

#?數(shù)據(jù)坐標(biāo)

z?=?np.linspace(0,?15,?1000)

x?=?np.sin(z)

y?=?np.cos(z)

#?繪制線性圖

ax.plot(x,?y,?z)

plt.show()

【01x03】Axes3D 對(duì)象創(chuàng)建方法三:gca

除了以上兩種方法以外,還可以先獲取畫(huà)布對(duì)象 fig,再通過(guò) fig.gca() 方法獲取當(dāng)前繪圖區(qū)(gca = Get Current Axes),然后指定 projection 參數(shù) 為 3d 即可,返回的 ax 為 Axes3D 對(duì)象。import?numpy?as?np

import?matplotlib.pyplot?as?plt

#?依次獲取畫(huà)布和繪圖區(qū)并創(chuàng)建?Axes3D?對(duì)象

fig?=?plt.figure()

ax?=?fig.gca(projection='3d')

#?數(shù)據(jù)坐標(biāo)

z?=?np.linspace(0,?15,?1000)

x?=?np.sin(z)

y?=?np.cos(z)

#?繪制線性圖

ax.plot(x,?y,?z)

plt.show()

以上三種方法運(yùn)行結(jié)果均為下圖:

【02x00】cmap 與 colorbar

默認(rèn)情況下,散點(diǎn)圖、線性圖、曲面圖等將以純色著色,但可以通過(guò)提供 cmap 參數(shù)支持顏色映射。cmap 參數(shù)用于設(shè)置一些特殊的顏色組合,如漸變色等。

如果使用了 cmap 參數(shù),則可以使用 pyplot.colorbar() 函數(shù)來(lái)繪制一個(gè)色條,即顏色對(duì)照條。

基本語(yǔ)法:matplotlib.pyplot.colorbar([mappable=None, cax=None, ax=None, **kw])

部分參數(shù)解釋如下表,其他參數(shù),如長(zhǎng)度,寬度等請(qǐng)參考官方文檔。參數(shù)描述

mappable要設(shè)置色條的圖像對(duì)象,該參數(shù)對(duì)于 Figure.colorbar 方法是必需的,但對(duì)于 pyplot.colorbar 函數(shù)是可選的

cax可選項(xiàng),要繪制色條的軸

ax可選項(xiàng),設(shè)置色條的顯示位置,通常在一個(gè)畫(huà)布上有多個(gè)子圖時(shí)使用

**kw可選項(xiàng),其他關(guān)鍵字參數(shù),參考官方文檔

【03x00】3D 線性圖:Axes3D.plot

基本方法:Axes3D.plot(xs, ys[, zs, zdir='z', *args, **kwargs])參數(shù)描述

xs一維數(shù)組,點(diǎn)的 x 軸坐標(biāo)

ys一維數(shù)組,點(diǎn)的 y 軸坐標(biāo)

zs一維數(shù)組,可選項(xiàng),點(diǎn)的 z 軸坐標(biāo)

zdir可選項(xiàng),在 3D 軸上繪制 2D 數(shù)據(jù)時(shí),數(shù)據(jù)必須以 xs,ys 的形式傳遞

,若此時(shí)將 zdir 設(shè)置為 ‘y’,數(shù)據(jù)將會(huì)被繪制到 x-z 軸平面上,默認(rèn)為 ‘z’

**kwargs其他關(guān)鍵字參數(shù),可選項(xiàng),可參見(jiàn) matplotlib.axes.Axes.plotimport?numpy?as?np

import?matplotlib.pyplot?as?plt

#?設(shè)置中文顯示

plt.rcParams['font.sans-serif']?=?['Microsoft?YaHei']

#?依次獲取畫(huà)布和繪圖區(qū)并創(chuàng)建?Axes3D?對(duì)象

fig?=?plt.figure()

ax?=?fig.gca(projection='3d')

#?第一條3D線性圖數(shù)據(jù)

theta?=?np.linspace(-4?*?np.pi,?4?*?np.pi,?100)

z1?=?np.linspace(-2,?2,?100)

r?=?z1**2?+?1

x1?=?r?*?np.sin(theta)

y1?=?r?*?np.cos(theta)

#?第二條3D線性圖數(shù)據(jù)

z2?=?np.linspace(-3,?3,?100)

x2?=?np.sin(z2)

y2?=?np.cos(z2)

#?繪制3D線性圖

ax.plot(x1,?y1,?z1,?color='b',?label='3D?線性圖一')

ax.plot(x2,?y2,?z2,?color='r',?label='3D?線性圖二')

#?設(shè)置標(biāo)題、軸標(biāo)簽、圖例,也可以直接使用?plt.title、plt.xlabel、plt.legend...

ax.set_title('繪制?3D?線性圖示例',?pad=15,?fontsize='12')

ax.set_xlabel('x?軸',?color='r',?fontsize='12')

ax.set_ylabel('y?軸',?color='g',?fontsize='12')

ax.set_zlabel('z?軸',?color='b',?fontsize='12')

ax.legend()

plt.show()

【04x00】3D 散點(diǎn)圖:Axes3D.scatter

基本方法:Axes3D.scatter(xs, ys[, zs=0, zdir='z', s=20, c=None, depthshade=True, *args, **kwargs])參數(shù)描述

xs一維數(shù)組,點(diǎn)的 x 軸坐標(biāo)

ys一維數(shù)組,點(diǎn)的 y 軸坐標(biāo)

zs一維數(shù)組,可選項(xiàng),點(diǎn)的 z 軸坐標(biāo)

zdir可選項(xiàng),在 3D 軸上繪制 2D 數(shù)據(jù)時(shí),數(shù)據(jù)必須以 xs,ys 的形式傳遞

若此時(shí)將 zdir 設(shè)置為 ‘y’,數(shù)據(jù)將會(huì)被繪制到 x-z 軸平面上,默認(rèn)為 ‘z’,

s標(biāo)量或數(shù)組類(lèi)型,可選項(xiàng),標(biāo)記的大小,默認(rèn) 20

c標(biāo)記的顏色,可選項(xiàng),可以是單個(gè)顏色或者一個(gè)顏色列表

支持英文顏色名稱(chēng)及其簡(jiǎn)寫(xiě)、十六進(jìn)制顏色碼等,更多顏色示例參見(jiàn)官網(wǎng)Color Demo

depthshadebool 值,可選項(xiàng),默認(rèn) True,是否為散點(diǎn)標(biāo)記著色以提供深度外觀

**kwargs其他關(guān)鍵字參數(shù),可選項(xiàng),可參見(jiàn) scatterimport?numpy?as?np

import?matplotlib.pyplot?as?plt

plt.rcParams['font.sans-serif']?=?['Microsoft?YaHei']

#?依次獲取畫(huà)布和繪圖區(qū)并創(chuàng)建?Axes3D?對(duì)象

fig?=?plt.figure()

ax?=?fig.gca(projection='3d')

n?=?100

def?randrange(n,?vmin,?vmax):

return?(vmax?-?vmin)*np.random.rand(n)?+?vmin

'''

定義繪制?n?個(gè)隨機(jī)點(diǎn),設(shè)置每一組數(shù)據(jù)點(diǎn)的樣式和范圍

x軸數(shù)據(jù)位于[23,32]區(qū)間,y軸數(shù)據(jù)位于[0,100]區(qū)間,z軸數(shù)據(jù)位于[zlow,zhigh]區(qū)間

'''

for?m,?zlow,?zhigh?in?[('o',?-50,?-25),?('^',?-30,?-5)]:

xs?=?randrange(n,?23,?32)

ys?=?randrange(n,?0,?100)

zs?=?randrange(n,?zlow,?zhigh)

ax.scatter(xs,?ys,?zs,?marker=m)

#?設(shè)置標(biāo)題、軸標(biāo)簽、圖例,也可以直接使用?plt.title、plt.xlabel...

ax.set_title('繪制?3D?散點(diǎn)圖示例',?pad=15,?fontsize='12')

ax.set_xlabel('x?軸',?color='b')

ax.set_ylabel('y?軸',?color='b')

ax.set_zlabel('z?軸',?color='b')

plt.show()

【05x00】3D 線框圖:Axes3D.plot_wireframe

基本方法:Axes3D.plot_wireframe(X, Y, Z[, *args, **kwargs])參數(shù)描述

X二維數(shù)組,x 軸數(shù)據(jù)

Y二維數(shù)組,y 軸數(shù)據(jù)

Z二維數(shù)組,z 軸數(shù)據(jù)

**kwargs其他關(guān)鍵字參數(shù),可選項(xiàng),如線條樣式顏色等,可參見(jiàn)Line3DCollectionimport?numpy?as?np

import?matplotlib.pyplot?as?plt

plt.rcParams['font.sans-serif']?=?['Microsoft?YaHei']

#?獲取?figure?畫(huà)布并通過(guò)子圖創(chuàng)建?Axes3D?對(duì)象

fig?=?plt.figure()

ax?=?fig.add_subplot(111,?projection='3d')

#?定義Z軸坐標(biāo)的生成方法

def?f(m,?n):

return?np.sin(np.sqrt(m?**?2?+?n?**?2))

#?設(shè)置3D線框圖數(shù)據(jù)

x?=?np.linspace(-6,?6,?30)

y?=?np.linspace(-6,?6,?30)

#?生成網(wǎng)格點(diǎn)坐標(biāo)矩陣,該方法在系列文章八中有具體介紹

X,?Y?=?np.meshgrid(x,?y)

Z?=?f(X,?Y)

#?繪制3D線框圖

ax.plot_wireframe(X,?Y,?Z,?color='c')

#?設(shè)置標(biāo)題、軸標(biāo)簽、圖例,也可以直接使用?plt.title、plt.xlabel...

ax.set_title('繪制?3D?線框圖示例',?pad=15,?fontsize='12')

ax.set_xlabel('x?軸')

ax.set_ylabel('y?軸')

ax.set_zlabel('z?軸')

plt.show()

總結(jié)

以上是生活随笔為你收集整理的python绘图3d_超好看的3D绘图方式,Python厉害了!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。