Matplotlib-working-with-text

提供:Dev Guides
移動先:案内検索

Matplotlib-テキストの操作

Matplotlibは、数式のサポート、ラスターおよびベクター出力の TrueType サポート、任意の回転を伴う改行区切りテキスト、およびUnicodeサポートを含む、広範なテキストサポートを備えています。 Matplotlibには、クロスプラットフォーム、W3C準拠のフォント検索アルゴリズムを実装する独自のmatplotlib.font_managerが含まれています。

ユーザーは、テキストのプロパティ(フォントサイズ、フォントの太さ、テキストの場所と色など)を大幅に制御できます。 Matplotlibは、多数のTeX数学記号とコマンドを実装しています。

コマンドの次のリストは、Pyplotインターフェイスでテキストを作成するために使用されます-

text Add text at an arbitrary location of the Axes.
annotate Add an annotation, with an optional arrow, at an arbitrary location of theAxes.
xlabel Add a label to the Axes’s x-axis.
ylabel Add a label to the Axes’s y-axis.
title Add a title to the Axes.
figtext Add text at an arbitrary location of the Figure.
suptitle Add a title to the 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()

上記のコード行は、次の出力を生成します-

テキストの操作