2014-05-16 2 views
6

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) 

또한 트릭을하지 않습니다.

답변

12

alphaplt.text()으로 전달되면 텍스트 글꼴의 투명도가 변경됩니다. 배경을 변경하려면 Text.set_bbox()를 사용하여 alpha을 변경해야합니다 : 당신은 또한`소품 = DICT (...)`와`ax.text (... 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 

enter image description here

+1

)' – endolith