# Plotting text with GLMakie

**URL:** <https://discourse.julialang.org/t/plotting-text-with-glmakie/112230>\
**Category:** General Usage\
**Tags:** question, package, glmakie\
**Created:** [March 28, 2024, 11:50am UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230 "2024-03-28T11:50:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 11:50am UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/1 "2024-03-28T11:50:26Z")

</div>

I am trying to plot text in a GLMakie window.

My code:

```julia
    import GLMakie
    fig = GLMakie.Figure(size = (400, 400))
    ax = GLMakie.Axis(fig[1,1])
    GLMakie.text!(0, 20; text="Hello", show_axis = false, fontsize = 30)
    display(GLMakie.Screen(), fig)
    nothing

```

Simpler, but same behaviour:

```julia
    import GLMakie
    fig = GLMakie.text(0, 20; text="Hello", show_axis=false, fontsize = 30)
    display(GLMakie.Screen(), fig)
    nothing

```

While it prints the text, it also shows an axis which is not what I want.  
`show_axis = false` has no effect.

If I do not create an `Axis` I get an error message, if I use `text` instead of `text!` the text is not getting displayed…

Any idea?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 12:21pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/2 "2024-03-28T12:21:16Z")

</div>

This works:

```julia
import GLMakie
fig = GLMakie.Figure(size = (400, 400))
lscene = GLMakie.LScene(fig[1, 1], show_axis=false)
GLMakie.text!(lscene, position = GLMakie.Point2f( 20, 0), "Hello", fontsize = 30, space=:pixel)
display(GLMakie.Screen(), fig)
nothing

```

Not very intuitive. Is there any easier solution?

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [March 28, 2024, 1:11pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/3 "2024-03-28T13:11:01Z")

</div>

Have you tried `Label` ?

```julia
julia> using GLMakie

julia> fig = Figure()

julia> Label(fig[1,1], "Hello")

```

![image](https://global.discourse-cdn.com/julialang/original/3X/3/7/37d29ac921b2e751500785b508598ab4f1647a4d.png)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 1:14pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/4 "2024-03-28T13:14:17Z")

</div>

What does `fig[1,1]` mean? How can a figure be an array? Is it always an array?  
If so, why? If so, of which dimensions?

And yes, I tried `Label`, but it does not support the keyword arguments `position` and `space`…

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [March 28, 2024, 1:21pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/5 "2024-03-28T13:21:02Z")

</div>

A `Figure` contains a `GridLayout`. `Label(fig[1,1],...)` tells the `Label` to use cell (1,1) of the `Figure`s `GridLayout`. I’d recommend reading through the excellent Makie tutorials:

> **[Basic Tutorial · Makie](https://docs.makie.org/stable/tutorials/basic-tutorial/)**
>
> Learn how to create data visualizations with Makie, the plotting ecosystem for the Julia language, in this introductory tutorial. Topics include Makie's different backends, basic building blocks like Figure, Axis, lines and scatters as well as how to...

[https://docs.makie.org/stable/tutorials/layout-tutorial/](https://docs.makie.org/stable/tutorials/layout-tutorial/)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 1:26pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/6 "2024-03-28T13:26:37Z")

</div>

I was reading the tutorial before. I still don’t find it logical to access array elements without creating an array (with known dimensions) in the first place. This feels like Matlab, you can never get an out-of-bounds error with this scheme, very error prone.

I would find the following syntax much cleaner:

Either

```julia
fig=Figure()

```

if you do not want any grid layout, or

```julia
fig=Figure(layout=GridLayout(2,3))

```

if you want one…

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [March 28, 2024, 1:44pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/7 "2024-03-28T13:44:44Z")

</div>

Probably worth a separate topic, but I never understood this choice either. I avoid the figure indexing syntax whenever possible by using AlgebraOfGraphics.jl or [direct plotting methods](https://docs.makie.org/stable/tutorials/basic-tutorial/#creating_figure_axis_and_plot_in_one_call) because I get confused every time I see it. Particularly, this statement from the basic tutorial makes no intuitive sense to me:

> We’ll learn more about layouts later, but for now the position `f[1, 1]` will just fill the whole figure.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [March 28, 2024, 2:00pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/8 "2024-03-28T14:00:57Z")

</div>

Well the full GridLayout fills the Figure and it grows on demand. By default it contains only the cell at (1,1).

Personally I find these semantics very convenient for iteratively constructing complicated figures. E.g. if you all of the sudden want another colorbar on the left then you don’t need to shift all subplots over and can just put the colorbar in the 0th column.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 2:26pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/9 "2024-03-28T14:26:22Z")

</div>

> [@ufechner7](#):
>
> This works:
> 
> ```julia
> import GLMakie
> fig = GLMakie.Figure(size = (400, 400))
> lscene = GLMakie.LScene(fig[1, 1], show_axis=false)
> GLMakie.text!(lscene, position = GLMakie.Point2f( 20, 0), "Hello", fontsize = 30, space=:pixel)
> display(GLMakie.Screen(), fig)
> nothing
> 
> ```

Coming back to the original question: Is there an easier solution than this one if you want to display a text in a Makie window and provide the x, y position?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [March 28, 2024, 3:00pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/10 "2024-03-28T15:00:09Z")

</div>

What _exactly_ do you want to achieve? There are many ways to do that, depending on what you’re doing exactly.  
Here is one:  
[https://docs.makie.org/stable/tutorials/scenes/#interaction\_with\_axis\_layouts](https://docs.makie.org/stable/tutorials/scenes/#interaction_with_axis_layouts)  
You can also use a `Scene` directly, for a lightweight, low level solution:

```julia
scene = Scene()
text!(scene, 20, 0, text="hello")
display(GLMakie.Screen(), scene)

```

You can also plot into an existing axis/lscene, with e.g. `text!(axis, 20, 0, text="hello", space=:pixel)`

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 3:09pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/11 "2024-03-28T15:09:48Z")

</div>

This doesn’t work. The code:

```julia
import GLMakie
scene = GLMakie.Scene()
GLMakie.text!(scene, 20, 0, text="hello", fontsize = 30)
display(GLMakie.Screen(), scene)
nothing

```

just gives a white window, but no text…

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 3:15pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/12 "2024-03-28T15:15:31Z")

</div>

OK, this works:

```julia
import GLMakie
scene = GLMakie.Scene()
GLMakie.text!(scene, 20, 0, text="hello", fontsize = 30, space=:pixel)
display(GLMakie.Screen(), scene)
nothing

```

But again, I find it confusing. What is the difference between a `Figure` and a `Scene`? How can it be that I can use a `Scene` instead of a `Figure`? Why do I need two distinct types if they are interchangeable?

UPDATE:  
Scene and Figure are actually NOT interchangeable. In this example I MUST use a Scene and cannot use a Figure. But why?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 3:51pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/13 "2024-03-28T15:51:22Z")

</div>

This also works, and is more logical for me:

```julia
import GLMakie
fig = GLMakie.Figure(size = (400, 400))
GLMakie.text!(fig.scene, 20, 0, text="hello", fontsize = 30, space=:pixel)
display(GLMakie.Screen(), fig)

```

No implicit automatic generation of a `Figure` in the background…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 28, 2024, 4:43pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/14 "2024-03-28T16:43:28Z")

</div>

The design is relatively simple:

A `Figure` is a `Scene` plus a `GridLayout`. You could use a plain `Scene` for all your drawing in Makie but then you’d have to place all your elements manually where you want them. So the `Figure` is a `Scene` that has already been connected to the `GridLayout` in a way that the layout follows the scene size (think resizing your window in GLMakie). The `Figure` also has a little bit of a bookkeeping functionality as it holds a vector of `Block`s which are just composites of child scenes and plots, but the underlying `Scene` does not know about that concept at all.

A `Figure` is not an array, the `fig[1, 1]` syntax directly forwards to `fig.layout[1, 1]` which is mostly more annoying to write as you’re going to do this often, and the `Figure` needs no other indexing behavior, so we made the choice long ago to have one mean the other.

A `GridLayout` is not an array. The indexing syntax was just the most ergonomic syntax to use. Indexing into it returns an object that describes a position in the layout. It doesn’t yet mutate the layout. Placing an object there by then doing `Axis(fig[2, 3])` or whatever does mutate the layout. It does not have a “storage area” like a matrix so there’s no reason not to allow this. It would be very inconvenient when building figures if you had to decide at line 1 how many rows and columns you want to have. You could not make anything iteratively this way.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 4:57pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/15 "2024-03-28T16:57:23Z")

