【问题标题】:How to place text above hatched zone using matplotlib?如何使用 matplotlib 在阴影区域上方放置文本?
【发布时间】:2015-06-29 16:48:07
【问题描述】:

如何使用 matplotlib 在阴影区域上方放置文本? 一个例子如下所示。 下面是我的示例代码,便于理解

from pylab import *
fig=figure()
x=array([0,1])
yc=array([0.55,0.48])


yhc=array([0.55,0.68])

yagg=array([0.45,0.48])

plot(x,yc,'k-',linewidth=1.5)
plot(x,yhc,'k-',linewidth=1.5)
plot(x,yagg,'k-',linewidth=1.5)

xticks(fontsize = 22)
yticks(fontsize = 22)
ylim(0,1)
ax=axes()


p=fill_between(x, yc, yhc,color="none")

from matplotlib.patches import PathPatch 
for path in p.get_paths(): 
    p1 = PathPatch(path, fc="none", hatch="/") 
    ax.add_patch(p1) 
    p1.set_zorder(p.get_zorder()-0.1) 

props = dict(boxstyle='round',facecolor='white', alpha=1,frameon='false')
text(0.6, 0.55, 'hi',fontsize=22)
fig.savefig('vp.png', transparent=True,bbox_inches='tight')

阴影区域确实很难看到文字。

【问题讨论】:

    标签: python text matplotlib


    【解决方案1】:

    您可以为text 命令提供一个具有自己背景的边界框 (bbox)。这是一个简单的更改:

    text(0.6, 0.55, 'hi',fontsize=22, bbox=dict(facecolor='white', alpha=0.9, ))
    

    这会像这样改变您的示例情节:

    如果您查找 bbox,您可以弄清楚如何关闭 bbox 周围的边框,如果它与您的孵化冲突。或者使它成为一个roundrec等。这是各种文本边界框样式的example

    【讨论】:

    • 谢谢,但是如果你这样做,你能去掉盒子的框架吗?还是没有选择,只能接受?
    • text(0.6, 0.55, 'hi',fontsize=22, bbox=dict(facecolor='white', edgecolor='none', alpha=0.9, )) 将没有可见的框架。
    猜你喜欢
    • 2012-01-07
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    相关资源
    最近更新 更多