Need help in formatting bar plot in Makie

I’m trying to recreate the following image using Makie and I’m having issues with some of the final formatting:

So far, I managed to come up with the following:

using CairoMakie

age_groups = ["0-12", "13-24", "25-36", "37-48", "49-60"]
num_individuals = [35, 23, 17, 13, 12]

barplot(
    num_individuals,
    color=:black,
    strokecolor=:white,
    strokewidth=1,
    width=0.5,
    gap=0,
    axis = (
            xlabel="Age Groups (in African Elephant years)",
            ylabel="Relative Abundance (%)",
            title="Type A Profile",
            limits=(nothing, nothing, 0, 35),
            yticks=(0:5:35),
            xticks=(1:5, age_groups),
            topspinevisible=false,
            rightspinevisible=false,
            xgridvisible=false,
            ygridvisible=false,
            xticksvisible=false
    )
)

To make the plot look more like the original, I’d like to eliminate the spacing between the bars including eliminating the space between the first bar and the y-axis. I’d like to also place tick marks between the bars along the x-axis. I’d also like to change the font to Arial, but I’ve been having difficulty figuring out how to do that.

Last, but not least, whenever I’ve tried exporting the figure, I’ve always ended up with a solid white image regardless of the format. I’m interested in a vector format, hence why I’m using CairoMakie.

Are all of these things possible using Makie? Would another plotting package be more capable?

Any help would be greatly appreciated. Thanks!

I’d like to eliminate the spacing between the bars including eliminating the space between the first bar and the y-axis

The bars are apart 1 unit by default and centered around their values starting at 1, so we set width to 1 and the xlimits to 0.5 and 5.5.

I’d like to also place tick marks between the bars along the x-axis

We can use minor ticks as a workaround because the tick labels are always relative to their ticks, so you can’t technically center them between two. But you can hide the major ticks and use the minor ticks for that.

I’d also like to change the font to Arial, but I’ve been having difficulty figuring out how to do that.

The easiest way is to pass font = "Arial" to the figure settings, via the figure = ... keyword arg. Would otherwise go to Figure(...) if the Figure wasn’t created implicitly with the plot.

Saving worked fine for me, try this:

age_groups = ["0-12", "13-24", "25-36", "37-48", "49-60"]
num_individuals = [35, 23, 17, 13, 12]

f, ax, bp = barplot(
    num_individuals,
    color=:black,
    strokecolor=:white,
    strokewidth=1,
    width=1,
    gap=0,
    axis = (
            xlabel="Age Groups (in African Elephant years)",
            ylabel="Relative Abundance (%)",
            title="Type A Profile",
            limits=(0.5, 5.5, 0, 35),
            yticks=(0:5:35),
            xticks=(1:5, age_groups),
            xticklabelsize = 14,
            xminorticks = 0.5:1:5.5,
            xminorticksvisible = true,
            xminorticksize = 8,
            topspinevisible=false,
            rightspinevisible=false,
            xgridvisible=false,
            ygridvisible=false,
            xticksvisible=false
    ),
    figure = (font = "Arial", resolution = (300, 450))
)
save("test.pdf", f)

1 Like

Thank you! This produced exactly what I was looking for!