Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing backend specific functionality with Julia Plots

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.

like image 628
tim Avatar asked Mar 09 '17 15:03

tim


People also ask

What is backend in Julia?

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.

What is GR () in Julia?

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.


2 Answers

No - the plot is a Plots object, not a PyPlot object. In your specific example you can do plot(sin, xmirror = true).

like image 88
Michael K. Borregaard Avatar answered Oct 11 '22 17:10

Michael K. Borregaard


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.

like image 44
Fred Schoen Avatar answered Oct 11 '22 18:10

Fred Schoen