如何设置Plots的默认属性?

How to set the default attributes of Plots?

在使用Plots绘图的Julia中,我知道在使用plot()时如何设置各种属性(Attributes)。

我想知道如何设置默认属性,这样我就不需要每次都设置它们了。

例如,我想将 font-family 更改为另一个,或者始终显示小刻度。

我用谷歌搜索,但找不到路。

将默认值存储在变量中并在需要时覆盖。

defs = (linestyle=:dash, linewidth=5, linecolor=:green)

plot(rand(5);defs...,linecolor=:red)

是这样的:

像这样使用default()函数

using Plots
default(titlefont = (20, "times"), legendfontsize = 18, guidefont = (18, :darkgreen), tickfont = (12, :orange), guide = "x", framestyle = :zerolines, yminorgrid = true)
plot([sin, cos], -2π, 2π, label = ["sin(θ)" "cos(θ)"], title = "Trigonometric Functions", xlabel = "θ", linewidth = 2, legend = :outertopleft)

摘自文档 here。我知道 Plots.jl 文档由于其大小而难以导航,但在这种情况下,我只是在文档搜索框中输入 default

请注意,在使用 default 函数时,您不会在随后对 plot 的调用中提供关键字参数,当然除非您想更改新指定的默认值。

由于您要求的是一种跨会话保存默认值的方法,因此我还会向您指出来自 installation docs 的额外提示:

You can override standard default values in your ~/.julia/config/startup.jl file: PLOTS_DEFAULTS = Dict(:markersize => 10, :legend => false, warn_on_unsupported = false)

所以在这里您将新的默认值定义为用作环境变量的字典,它允许在加载图之前设置默认值(因此没有可用的 default 函数)。