使用Julia Plots访问后端特定功能 [英] Accessing backend specific functionality with Julia Plots
问题描述
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?
例如,对于默认的pyplot
后端,我尝试了
E.g., for the default pyplot
backend, I tried
using Plots
p = plot(sin)
p.o[:axes][1][:xaxis][:set_ticks_position]("top")
,但是情节没有改变.此后再调用p.o[:show]()
也无济于事.
but the plot does not change. Calling p.o[:show]()
afterwards does not help, either.
换句话说:对于最初使用Plots
创建的图,是否可以使用PyPlot
接口?
In other words: Is there a way to use the PyPlot
interface for a plot that was initially created with Plots
?
保存图形时,对PyPlot
对象的更改变得可见(也在gui中):
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")
在这里,我使用了p.o[:axes][1] == gca()
.必须设置top_margin=1cm
,因为绘图区域不会自动调整(对于我的实际微调,这无关紧要).
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).
这也适用于后续更新,只要仅使用PyPlot
界面即可.例如,在执行以下命令后,除了顶部的标签外,该图还将具有红色的右边框:
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")
但是,当使用像plot!(xlabel="foo")
这样的Plots
命令时,以前用PyPlot
进行的所有更改都会被覆盖(这并不令人惊讶).
However, when a Plots
command like plot!(xlabel="foo")
is used, all previous changes made with PyPlot
are overwritten (which is not suprising).
剩下的问题是如何以交互方式更新gui,而不必显式调用PyPlot.savefig
.
The remaining question is how to update the gui interactively without having to call PyPlot.savefig
explicitly.
推荐答案
我试图做同样的事情,但是没有找到更新现有图的解决方案.但这是部分答案:您可以从PyPlot轴对象中查询信息
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)
它会更新.
这篇关于使用Julia Plots访问后端特定功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!