【问题标题】:Adjusting Text background transparency调整文本背景透明度
【发布时间】:2014-07-05 00:03:08
【问题描述】:

我正在尝试在 matplotlib 图形上放置一些带有背景的文本,文本和背景都是透明的。以下代码

import numpy as np
import matplotlib.pyplot as plt
plt.figure()
ax = plt.subplot(111)
plt.plot(np.linspace(1,0,1000))
t = plt.text(0.03,.95,'text',transform=ax.transAxes,backgroundcolor='0.75',alpha=.5)
plt.show()

使文本相对于文本的背景半透明,但背景相对于它在图中遮挡的线条完全不透明。

t.figure.set_alpha(.5)

t.figure.patch.set_alpha(.5)

也不要这样做。

【问题讨论】:

    标签: python text matplotlib plot transparency


    【解决方案1】:

    传递给plt.text()alpha 将改变文本字体的透明度。要更改背景,您必须使用 Text.set_bbox() 更改 alpha

    t = plt.text(0.5, 0.5, 'text', transform=ax.transAxes, fontsize=30)
    t.set_bbox(dict(facecolor='red', alpha=0.5, edgecolor='red'))
    #changed first dict arg from "color='red'" to "facecolor='red'" to work on python 3.6
    

    【讨论】:

    • 你也可以props = dict(...)ax.text(... bbox=props)
    猜你喜欢
    • 2019-07-27
    • 1970-01-01
    • 2016-09-17
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多