简体   繁体   English

结合数据坐标和像素坐标

[英]Combining data coordinates with pixel coordinates

I'm trying to add annotations at specific positions on a matplotlib figure. 我正在尝试在matplotlib图的特定位置添加注释。 I want to specify a position in data coordinates and then offset that by a distance in pixel (or point) coordinates. 我想在数据坐标中指定一个位置,然后将其偏移一个像素(或点)坐标中的距离。 It seems like I should be able to say 看来我应该可以说

offset = ScaledTranslation(-6/72, -6/72, self.figure.dpi_scale_trans)
self.axes.annotate(u'\u00d7', xy=(wavelength, intensity), xycoords=offset)

I would expect this to position the annotation six pixels lower and six pixels to the left of x = wavelength , y = intensity . 我希望这样可以将注释的位置放低x = wavelengthy = intensity ,低6个像素,向左6个像素。 Instead I get an exception: 相反,我得到一个例外:

Exception in thread Thread-7:
Traceback (most recent call last):
  [...]
  File "/Users/bdesham/Projects/New GUI/application/views/spectrum_view.py", line 117, in update_graph_display
    self.figure.canvas.draw()
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/backends/backend_wxagg.py", line 44, in draw
    FigureCanvasAgg.draw(self)
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/figure.py", line 1034, in draw
    func(*args)
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/text.py", line 2002, in draw
    xy_pixel = self._get_position_xy(renderer)
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/text.py", line 1640, in _get_position_xy
    return self._get_xy(renderer, x, y, self.xycoords)
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/text.py", line 1457, in _get_xy
    if s1 == 'data':
  File "/Users/bdesham/Code/virtualenv/lib/python2.7/site-packages/matplotlib/transforms.py", line 1562, in __eq__
    if other.is_affine:
AttributeError: 'str' object has no attribute 'is_affine'

What am I doing wrong? 我究竟做错了什么?

I can't reproduce your error, however what you are trying to do can be done much more directly: 我无法重现您的错误,但是您可以尝试直接执行以下操作:

fig, ax = plt.subplots()

ax.plot([.5], [.5], 'bo')
ax.annotate(u'tt', xy=(.5, .5), xytext=(-6, -6), textcoords='offset points')

with out the need for you to do the scaling with dpi yourself. 无需您自己使用dpi进行缩放。

In the future, can you post minimal runable examples with your question? 将来,您可以在问题中发布最少的可运行示例吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM