I have to plot some data and some vertical lines to delimit interesting intervals and then I would like to add some labels to that using text. I can not entirely avoid the labels overlapping with the data or the vertical lines, so I decided to put a bbox around the text to keep it readable. My problem is that I am not able to align it centrally within this box and this is clearly visible and quite annoying in my opinion.

我需要画一些数据和一些竖线来分隔有趣的区间然后我想用文本添加一些标签。我不能完全避免标签与数据或竖线重叠,所以我决定在文本周围放置一个bbox以保持它的可读性。我的问题是,我不能把它集中在这个盒子里,这在我看来是显而易见的,而且很烦人。

I'm doing something like this:

我这样做:

import numpy
import matplotlib
import matplotlib.pyplot as plt

fig=plt.figure()
plot=fig.add_subplot(111)
x=numpy.linspace(1,10,50)
y=numpy.random.random(50)
plot.plot(x,y)
plot.text(4.5,.5,'TEST TEST',\
          bbox={'facecolor':'white','alpha':1,'edgecolor':'none','pad':1})
plot.axvline(5,color='k',linestyle='solid')
plt.show()

Which creates the following plot:

这就产生了以下情节:

It is quite apparent, that the text is not centered in its bbox. How can I change this? I've spent quite some time on Google but I could not find anything.

很明显,文本不是以它的bbox为中心的。我怎样才能改变这个呢?我花了不少时间在谷歌上,但我什么也找不到。

EDIT:

编辑:

Thanks for the suggestions so far.

谢谢你的建议。

This suggests that what I see is actually desired behavior. Apparently the bbox in new versions of matplotlib is chosen taking into account the possible maximum descent of the text it contains (the descent of 'g').

这表明我所看到的实际上是想要的行为。显然,新版本的matplotlib中的bbox考虑了它所包含的文本可能的最大下降(“g”的下降)。

When a 'g' appears in the text, this does indeed look good: Unfortunately in my case there is no 'g' or anything with a similar descent. Does anyone have any further ideas?

当“g”出现在文本中时,这确实看起来很好:不幸的是,在我的情况下,没有“g”或任何类似的下降。有人有进一步的想法吗?

1 个解决方案

#1


3

Use the text properties ha and va:

使用文本属性ha和va:

plot.text(5.5,.5,'TEST TEST TEST TEST',
          bbox={'facecolor':'white','alpha':1,'edgecolor':'none','pad':1},
          ha='center', va='center') 

To check, draw lines in the center of your plot:

为了检查,在你的情节中心画线:

plot.axvline(5.5,color='k',linestyle='solid')
plot.axhline(0.5,color='k',linestyle='solid')

更多相关文章

  1. 机器学习之路:python 文本特征提取 CountVectorizer, TfidfVector
  2. Python Module之textwrap - 文本段落格式编排
  3. Python -在文本文件中添加日期戳
  4. python在文本开头插入一行
  5. python的PIL绘制多行文本的图像。
  6. linux vim下如何让类似txt的文本,呈现出某种语言的高亮形式?
  7. 文本文件到字符串数组?
  8. 文本处理小工具SED用法
  9. 个人网站对xss跨站脚本攻击(重点是富文本编辑器情况)和sql注入攻击

随机推荐

  1. Amoeba实现mysql主从读写分离1
  2. 查询从wordnet mysql获取反义词?
  3. Mysql 命令行快速导入数据
  4. Qt通过ODBC连接SQL Server2008实践总结
  5. Oracle SQL Developer是一个与c#一起使用
  6. mysql中insert...select引发的死锁
  7. SQL对多个列分别进行合并的操作,100分送
  8. [置顶] SQL Server 日期相关
  9. 2017年末大数据Linux,Oracle,MySQL,J2EE(
  10. [置顶] mysql七表查询实例(二)