Matplotlib - 文字處理



Matplotlib 具有廣泛的文字支援,包括對數學表示式的支援,TrueType 對光柵和向量輸出的支援,任意旋轉的換行分隔文字以及 Unicode 支援。Matplotlib 包含自己的 matplotlib.font_manager,它實現了一個跨平臺、符合 W3C 標準的字型查詢演算法。

使用者可以對文字屬性(字型大小、字型粗細、文字位置和顏色等)進行大量控制。Matplotlib 實現大量的 TeX 數學符號和命令。

以下命令用於在 Pyplot 介面中建立文字:

text 在 Axes 的任意位置新增文字。
annotate 在 Axes 的任意位置添加註釋,可選帶箭頭。
xlabel 為 Axes 的 x 軸新增標籤。
ylabel 為 Axes 的 y 軸新增標籤。
title 為 Axes 新增標題。
figtext 在 Figure 的任意位置新增文字。
suptitle 為 Figure 新增標題。

所有這些函式都會建立一個並返回一個matplotlib.text.Text() 例項。

以下指令碼演示了上述某些函式的使用:

import matplotlib.pyplot as plt
fig = plt.figure()

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

ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
ax.text(3, 8, 'boxed italics text in data coords', style='italic', 
bbox = {'facecolor': 'red'})
ax.text(2, 6, r'an equation: $E = mc^2$', fontsize = 15)
ax.text(4, 0.05, 'colored text in axes coords',
verticalalignment = 'bottom', color = 'green', fontsize = 15)
ax.plot([2], [1], 'o')
ax.annotate('annotate', xy = (2, 1), xytext = (3, 4),
arrowprops = dict(facecolor = 'black', shrink = 0.05))
ax.axis([0, 10, 0, 10])
plt.show()

以上程式碼將生成以下輸出:

Working With Text
廣告