我正在使用 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()