テキストの表示と書式設定
テキストの表示
Axes.text() を使って Axes にテキストを表示することができます。
インスタンス引数にはテキスト左下の座標と表示する文字列などを指定します。fontfamily や size, color などを設定することもできます。その場合は matplotlib.text.Textクラスのインスタンス が生成されることになります。
# TEXT_01 # Matplotlibをインポート import matplotlib.pyplot as plt # Figureを設定 fig = plt.figure(figsize =(6, 6)) # Axesを追加 ax = fig.add_subplot(111) # 目盛線を描画 ax.grid() # Axesにテキストを追加 ax.text(0.2, 0.2, "Python", size = 40, color = "blue") ax.text(0.4, 0.6, "Python", size = 20, color = "red") ax.text(0.8, 0.8, "Python", size = 15, color = "green")
フォント辞書
フォント辞書を作成して Axes.text() の引数に渡すこともできます。
# TEXT_02 # Matplotlibをインポート import matplotlib.pyplot as plt # Figureを設定 fig = plt.figure(figsize =(6, 6)) # Axesを追加 ax = fig.add_subplot(111) # 目盛線を描画 ax.grid() # フォント辞書の作成 fdic = { "family" : "Georgia", "style" : "italic", "size" : 50, "color" : "darkblue", "weight" : "heavy" } # Axesにテキストを追加 ax.text(0.2, 0.4, "Python", fontdict = fdic)
新品価格 | ![]() |

bbox
Axes.text() の引数に bbox を渡すこともできます。
bbox はテキストを囲む図形のスタイルを設定する辞書です。辞書の中で "boxstyle" が指定されると matplotlib.patches.FancyBboxPatchクラスのインスタンス(特殊な形をした図形)が呼び出されることになります。"boxstyle" が指定されない場合は、matplotlib.patches.Rectangleクラスのインスタンス(普通の長方形)が作られます。
# TEXT_03 # Matplotlibをインポート import matplotlib.pyplot as plt # Figureを設定 fig = plt.figure(figsize =(6, 6)) # Axesを追加 ax = fig.add_subplot(111) # 目盛線を描画 ax.grid() # bboxの作成 boxdic = { "facecolor" : "lightgreen", "edgecolor" : "darkred", "boxstyle" : "Round", "linewidth" : 2 } # Axesにテキストを追加 ax.text(0.2, 0.4, "Python", size = 40, bbox = boxdic)
行間隔の設定
Axes.text() の引数に linspacing を渡して改行したときの行間隔(スペース)を設定することができます。
# TEXT_04 # Matplotlibをインポート import matplotlib.pyplot as plt # Figureを設定 fig = plt.figure(figsize =(6, 6)) # Axesを追加 ax = fig.add_subplot(111) # 目盛線を描画 ax.grid() # 行間隔を変えてテキストを表示 ax.text(0.2, 0.8, "linespacing = 1\nPython", size = 20, linespacing = 1) ax.text(0.2, 0.5, "linespacing = 2\nPython", size = 20, linespacing = 2) ax.text(0.2, 0.1, "linespacing = 3\nPython", size = 20, linespacing = 3)
matplotlib.text.Textクラスのインスタンス引数
引数 | 記述方法 |
---|---|
alpha | float |
backgroundcolor | color |
bbox | patches.FancyBboxPatchオブジェクトのプロパティ辞書 |
color | color |
fontfamily | FONTNAME |
fontproperties | font_manager.FontProperties |
fontsize | {数値, "xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"} |
fontstretch | {0-1000の数値, "ultra-condensed", "extra-condensed", "condensed", "semi-condensed", "normal", "semi-expanded", "expanded", "extra-expanded", "ultra-expanded"} |
fontstyle | {"normal", "italic", "oblique"} |
fontweight | {0-1000の数値, "ultralight", "light", "normal", "regular", "book", "medium", "roman", "semibold", "demibold", "demi", "bold", "heavy", "extra bold", "black"} |
horizontalalignment | {"center", "right", "left"} |
in_layout | bool |
linespacing | float (fontsizeに対する倍率) |
position | (float, float) |
rotation | {度数単位の角度, "vertical", "horizontal"} |
rotation_mode | {None, "default", "anchor"} |
url | str |
verticalalignment | {"center", "top", "bottom", "baseline", "center_baseline"} |
コメントを書く