2017-05-08 59 views
3

我试图使用matplotlib从Python 3中,以实现类似的图像如下图所示:的Python 3 - 垂直线内matplitlib文本

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

如果我可以把点线的中间这段文字,这将是巨大的。

回答

4

这不是一个解决方案,而是更多的解决方法。您可以尝试设置文本的背景颜色或使用特定颜色添加边界框,以避免线条被遮挡。这将使文本显示为内联。

你会实现这个像这样:

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

enter image description here

plt.text(0.5, 0.5, 'Some Text', ha='center', va='center',rotation='vertical', bbox={'facecolor':'white', 'pad':5}) 

enter image description here

当然,当这条线被上直方图覆盖出现问题不同的颜色,你会那么必须将背景或框的颜色与直方图相匹配。这将显示类似这样:

enter image description here

+0

谢谢你的答案:) – n00b