将数据坐标与像素坐标相结合

时间:2013-12-19 23:10:07

标签: python matplotlib

我正在尝试在matplotlib图上的特定位置添加注释。我想在数据坐标中指定一个位置,然后将其偏移一个像素(或点)坐标的距离。好像我应该说

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

我希望这会将注释定位在距 x = wavelength y = intensity左侧六个像素和六个像素的位置。相反,我得到一个例外:

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'

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我无法重现您的错误,但是您尝试做的事情可以更直接地完成:

fig, ax = plt.subplots()

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

不需要你自己用dpi进行缩放。

将来,您可以在问题中发布最小的可运行示例吗?