2013-12-19 65 views
0

我試圖在matplotlib圖上的特定位置添加註釋。我想在數據座標中指定一個位置,然後用像素(或點)座標中的距離來偏移該位置。好像我應該可以說結合數據座標與像素座標

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

我希望它可以較低的六個像素和六個像素定位註釋的X = wavelength左側,ÿ = 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

我無法重現你的錯誤,但是你正在嘗試做可以更直接完成:

fig, ax = plt.subplots() 

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

用了,需要你自己做的縮放與DPI。

未來,您可以發佈最小的可運行示例與您的問題?