Plots
is simple and powerful but sometimes I would like to have a little bit more control over individual elements of the plot to fine-tune its appearance.
Is it possible to update the plot object of the backend directly?
E.g., for the default pyplot
backend, I tried
using Plots
p = plot(sin)
p.o[:axes][1][:xaxis][:set_ticks_position]("top")
but the plot does not change. Calling p.o[:show]()
afterwards does not help, either.
In other words: Is there a way to use the PyPlot
interface for a plot that was initially created with Plots
?
Edit:
The changes to the PyPlot
object become visible (also in the gui) when saving the figure:
using Plots
using PyPlot
p = Plots.plot(sin, top_margin=1cm)
gui() # not needed when using the REPL
gca()[:xaxis][:set_ticks_position]("top")
PyPlot.savefig("test.png")
Here, I used p.o[:axes][1] == gca()
. One has to set top_margin=1cm
because the plot area is not adjusted automatically (for my actual fine-tuning, this doesn't matter).
This also works for subsequent updates as long as only the PyPlot
interface is used. E.g., after the following commands, the plot will have a red right border in addition to labels at the top:
gca()[:spines]["right"][:set_color]("red")
PyPlot.savefig("test.png")
However, when a Plots
command like plot!(xlabel="foo")
is used, all previous changes made with PyPlot
are overwritten (which is not suprising).
The remaining question is how to update the gui interactively without having to call PyPlot.savefig
explicitly.
Backends are the lifeblood of Plots, and the diversity between features, approaches, and strengths/weaknesses was one of the primary reasons that I started this package. For those who haven't had the pleasure of hacking on 15 different plotting APIs: first, consider yourself lucky.
GR is the default backend for Plots . To explicitly specify the GR backend, you can use: using Plots gr() The demos are generated from Plots. _examples . Empty demos are features that this backend does not support.
No - the plot is a Plots object, not a PyPlot object. In your specific example you can do plot(sin, xmirror = true
).
I'm trying to do the same but didn't find a solution to update an existing plot. But here is a partial answer: you can query information from the PyPlot axes object
julia> Plots.plot(sin, 1:4)
julia> Plots.PyPlot.plt[:xlim]()
(1.0,4.0)
julia> Plots.plot(sin, 20:24)
julia> ax = Plots.PyPlot.plt[:xlim]()
(20.0,24.0)
and it gets updated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With