【发布时间】:2020-09-30 10:28:48
【问题描述】:
我正在使用 matplotlib plt.text 函数将文本框添加到我的直方图中。在bbox 参数中,我指定boxstyle、facecolor、edgecolor 和alpha。但是,当我运行它并显示绘图时,盒子的面和它的边缘都相对于alpha 变得透明。这会稍微改变两种颜色,我想保持我的边缘稳固。有谁知道设置 alpha 的方法,使边框保持不透明(alpha=1),但 facecolor 可以设置为任何值(alpha = [0,1])。
谢谢。
import matplotlib.pyplot as plt
import statistics
fig, ax = plt.subplots()
ax.hist(x=data, bins='auto', color='#0504aa', alpha=0.7, rwidth=0.85)
plt.grid(axis='y', alpha=0.75)
textstr = '\n'.join((
r'$n=%.2f$' % (len(data), ),
r'$\mu=%.2f$' % (round(statistics.mean(data), 4), ),
r'$\mathrm{median}=%.2f$' % (round(statistics.median(data), 4), ),
r'$\sigma=%.2f$' % (round(statistics.pstdev(data), 4), )))
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=dict(boxstyle='square,pad=.6',facecolor='lightgrey', edgecolor='black', alpha=0.7))
plt.show()
【问题讨论】:
标签: python matplotlib plot text alpha