</div>

Thanks for the detailed explanation!

But why is

```julia
GLMakie.Label(fig[1,1], text="hello", fontsize = 30)

```

allowed, but

```julia
GLMakie.text!(fig[1,1], 20, 0, text="hello", fontsize = 30, space=:pixel)

```

is not allowed?

Which commands expect a `GridPosition` as first parameter, and which commands expect a `Scene` or `LScene`?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 28, 2024, 4:59pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/16 "2024-03-28T16:59:53Z")

</div>

The documentation is split between `Block`s and `Plot`s. [Reference · Makie](https://docs.makie.org/stable/reference/)

`Block`s like `Axis` all are layout-aware objects based on `Scene`s with content that are meant to be rectangular pieces in a `Figure`. `Plot`s are combinations of Makie’s visual primitives and are meant to live as children of a `Scene` (when you plot into an `Axis` you really plot into the child `Scene` that the `Axis` owns).

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 5:46pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/17 "2024-03-28T17:46:12Z")

</div>

> [@sdanisch](#):
>
> What _exactly_ do you want to achieve?

I created a pop-up window that shows some statistics about my plots:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/a/aa094f6dba59e02af772f11cfb826b0af29629f9.png)

Source code: [KiteControllers.jl/examples/stats.jl at 2c4eb42a88c16acf71c34c84ad89764b902aa0a2 · aenarete/KiteControllers.jl · GitHub](https://github.com/aenarete/KiteControllers.jl/blob/2c4eb42a88c16acf71c34c84ad89764b902aa0a2/examples/stats.jl#L11C1-L41C4)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 5:50pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/18 "2024-03-28T17:50:04Z")

</div>

> [@jules](#):
>
> `Block`s like `Axis` all are layout-aware objects based on `Scene`s with content that are meant to be rectangular pieces in a `Figure`. `Plot`s are combinations of Makie’s visual primitives and are meant to live as children of a `Scene` (when you plot into an `Axis` you really plot into the child `Scene` that the `Axis` owns).

Thanks for your explanation. But I cannot find this kind of explanation in the documentation.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 28, 2024, 6:02pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/19 "2024-03-28T18:02:36Z")

</div>

[https://docs.makie.org/stable/explanations/blocks/](https://docs.makie.org/stable/explanations/blocks/)  
[https://docs.makie.org/stable/explanations/figure/](https://docs.makie.org/stable/explanations/figure/)  
[https://docs.makie.org/stable/explanations/layouting/](https://docs.makie.org/stable/explanations/layouting/)  
[https://docs.makie.org/stable/explanations/scenes/](https://docs.makie.org/stable/explanations/scenes/)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 28, 2024, 6:04pm UTC](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230/20 "2024-03-28T18:04:40Z")

</div>

Yes, a lot of explanations, but not in context, the relation between these four things is not explained, only each on itself. The overview is missing.

[Next page](https://discourse.julialang.org/t/plotting-text-with-glmakie/112230.md?page=2)
