使用 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.
换句话说:有没有办法将 PyPlot
接口用于最初使用 Plots
创建的绘图?
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).
剩下的问题是如何在不显式调用 PyPlot.savefig
的情况下以交互方式更新 gui.
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屋!