Makie - is there a function to show a figure?


This is really a very basic question, but I find the short examples given on a bit confusing regarding which instruction(s) to use to actually display a figure, apart from typing its variable name in the REPL (I am using CairoMakie from within VSCode with the Julia extension, in case that matters).

My confusion comes from the fact that some functions (e.g. barplot) return a figure and seem to magically force showing it. But other functions such as barplot! show nothing. From the examples, it seems that I have to first create a figure, then an axis (because somehow creating a figure does not create a default axis?), then plot into that figure, and just type the name of that figure to force showing it, as in:

using CairoMakie
f = Figure()
a = Axis(f[1,1]) 
p = barplot!([1,2], [10,11])
f    # This actually shows the figure

Now the fun part is that if the code above is within a script, and I want to do something useful after plotting, I have no idea how to show the figure, except by adding a line with f at the very end of the script (or using the REPL to type f once the script has run).

using CairoMakie
f = Figure()
a = Axis(f[1,1]) 
p = barplot!([1,2], [10,11])
f    # This actually shows the figure... But NOT if it's not the last instruction!
# Some useful stuff
x = 4
# The figure is not shown

I tried show(f) but this prints some lines of information in the REPL.
Is there some other function I missed?


PS. Speaking about Makie documentation, some examples show that barplot! for example can be used with a first argument which is an axis (barplot!(a, ...) in the example above). Where would I find which arguments are accepted?
I found this page: barplot
Some examples use an axis, sometimes as first argument (with just the axis variable name), sometimes with a axis = (...) argument. Is there a list of arguments somewhere? Help obtained with ?barplot! in the REPL does not even mention a possible argument before x and y.

1 Like

Thanks, but I think it has the same problem as simply typing the figure variable name.
If you use it in the REPL, it shows the figure, but in a script, it will only show the figure if that’s the last instruction in the script.

1 Like

display(fig) does what returning in the repl does.

1 Like

And this is the page that explains the plot function signatures

Thanks. Now that I know what to look for, I noticed it is mentioned in a blue box in the Basic Tutorial.
But strangely enough, even the code snippets on that page use current_figure() and not display(fig). And I didn’t see display(fig) being used in any of the other tutorials I looked at.

Thanks, very useful.

Hm yes the display behavior is a basic Julia functionality, though. And inside the documentation environment display wouldn’t work because you have to return things from code blocks to display them, you don’t have direct access. It’s always difficult to know what to include in the tutorials and docs, let’s say to many who have used Julia for a while it would be obvious that this is how it works but it can still be confusing for all the others. So I can only recommend if you have this experience that something was very confusing while learning Makie, open an issue or even a documentation PR to save those after you from going through the same :slight_smile: We only have our limited experiences to come up with content and may miss obvious things to include