Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: How to 'anchor' text to be always visible and set_text method Matplotlib
    primarykey
    data
    text
    <p>I have this code:</p> <pre><code>from pylab import * from mpl_toolkits.axes_grid.anchored_artists import AnchoredText import numpy as np import matplotlib.pylab as plt fig = plt.figure() ax = fig.add_subplot(111) ann = AnchoredText('If you zoom in or out, i stay here\nbut can you update this text?', prop=dict(size=8), frameon=True, loc=2, ) ann.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") ax.add_artist(ann) delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = (Z1 - Z2) * 10 plt.contourf(X, Y, Z) plt.show() </code></pre> <p>Is there a way to anchor text to be always visible but to also be able to use a <code>set_text</code> method like in text objects to update the containing text? Here i used AnchoredText which is anchored perfectly but i cannot find a method to change the text it contains. In <a href="http://matplotlib.org/users/annotations_guide.html?highlight=anchoredtext#placing-artist-at-the-anchored-location-of-the-axes" rel="nofollow">AnchoredText</a> Matplotlib documentation i cannot find such a method available. If it cannot be done with AnchoredText can it be done with a simple text object?</p> <p><strong>EDIT</strong></p> <p>I accepted the solution from <strong>David Zwicker</strong> and here is a working example for anyone in need of it:</p> <pre><code>from pylab import * from mpl_toolkits.axes_grid.anchored_artists import AnchoredText import numpy as np import matplotlib.pylab as plt import random fig = plt.figure() ax = fig.add_subplot(111) ann = AnchoredText('If you zoom in or out, i stay here\nbut can you update this text?$4.1f$ km s$^{-1}$', prop=dict(size=15), frameon=True, loc=2, ) ann.patch.set_boxstyle("round,pad=0.,rounding_size=0.2") ax.add_artist(ann) delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = (Z1 - Z2) * 10 plt.contourf(X, Y, Z) a = ["hhhhh", "qqqqq", "aaaaaaa","ttttt","yyyyy","oooooooo"] def setanntext(self): ann.txt.set_text(random.choice(a)) plt.draw() buttonax = plt.axes([0.7, 0.05, 0.1, 0.1]) button = Button(buttonax, 'set text',color='0.85', hovercolor='0.95') button.on_clicked(setanntext) plt.show() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload