Numeric string format for Makie plot?

Hello everyone! Sorry if my question is too basic for some people. Basically I’m trying to define the numeric string format for my Makie plot, specifically for the axis. The numbers on the y-axis that I want to display have the following format: 1,000,000.328, where you have comma separator for every three number and decimal separator for the decimal portion. I’ve tried the Python number formatting, like {:.3f} or {.,}, but result is far from what I expect. Below is an example.

fig = Figure(resolution = (1920, 1080))

update_theme!(fontsize = 30)

axis = fig[1, 1] = Axis(fig, 
                        title = "An example",
                        ytickformat = "{:.3f}")

dat = DataFrame(x = 1:5, y = [1_000_000.236, 1_000_000.660, 1_000_000.368, 1_000_000.225, 1_000_000.224])

barplot!(axis, dat.x, dat.y)

fig

So, is there any way to achieve my goal? Thank you in advance !

Seems like Formatting.jl (which the format strings use in the background) doesn’t do this, you could try GitHub - JuliaString/Format.jl: A Julia package to provide C and Python-like formatting support instead.

1 Like

Hi, thank you for your response. I’ve spent a couple of hours playing around with the package, it’s very handy. But yet, I still have no idea how to implement it with Makie, since it keeps raising error as invalid string format. Could you please make an example of using it with xticklabel or yticklabel ?

Try

using CairoMakie
using Formatting
using DataFrames

fig = Figure()
update_theme!(fontsize = 30)
axis = fig[1,1] = Axis(fig,
                       title = "An examle",
                       ytickformat = v -> format.(v, commas=true, precision=3)
                        )
dat = DataFrame(x = 1:5, y = [1_000_000.236, 1_000_000.660, 1_000_000.368, 1_000_000.225, 1_000_000.224])
barplot!(axis, dat.x, dat.y)

save("mwe.png", fig)

which gives me

2 Likes

Ah so it does work when using format, I just tried the format string syntax "{:,.3f}" which parses but doesn’t do anything.

1 Like

Thanks everyone for your help and responses :pray: I really appreaciate :heart:

1 Like