Python 3 matplitlib文本在垂直lin内

2024-04-16 19:35:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python 3中的matplotlib来实现如下所示的效果:

enter image description here

有人问过类似的问题here,但公认的答案不足以满足我的需要。我需要在虚线中间添加文本(我可以用plt.axvline()函数绘制)。在

这是我试过的

import matplotlib.pylab as plt
plt.hist(some_data)
plt.axvline(0.5, color='k', linestyle='--')
plt.text(0.5, 0.5, 'Some Text', ha='center', va='center',rotation='vertical')
plt.show()

如果我能把这篇文章放在虚线中间,那就太好了。在


Tags: 函数答案文本importherematplotlibas绘制
1条回答
网友
1楼 · 发布于 2024-04-16 19:35:03

这不是一个解决方案,而是一个解决办法。您可以尝试设置文本的背景颜色,或添加一个边框,使其具有特定的颜色,使该行黯然失色。这将使文本显示为内联。在

您可以这样实现:

plt.text(0.5, 0.5, 'Some Text', ha='center', va='center',rotation='vertical', backgroundcolor='white')

enter image description here

以及

^{pr2}$

enter image description here

当然,当这条线被覆盖在一个不同颜色的柱状图上时,会出现一个问题,然后你必须将背景或方框的颜色与直方图相匹配。这将显示如下内容:

enter image description here

相关问题 更多 >