0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

matplotlib: highlight-text 文章の中で部分的にフォントスタイルを変える

Last updated at Posted at 2025-11-07

一つの文章の中でフォントスタイルを変えることはできないかと調べていて見つけたのがこれ。

https://github.com/znstrider/highlight_text

サンプルコードは以下の通り。< > で囲った文字列のプロパティを順に指定することにより、フォントのスタイルを変えることができる。

#https://github.com/znstrider/highlight_text
import matplotlib.pyplot as plt
from highlight_text import ax_text
import numpy as np


def main():
    fsz=10
    xmin,xmax,dx= 0,2,0.5
    ymin,ymax,dy= 0,2,0.5
    iw,ih=5,5
    plt.figure(figsize=(iw,ih),facecolor='w')
    plt.rcParams['font.size']=fsz
    plt.rcParams['font.family']='sans-serif'
    plt.xlim([xmin,xmax])
    plt.ylim([ymin,ymax])
    plt.xticks(np.arange(xmin,xmax+dx,dx))
    plt.yticks(np.arange(ymin,ymax+dy,dy))
    plt.xlabel('x_axis')
    plt.ylabel('y_axis')

    text = 'Highlight-text can change the font property\n \
    of a part in a sentence as shown below.\n \
    This is <Red>. \n \
    This is <Lime>.\n \
    This is <Blue>.'

    ax_text(
        x=1,
        y=1,
        s=text,
        ha='center',
        va='center',
        fontsize=fsz,
        color='#000000',
        highlight_textprops=[
            {"color": "#ff0000", 'fontweight': 'bold'},
            {"color": "#00ff00", 'fontweight': 'bold'},
            {"color": "#0000ff", 'fontweight': 'bold'}]
        )

    plt.show()
    
#---------------
# Execute
#---------------
if __name__ == '__main__': main()

以 上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?