Skip to content

Commit

Permalink
Add options to easily create simple matrix layouts (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanaelbosch authored Apr 19, 2023
1 parent 058cbdd commit deb4ce8
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TuePlots"
uuid = "7dc86ad5-723e-4492-bd0d-d0b58009a4c8"
authors = ["Nathanael Bosch <[email protected]> and contributors"]
version = "0.1.1"
version = "0.2.0"

[deps]
MakieCore = "20f20a25-4f0e-4fdf-b5d1-57303727442b"
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ using CairoMakie, TuePlots, Random
data = cumsum(randn(Xoshiro(2), 4, 201), dims = 2)

# Create a Makie.Theme with correct font, fontsize, and figure size:
T = Theme(TuePlots.SETTINGS[:AISTATS2022];
font = true, fontsize = true, figsize = :half, thinned = true)
T = Theme(
TuePlots.SETTINGS[:AISTATS2022];
font = true,
fontsize = true,
figsize = true,
single_column = true,
thinned = true
)

with_theme(T) do
fig = Figure()
Expand Down Expand Up @@ -69,7 +75,8 @@ theme(:default;
TuePlots.get_plotsjl_theme_kwargs(
TuePlots.SETTINGS[:AISTATS2022];
fontsize = true,
figsize = :half,
figsize = true,
single_column = true,
)...)

plot(data',
Expand Down
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Eyeball = "ce36242b-ad83-4f84-8519-47cd8aeefd60"
LiveServer = "16fef848-5104-11e9-1b77-fb7a48bbb589"
OhMyREPL = "5fb14364-9ced-5910-84b2-373655c76a03"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Expand Down
66 changes: 62 additions & 4 deletions docs/src/example_makie.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Using TuePlots with Makie

!!! info
This tutorial shows how to set fonts, fontsize, and figure sizes with TuePlots.jl.
**The resulting plots might not actually look better in the browser! But that's not really a problem: the goal is to make plots for _publications_.**
Keep this in mind when interpreting the resulting figures.

Let's make a simple series plot of some time series data:

```@example 1
Expand All @@ -8,8 +13,8 @@ CairoMakie.activate!(type = "svg")
data = cumsum(randn(Xoshiro(2), 4, 201), dims = 2)
function plot_data(data; resolution = (487.822, 301.491))
fig = Figure(resolution = resolution)
function plot_data(data)
fig = Figure(px_per_unit=10,pt_per_unit=10)
ax = Axis(fig[1, 1], xlabel = "Time", ylabel = "Quantity of interest")
sp = series!(ax, data, labels = ["label $i" for i in 1:4])
axislegend(ax)
Expand All @@ -21,14 +26,17 @@ plot_data(data)

This plot looks fine here in the documentation, but when you imagine it as a full-width plot in a paper the fonts are much too large.
It's easy to fix these with TuePlots.jl!

## Creating and using Makie Themes with TuePlots.jl
All we have to do is create a Makie `Theme` from one of the available settings:

```@example 1
T = Theme(
TuePlots.SETTINGS[:ICML2022];
font = true,
fontsize = true,
figsize = false, # already chosen correctly in this example
single_column = false,
figsize = true,
thinned = false, # explained later
)
```
Expand All @@ -41,7 +49,11 @@ with_theme(T) do
end
```

Fonts and fontsizes are correct now!
Fonts and fontsizes are correct now, and the figure size fits perfectly into LaTeX:
You can directly include it with `\includegraphics{plot.pdf}`, _without even setting some `[width=\linewidth]`_!


## Combining TuePlots.jl's themes with your custom theme

But we're not quite happy with the result yet.
Let's make the plot more beautiful, e.g. by making lines thinner and reducing the padding.
Expand Down Expand Up @@ -70,3 +82,49 @@ end
```

Voilà! And if you like this setting, you can also reduce padding and line widths with less effort by just setting `thinned=true` when creating the Theme.


## Adjust heights to fit simple subplot layouts

In the example above, TuePlots.jl conveniently set the figure size such that the figure nicely fits into the paper.
But the result does not look as nice when we want to have multiple subplots:

```@example 1
data = cumsum(randn(Xoshiro(2), 4, 201), dims = 2)
function simple_subplots(data)
fig = Figure()
for i in 1:3
ax = Axis(fig[1, i])
sp = series!(ax, data)
end
return fig
end
with_theme(T) do
simple_subplots(data)
end
```

The overall figure size is still the same as above, the width fits the paper and the height is chosen such that the resulting figure has a golden ratio.
But as a result, the individual subplots became too tall.
Instead, we might want to keep the golden ratio of each subplot, and choose the height accordingly.
You can do this by specifying `nrows=1` and `ncols=3` when creating the Makie theme:

```@example 1
T2 = Theme(
TuePlots.SETTINGS[:ICML2022];
font = true,
fontsize = true,
figsize = true,
nrows=1,
ncols=3,
thinned = true,
)
with_theme(T2) do
simple_subplots(data)
end
```

There we go!
This way, each individual subplot has the golden ratio.
2 changes: 1 addition & 1 deletion docs/src/example_plotsjl.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ theme(:default;
TuePlots.SETTINGS[:ICML2022];
font = true,
fontsize = true,
figsize = :full,
figsize = true,
)...)
plot(
data',
Expand Down
2 changes: 0 additions & 2 deletions src/TuePlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ using MakieCore

const POINTS_PER_INCH = 72.27
const GOLDEN_RATIO = (5.0^0.5 - 1.0) / 2.0
width_to_resolution(width) =
(width * POINTS_PER_INCH, width * POINTS_PER_INCH * GOLDEN_RATIO)

const FONTSIZE_REDUCTION = 1
const SMALL_FONTSIZE_OFFSET = 2
Expand Down
26 changes: 19 additions & 7 deletions src/to_makie.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
"""
MakieCore.Theme(setting::TuePlotsSetting;
font = true, fontsize = true, figsize = :full, thinned = trues+
font = true, fontsize = true, figsize = :full, thinned = true
Make a Makie `Theme` out of the `TuePlotsSetting`.
"""
function MakieCore.Theme(
setting::TuePlotsSetting;
font = true, fontsize = true, figsize = :full, thinned = true,
font = true,
fontsize = true,
figsize = true,
single_column = false,
subplot_height_to_width_ratio = GOLDEN_RATIO,
nrows = 1, ncols = 1,
thinned = true,
)
theme = MakieCore.Theme()

Expand Down Expand Up @@ -38,11 +44,17 @@ function MakieCore.Theme(
)
end

if figsize == :full
resolution = width_to_resolution(setting.width)
theme = merge(theme, MakieCore.Theme(resolution = resolution))
elseif figsize == :half && setting.width_half isa Number
resolution = width_to_resolution(setting.width_half)
if figsize
width = if !single_column
setting.width
else
if !(setting.width_half isa Number)
error("`single_column` not supported for this setting")
end
setting.width_half
end
height = width * subplot_height_to_width_ratio * nrows / ncols
resolution = (width * POINTS_PER_INCH, height * POINTS_PER_INCH)
theme = merge(theme, MakieCore.Theme(resolution = resolution))
end

Expand Down
25 changes: 17 additions & 8 deletions src/to_plotsjl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ Build a named tuple to be used with `Plots.theme` out of the `TuePlotsSetting`.
"""
function get_plotsjl_theme_kwargs(
setting::TuePlotsSetting;
font = false, fontsize = true, figsize = :full, thickness_scaling = true,
font = false,
fontsize = true,
figsize = true,
single_column = false,
subplot_height_to_width_ratio = GOLDEN_RATIO,
nrows = 1, ncols = 1,
thickness_scaling = true,
)
kwargs = (;)

Expand All @@ -30,14 +36,17 @@ function get_plotsjl_theme_kwargs(
)
end

if figsize == :full
resolution = width_to_resolution(setting.width)
kwargs = merge(kwargs, (; size = resolution))
elseif figsize == :half
if !(setting.width_half isa Number)
raise(ArgumentError("`:half` figsize is not available for this setting."))
if figsize
width = if !single_column
setting.width
else
if !(setting.width_half isa Number)
error("`single_column` not supported for this setting")
end
setting.width_half
end
resolution = width_to_resolution(setting.width_half)
height = width * subplot_height_to_width_ratio * nrows / ncols
resolution = (width * POINTS_PER_INCH, height * POINTS_PER_INCH)
kwargs = merge(kwargs, (; size = resolution))
end

Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import MakieCore, Plots
@testset "$k" for k in setting_keys
s = TuePlots.SETTINGS[k]
@test MakieCore.Theme(s) isa MakieCore.Theme
if s.width_half isa Number
@test_nowarn MakieCore.Theme(s; single_column = true)
else
@test_throws ErrorException MakieCore.Theme(s; single_column = true)
end
end
end

Expand Down

2 comments on commit deb4ce8

@nathanaelbosch
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/81889

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" deb4ce8e743fcee27012fa285c6600dd67644c9b
git push origin v0.2.0

Please sign in to comment.