日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

基于Matplotlib?調(diào)用?pyplot?模塊中?figure()?函數(shù)處理?figure圖形對(duì)象_python

作者:睿科知識(shí)云 ? 更新時(shí)間: 2022-04-25 編程語(yǔ)言

Matplotlib 中,面向?qū)ο缶幊痰暮诵乃枷胧莿?chuàng)建圖形對(duì)象(figure object)。通過(guò)圖形對(duì)象來(lái)調(diào)用其它的方法和屬性,這樣有助于我們更好地處理多個(gè)畫(huà)布。在這個(gè)過(guò)程中,pyplot 負(fù)責(zé)生成圖形對(duì)象,并通過(guò)該對(duì)象來(lái)添加一個(gè)或多個(gè) axes 對(duì)象(即繪圖區(qū)域)。

Matplotlib 提供了matplotlib.figure圖形類(lèi)模塊,它包含了創(chuàng)建圖形對(duì)象的方法。通過(guò)調(diào)用 pyplot 模塊中 figure() 函數(shù)來(lái)實(shí)例化 figure 對(duì)象。

如下所示:

from matplotlib import pyplot as plt
#創(chuàng)建圖形對(duì)象
fig = plt.figure()

該函數(shù)的參數(shù)值,如下所示:

參數(shù) 說(shuō)明
figsize 指定畫(huà)布的大小,(寬度,高度),單位為英寸。
dpi 指定繪圖對(duì)象的分辨率,即每英寸多少個(gè)像素,默認(rèn)值為80。
facecolor 背景顏色。
dgecolor 邊框顏色。
frameon 是否顯示邊框。

下面使用 figure() 創(chuàng)建一個(gè)空白畫(huà)布:

fig = plt.figure()

我們使用add_axes() axes 軸域添加到畫(huà)布中。

如下所示:

ax=fig.add_axes([0,0,1,1])

add_axes() 的參數(shù)值是一個(gè)序列,序列中的 4 個(gè)數(shù)字分別對(duì)應(yīng)圖形的左側(cè),底部,寬度,和高度,且每個(gè)數(shù)字必須介于 0 到 1 之間。

設(shè)置 x 和 y 軸的標(biāo)簽以及標(biāo)題,如下所示:

ax.set_title("sine wave")
ax.set_xlabel('angle')
ax.set_ylabel('sine')

調(diào)用 axes 對(duì)象的 plot() 方法,對(duì) x 、 y 數(shù)組進(jìn)行繪圖操作:

ax.plot(x,y)

完整的代碼如下所示:

from matplotlib import pyplot as plt
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
y = np.sin(x)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.plot(x,y)
ax.set_title("sine wave")
ax.set_xlabel('angle')
ax.set_ylabel('sine')
plt.show()

輸出結(jié)果如下:

在 Jupyter Notebook 中運(yùn)行程序,結(jié)果如下:

原文鏈接:https://blog.csdn.net/ccc369639963/article/details/122980719

欄目分類(lèi)
最近更新