调整文本背景透明度

编程入门 行业动态 更新时间:2024-10-27 10:20:40
本文介绍了调整文本背景透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm trying to put some text with a background on a matplotlib figure, with the text and background both transparent. The following code

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()

使文本相对于文本背景为半透明,但背景相对于其在图中遮盖的线并不完全透明.

makes the text semi-transparent relative to the text's background, but the background isn't at all transparent relative to the line it obscures in the figure.

t.figure.set_alpha(.5)

t.figure.patch.set_alpha(.5)

也不要耍花招.

推荐答案

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

The alpha passed to plt.text() will change the transparency of the text font. To change the background you have to change the alpha using Text.set_bbox():

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

更多推荐

调整文本背景透明度

本文发布于:2023-06-07 22:09:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:透明度   文本   背景

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!