How to set the default attributes of Plots?
Asked Answered
D

3

7

In Julia plotting with Plots, I know how to set the various attributes when using plot() (Attributes).

I want to know how to set the default attributes, so that I don't need to set them every time.

For instance, I want to change the font-family to another one, or show the minor ticks always.

I googled but I can not find the way.

Diamonddiamondback answered 1/1, 2021 at 13:57 Comment(0)
P
10

This is the way:

Use the default() function like so

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)

Taken from the documentation here. I know the Plots.jl docs can be a bit tricky to navigate due to their size, but in this case I just typed default into the doc search box.

Note that when using the default function you don't supply the keyword args in subsequent calls to plot, unless of course you want to change away from your newly specified defaults.

Since you are asking for a way to save defaults across sessions, I will also point you to this additional tip from the 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)

So here you are defining the new defaults as a dictionary which is used as an environment variable, which allows setting defaults before loading Plots (and thus without having the default function available).

Philo answered 2/1, 2021 at 7:41 Comment(0)
O
2

Store your defaults in a variable and overwrite whenever needed.

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

plot(rand(5);defs...,linecolor=:red)
Octahedral answered 1/1, 2021 at 14:58 Comment(2)
Thank you. Is there a way that I can store the defs in a file, so that I can just load it? For example, in jupyter/python/matplotlib, I can put a file in ~/.matplotlib/stylelib/mystyle.mplstyle. I can just run plt.style.use(mystyle) to use it.Diamonddiamondback
None of these suggestions seems to allow setting vector quantities whereby one might want to set colors, as in color = [:red :blue: green]. the defs = method fails as does setting the environment variable PLOTS_DEFAULTS.Wysocki
S
0

My solution is to create an empty plot with all "default" attributes and then just add necessary layers each time to this empty plot.

Swirly answered 13/9, 2022 at 21:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.