JuliaPy / PyPlot.jl

Plotting for Julia based on matplotlib.pyplot
https://github.com/JuliaPy/PyPlot.jl
MIT License
478 stars 88 forks source link

PyPlot not updating figure with annotate, but prints the new annotations over the old #308

Closed thatlittleboy closed 7 years ago

thatlittleboy commented 7 years ago

I am using Atom+Juno (v0.3.0), PyPlot (v 2.3.2).

Consider the following minimal example:

using PyPlot
  x = linspace(0,10,100)
  y = x.^2
  fig1 = figure("Figure 1 plot",figsize=(5,5))
  ax = gca()
  p = plot(x,y,ls="-.",label="Test plot")
  axis("tight")
title(L"Example Plot of $y=x^2$")
  legend(loc="upper left",fancybox="true")
annotate("Figure Top Right",
    xy=[1;1],
    xycoords="figure fraction",
    xytext=[0,0],
    textcoords="offset points",
    ha="right",
    va="top")

Running this, I obtain a figure in a window called "Figure 1 plot" as expected. Keeping the figure window open, I then make a change to the annotate argument, say: "Figure Top Right" -> "Figure New Right" and run the annotate(...) block.

Instead of obtaining an updated/refreshed figure with "Figure New Right" on the top right of the figure, I see that "Figure New Right" is printed over the old "Figure Top Right", like so:

figure_1_plot

How should I set up my code so that the PyPlot figure updates, not prints over the old settings? Note that I know that I can run the whole code again (to generate a new figure window, from scratch), but that becomes a hassle when my plotting code is very long, and unnecessary imo.


Ninja edit:

According to the matplotlib documentation, the draw() function "redraw(s) the current figure. This is used to update a figure that has been altered, but not automatically re-drawn".

Even adding

<previous code>
fig1[:canvas][:draw]()

below the annotation block from before, the result is still the same. I'm probably misunderstanding the use of the draw function here then..

stevengj commented 7 years ago

draw doesn't erase the old annotation. (In fact, it doesn't really do anything here.)

Calling annotate twice just adds two annotations. This is not a bug, this is how matplotlib works. If you want to erase the annotation, the simplest thing is to clear the figure and draw a new one.

stevengj commented 7 years ago

You can also call the remove method of the old annotation: https://stackoverflow.com/questions/12222397/python-and-remove-annotation-from-figure