You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改offsetbox TextArea的字体颜色及指定文本颜色?

How to Modify Text Color in matplotlib.offsetbox.TextArea

Great questions! Working with matplotlib's offsetbox components can feel a bit opaque at first, so let's break down solutions to both your problems step by step.


1. Change the Font Color of an Entire TextArea

The TextArea class accepts a textprops parameter where you can define font-related styles (including color) either during initialization or after creating the box.

Example Code:

import matplotlib.pyplot as plt
from matplotlib.offsetbox import TextArea, AnnotationBbox

fig, ax = plt.subplots()

# Option 1: Set color when creating the TextArea
red_text = TextArea("This is full red text", textprops={'color': 'crimson', 'fontsize': 14})
red_box = AnnotationBbox(red_text, (0.5, 0.7), frameon=False)
ax.add_artist(red_box)

# Option 2: Modify color after creating the TextArea
blue_text = TextArea("This is updated to blue", fontsize=14)
blue_text.set_textprops(color='darkblue')  # Update the text color post-creation
blue_box = AnnotationBbox(blue_text, (0.5, 0.5), frameon=False)
ax.add_artist(blue_box)

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.show()

This works because textprops passes styling directly to the underlying matplotlib text object, so you can also set other properties like font size, weight, or family here.


2. Change Color of Specific Text Segments

A single TextArea uses uniform styling for all its text, so to color specific parts, you have two reliable approaches:

The RichText offsetbox component supports HTML-style markup, making it easy to apply color (and other styles) to specific text segments without extra work.

Example Code:

import matplotlib.pyplot as plt
from matplotlib.offsetbox import RichText, AnnotationBbox

fig, ax = plt.subplots()

# Use HTML <span> tags to target specific text
styled_text = RichText(
    "Plain text <span style='color:forestgreen'>green highlighted text</span> back to plain",
    textprops={'fontsize': 14}
)
text_box = AnnotationBbox(styled_text, (0.5, 0.5), frameon=False)
ax.add_artist(text_box)

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.show()

You can also add bold, italic, or font size changes within the <span> tags for more customization.

Method 2: Combine Multiple TextAreas with HPacker

If you prefer not to use HTML markup, you can create separate TextArea elements for each colored segment and pack them horizontally to look like a single block of text.

Example Code:

import matplotlib.pyplot as plt
from matplotlib.offsetbox import TextArea, HPacker, AnnotationBbox

fig, ax = plt.subplots()

# Create individual text segments with different colors
segment1 = TextArea("Hello ", textprops={'color': 'black', 'fontsize': 14})
segment2 = TextArea("colorful", textprops={'color': 'purple', 'fontsize': 14, 'fontweight': 'bold'})
segment3 = TextArea(" world!", textprops={'color': 'black', 'fontsize': 14})

# Pack segments horizontally with no spacing between them
combined_box = HPacker(
    children=[segment1, segment2, segment3],
    align="center",
    pad=0,
    sep=0
)

text_box = AnnotationBbox(combined_box, (0.5, 0.5), frameon=False)
ax.add_artist(text_box)

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.show()

This method gives you fine-grained control over each text segment's position, but it's more verbose than using RichText.


内容的提问来源于stack exchange,提问作者Ashinori

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:55:13