Matplotlib - 文字屬性



Matplotlib 中的文字屬性指的是一組可以配置的屬性,用於控制繪圖中文字元素的外觀和佈局。這些屬性包括各種特性,例如:

  • 字型樣式
  • 顏色
  • 大小
  • 對齊方式等等。

透過操縱這些屬性,您可以自定義繪圖中文字的視覺效果。

在 Matplotlib 中控制文字屬性和佈局涉及配置 matplotlib.text.Text 例項的各種屬性。這些屬性可以透過 set_title、set_xlabel 和 text 等函式中的關鍵字引數進行調整。

下面,我們將探討 Matplotlib 中的關鍵文字屬性以及示例。

文字佈局和定位屬性

文字佈局和定位是放置和對齊繪圖中文字元素的關鍵方面。以下是屬性列表及其詳細資訊。

  • 位置 - 指定放置文字的座標 (x, y)。

  • 旋轉 - 定義文字的旋轉角度。選項包括度數、'vertical' 或 'horizontal'。

  • 水平對齊 (ha) - 確定文字沿 x 軸的對齊方式。選項包括 'center'、'right' 和 'left'。

  • 垂直對齊 (va) - 控制文字沿 y 軸的對齊方式。選項包括 'center'、'top'、'bottom' 和 'baseline'。

  • 多行對齊 - 對於換行符分隔的字串,此屬性控制不同行的左、中或右對齊。

示例

此示例演示了各種佈局和定位屬性的應用。

import matplotlib.pyplot as plt

# Create a figure
fig, ax = plt.subplots(figsize=(7, 4))

# Set the axis limits
plt.axis((0, 10, 0, 10))

# Add text with various layout and positioning properties
ax.text(5, 7, 'Centered Text with 0 rotation', horizontalalignment='center', verticalalignment='center', rotation=0)
ax.text(1, 5, 'Right Aligned Text with 90 degrees rotation', ha='right', va='center', rotation=90)
ax.text(9, 5, 'Left Aligned Text with -90 degrees rotation', ha='left', va='center', rotation=-90)
ax.text(5, 2, 'Multiline\nText', ha='center', va='center', multialignment='center')

# Display the plot
plt.show()
print('Text is added successfully with the various layout and positioning properties..')

輸出

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

Test_properties Example 1
Text is added successfully with the various layout and positioning properties..

文字顏色和透明度屬性

顏色和透明度屬性增強了繪圖中文字元素的視覺外觀。

  • color - 指定文字的顏色。這可以是任何有效的 matplotlib 顏色。

  • backgroundcolor - 定義文字後面的背景顏色。可以使用任何有效的 matplotlib 顏色進行設定。

  • alpha - 表示文字的透明度。它指定為浮點數,其中 0.0 表示完全透明,1.0 表示完全不透明。

示例

此示例演示如何在 Matplotlib 中使用顏色和透明度屬性。

import matplotlib.pyplot as plt

# Create a figure
fig, ax = plt.subplots(figsize=(7, 4))

# Set the axis limits
plt.axis((0, 10, 0, 10))

# Add text with color and transparency properties
ax.text(3, 8, 'Plain text without any property')
ax.text(3, 6, 'Colored Text', color='blue')
ax.text(3, 4, 'Background Color', backgroundcolor='yellow')
ax.text(3, 2, 'Transparent Text', alpha=0.5)

# Display the plot
plt.show()
print('Text added successfully...')

輸出

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

Test_properties Example 2
Text added successfully...

不同的字型屬性

字型屬性對於自定義繪圖中文字元素的外觀至關重要。這些屬性可以控制用於渲染文字的字型的型別、樣式、粗細、大小、變體和名稱。

  • Family - 指定要使用的字型型別,例如 'serif'、'sans-serif' 或 'monospace'。

  • Style 或 Fontstyle - 確定字型的樣式,選項包括 'normal'、'italic' 或 'oblique'。

  • Weight 或 Fontweight - 字型的粗細或粗體。選項包括 'normal'、'bold'、'heavy'、'light'、'ultrabold' 和 'ultralight'。

  • Size 或 Fontsize - 指定字型的磅值大小或相對大小,例如 'smaller' 或 'x-large'。

  • Name 或 Fontname - 將字型的名稱定義為字串。示例包括 'Sans'、'Courier'、'Helvetica' 等等。

  • Variant - 描述字型變體,選項包括 'normal' 或 'small-caps'。

示例

此示例演示如何使用各種字型屬性來自定義 Matplotlib 中文字的外觀。

import matplotlib.pyplot as plt

# Create a figure
fig = plt.figure(figsize=(7, 4))

# Set the axis limits
plt.axis((0, 10, 0, 10))

# Define a long string
sample_text = ("Tutorialspoint")

# Add text at various locations with various configurations
plt.text(0, 9, 'Oblique text placed at the center with a 25-degree rotation',
   fontstyle='oblique', fontweight='heavy', ha='center', va='baseline', rotation=45, wrap=True)
plt.text(7.5, 0.5, 'Text placed at the left with a 45-degree rotation', ha='left', rotation=45, wrap=True)
plt.text(5, 5, sample_text + '\nMiddle', ha='center', va='center', color='green', fontsize=24)
plt.text(10.5, 7, 'Text placed at the right with a -45-degree rotation', ha='right', rotation=-45, wrap=True)
plt.text(5, 10, 'Sans text with oblique style is placed at the center', fontsize=10, fontname='Sans', 
   style='oblique', ha='center', va='baseline', wrap=True)
plt.text(6, 3, 'A serif family text is placed right with the italic style', family='serif', 
   style='italic', ha='right', wrap=True)
plt.text(-0.5, 0, 'Small-caps variant text is placed at the left with a -25-degree rotation', 
   variant='small-caps', ha='left', rotation=-25, wrap=True)

# Display the plot
plt.show()
print('Text added successfully...')

輸出

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

Test_properties Example 3
Text added successfully...

邊界框和裁剪屬性

邊界框和裁剪屬性用於控制繪圖中文字元素的佈局和可見性。這些屬性包括為文字指定邊界框、定義裁剪引數以及啟用或停用裁剪。

  • Bbox - 為文字定義邊界框。它包括顏色、填充等屬性。

  • Clip Box 和 Clip Path - 指定用於裁剪文字的邊界框或路徑。這些屬性允許您控制文字可見的區域。

  • Clip On - 一個布林值,指示是否啟用了裁剪。設定為 True 時,文字將被裁剪到指定的區域。

示例

此示例演示了在向繪圖新增文字時如何使用邊界框和裁剪屬性。

import matplotlib.pyplot as plt

# Create a figure
fig = plt.figure(figsize=(7, 4))

# Set the axis limits
plt.axis((0, 10, 0, 10))

# Add text with bounding box and clipping properties
plt.text(3, 7, 'Text with Bounding Box', bbox={'facecolor': 'yellow', 'edgecolor': 'blue', 'pad': 5})
plt.text(3, 5, 'Clipped Text', bbox={'facecolor': 'lightgreen', 'edgecolor': 'darkgreen', 'pad': 5}, clip_on=True)

# Display the plot
plt.show()
print('Text added successfully...')

輸出

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

Test_properties Example 4
Text added successfully...

其他屬性

Matplotlib 還提供其他屬性,例如 Label、Linespacing、picker 等。

廣告