How do I make x and y axes thicker with Plots (Julia)?
Asked Answered
U

2

6

How can I make the lines for the x- and y-axes thicker in Julia Plots? Is there a simple way to achieve this?

MWE:

using Plots

Nx, Ny = 101,101
x = LinRange(0, 100, Nx)
y = LinRange(0, 100, Ny)

foo(x,y; x0=50, y0=50, sigma =1) = exp(- ((x-x0)^2 + (y-y0)^2)/(2*sigma^2)  )
NA = [CartesianIndex()]  # for "newaxis"
Z = foo.(x[:,NA], y[NA,:], sigma=10);

hm = heatmap(x, y, Z, xlabel="x", ylabel="y", c=cgrad(:Blues_9), clim=(0,1))
plot(hm, tickfontsize=10, labelfontsize=14)

Leads to: Current output

The posts I found so far suggested that this was not possible:

  1. https://discourse.julialang.org/t/plots-jl-modify-frame-thickness/24258/4
  2. https://github.com/JuliaPlots/Plots.jl/issues/1099

It this still so?

The actual code for my plot is much longer. I would not like to rewrite all of it in a different plot library.

Utilitarianism answered 27/11, 2021 at 9:33 Comment(0)
H
4

Currently, there does not seem to be an attribute for axes thickness in Plots.jl.

As a workaround, you may use the attribute thickness_scaling, which will scale the thickness of everything: lines, grid lines, axes lines, etc. Since you only want to change the thickness of axes, you need to scale down the others. Here is your example code doing that using pyplot backend.

using Plots
pyplot() # use pyplot backend

Nx, Ny = 101,101
x = LinRange(0, 100, Nx)
y = LinRange(0, 100, Ny)

foo(x,y; x0=50, y0=50, sigma =1) = exp(- ((x-x0)^2 + (y-y0)^2)/(2*sigma^2)  )
NA = [CartesianIndex()]  # for "newaxis"
Z = foo.(x[:,NA], y[NA,:], sigma=10);

hm = heatmap(x, y, Z, xlabel="x", ylabel="y", c=cgrad(:Blues_9), clim=(0,1))
plot(hm, tickfontsize=10, labelfontsize=14) # your previous plot

# here is the plot code that shows the same plot with thicker axes on a new window
# note that GR backend does not support `colorbar_tickfontsize` attribute
plot(hm, thickness_scaling=2, tickfontsize=10/2, labelfontsize=14/2, colorbar_tickfontsize=8/2, reuse=false)

See Julia Plots Documentation for more about plot attributes.

Higgle answered 28/11, 2021 at 18:15 Comment(2)
Thank you very much! A good workaround is what I was looking for. Why is the pyplot() backend necessary?Utilitarianism
It may not be necessary but only the pyplot backend seems to support colorbar_tickfontsize option. If you don't use it, the numbers next to the colorbar ticks will be larger than normal.Higgle
S
0

A simple workaround where you do not need to add attributes for all the fonts is to add verticle and horizontal lines at the limits for x and y of the plots. For example, if I have a figure fig with 4 subplots, each with the same bounds, I can use this to get a thicker box frame:

for i ∈ 1:4
    vline!(fig[i], [xlim_lb, xlim_ub],
        linewidth=3,
        color=:black,
        label=false)
    hline!(fig[i], [ylim_lb, ylim_ub],
        linewidth=3,
        color=:black,
        label=false)
end

or for the original example here, add this to the end:

frame_thickness = 5
vline!([x[1], x[end]], color=:black, linewidth=frame_thickness, label=false)
hline!([y[1], y[end]], color=:black, linewidth=frame_thickness, label=false)
Sheeran answered 22/8, 2022 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.