Matplotlib - 圖形類



在 Matplotlib 中,Figure 是一個頂級容器,它包含繪圖或視覺化中的所有元素。它是包含各種元件(如座標軸、標籤、標題、圖例、顏色條和其他元素)的整體視窗或畫布。

參見下圖以供參考:

Figure_class Intro

在上圖中,綠色區域表示圖形,白色區域是座標軸區域。

Matplotlb 中的圖形類

Matplotlib 中的Figure() 類是一個頂級圖形物件,充當所有繪圖元素的主要容器。它將所有內容組合在一起,包括子圖、座標軸、標題、圖例和其他圖形元素。

此類可在matplotlib.figure 模組中找到,除了 Figure() 類之外,該模組還包含與建立和管理圖形相關的類,並提供多種自定義選項。

建立圖形

通常使用 pyplot 方法(如 figure、subplots 和 subplot_mosaic)建立 Figure 例項。這些方法同時返回 Figure 例項和一組 Axes,提供了一種方便的方式來建立和使用視覺化效果。

示例

這是一個使用pyplot.figure() 方法建立圖形的示例。

import matplotlib.pyplot as plt
import numpy as np

# Creating the Figure instance
fig = plt.figure(figsize=[7, 3], facecolor='lightgreen', layout='constrained')

# Adding a title to the Figure
fig.suptitle('Figure')

# Adding a subplot (Axes) to the Figure
ax = fig.add_subplot()

# Setting a title for the subplot
ax.set_title('Axes', loc='left', fontstyle='oblique', fontsize='medium')

# Showing the plot
plt.show()
輸出

執行上述程式碼後,我們將得到以下輸出:

Figure_class Ex1

示例

此示例演示瞭如何在 Matplotlib 的單個指令碼中分別建立多個圖形。

from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create Figure 1
fig1 = plt.figure("Figure 1")
plt.plot([1, 3, 7, 3, 1], c="red", lw=2)
plt.title("Figure 1")

# Create Figure 2
fig2 = plt.figure("Figure 2")
plt.plot([1, 3, 7, 3, 1], c="green", lw=5)
plt.title("Figure 2")

# Display both figures
plt.show()
輸出

執行上述程式碼後,您將得到以下輸出:

Figure_class Ex5_1

 

Figure_class Ex5_2

建立帶有子圖網格的圖形

建立圖形時,可以自定義各種選項,包括子圖、大小、解析度、顏色和佈局。Figure 類的屬性(如 figsize、dpi、facecolor、edgecolor、linewidth 和 layout)在塑造視覺化的外觀方面起著至關重要的作用。

示例

這是一個使用pyplot.subplots() 方法建立 2x2 子圖網格以及多個自定義選項的示例。

import matplotlib.pyplot as plt
import numpy as np

# Create a 2x2 grid of subplots with various customization options
fig, axs = plt.subplots(2, 2, figsize=(7, 4), facecolor='lightgreen',
   layout='constrained')

# Super title for the entire figure
fig.suptitle('2x2 Grid of Subplots', fontsize='x-large')

# Display the Figure
plt.show()

輸出

執行上述程式碼後,我們將得到以下輸出:

Figure_class Ex2

示例

這是另一個使用plt.subplot_mosaic() 方法建立更復雜佈局的示例。

import matplotlib.pyplot as plt

# Create a more complex layout using plt.subplot_mosaic()
fig, axs = plt.subplot_mosaic([['A', 'right'], ['B', 'right']], 
   facecolor='lightgreen',
   layout='constrained')

# Add text to each subplot
for ax_name, ax in axs.items():
   ax.text(0.5, 0.5, ax_name, ha='center', va='center', 
   fontsize='large', fontweight='bold', color='blue')

# Super title for the entire figure
fig.suptitle('Complex Layout using subplot_mosaic()', fontsize='x-large')

# Display the Figure
plt.show()

輸出

執行上述程式碼後,我們將得到以下輸出:

Figure_class Ex3

儲存圖形

完成視覺化後,可以使用savefig() 方法輕鬆地將圖形儲存到磁碟。此方法允許您指定檔案格式(例如,PNG、PDF)並自定義解析度和邊界框等選項。

示例

讓我們來看一個儲存 Figure 物件的簡單示例。

import matplotlib.pyplot as plt

# Create a 2x2 grid of subplots with various customization options
fig, axs = plt.subplots(2, 2, figsize=(7, 4), facecolor='lightgreen',
   layout='constrained')

# Super title for the entire figure
fig.suptitle('2x2 Grid of Subplots', fontsize='x-large')

# Super title for the entire figure
fig.suptitle('Saving a Figure', fontsize='x-large')

# Display the Figure
plt.show()

# Save the Figure object to a file
fig.savefig('Saved Figure.png', dpi=300)

輸出

執行上述程式後,以下圖形將儲存在您的工作目錄中:

Figure_class Ex4
廣告