# Using Makie for Allotaxonometry

**URL:** https://discourse.julialang.org/t/using-makie-for-allotaxonometry/73453
**Category:** Visualization
**Tags:** question, plotting, makie
**Created:** [December 21, 2021, 8:07pm UTC](https://discourse.julialang.org/t/using-makie-for-allotaxonometry/73453 "2021-12-21T20:07:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jstonge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jstonge/32/32122_2.png) [@jstonge](https://discourse.julialang.org/u/jstonge)
#### Post date: [December 21, 2021, 8:07pm UTC](https://discourse.julialang.org/t/using-makie-for-allotaxonometry/73453/1 "2021-12-21T20:07:03Z")

</div>

Hi,

As part of a small project of mine, I want to replicate a figure in a paper using `Makie` (this [paper](https://physics.paperswithcode.com/paper/allotaxonometry-and-rank-turbulence) and these [figures](https://mvarnold.w3.uvm.edu/storywrangler/lightbox/)). The idea of allotaxonometry plots is that you compare two systems that are zipf distributed (in the linked figures, twitter at time _t_ and _t+1_) to see the relative use of, say, terms. The key challenge is to work on a rotated scene, which I found was not too difficult using `Makie` (once you understand quaternions; see below for my current attempt). To do this, I simply create a heatmap and draw a background, which I then rotate using `rotate!()`, e.g

```julia
f = Figure()
ax = Axis(f[1,1])

# custom function to distinguish between the 2 systems  
draw_background2!(counts_rank)
heatmap!(counts_rank, colormap = Reverse(cgrad(:magma)))

q = qrotation(SVector(1, -0.415, 0), deg2rad(180))
rotate!(f.scene.children[1], q

# find the new relevant limits   
limits!(-54, 54, -108, 0)

display(f)

```

Now, the tricky part is that I want to (i) annotate squares far away from the axis, which represent terms that have changed the most during that time period (text annotation are now messy to work with, due to the rotation), and (ii) rotate the axis so that I can properly label them (again, see linked figures to see what I mean). Any help of how to best achieve these goals would be greatly appreciated! I think this type of plot could be a great addition to the `Makie` gallery.

 ![Screenshot from 2021-12-21 14-46-26](https://global.discourse-cdn.com/julialang/original/3X/3/9/39f9b502ae3a68bb0ac91db005f48fec905cd6d4.png)

---

<div class="post-metadata">

### Author: ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)
#### Post date: [December 22, 2021, 12:15am UTC](https://discourse.julialang.org/t/using-makie-for-allotaxonometry/73453/2 "2021-12-22T00:15:22Z")

</div>

There was some discussion about a rotated inset axis recently, might be interesting for you:

> [@Rotating Plots in Makie](https://discourse.julialang.org/t/rotating-plots-in-makie/72017):
>
> I’m interested in understanding how one can rotate a whole axis/plot in Makie, in order to produce visualizations like the one below. This solution was posted by Simon Danisch on Slack: using GLMakie f, ax, pl = barplot(rand(5), bar\_labels=:y, figure=(resolution=(500, 500),)) hideydecorations!(ax) hidespines!(ax, :t, :r, :l) hidexdecorations!(ax, ticks=false) tightlimits!(ax) f img = Makie.colorbuffer(ax.scene) f, ax, pl = scatter(rand(Point2f, 100), axis=(aspect = DataAspect(),)) …

> <https://github.com/MakieOrg/Makie.jl/issues/1476>
>
> I was trying to rotate a plot (and axis) to produce a visualization like this on…e below:
> !\[image\](https://user-images.githubusercontent.com/6407557/143333206-cf0b4206-8a6f-4d2f-864c-08b232fcd80d.png)
> I've posted the question and the following answer was given:
> \`\`\`julia
> fig = Figure()
> b = Axis(fig\[1, 1\])
> scene = Scene(fig.scene)
> campixel!(scene)
> ax = Axis(scene, bbox = MakieLayout.BBox(-100, 100, -100, 100))
> 
> for s in (scene, ax.scene)
> Makie.rotate!(s, Vec3f(0,0,1), -pi/4)
> translate!(s, Vec3f(300, 300, 100))
> end
> fig
> \`\`\`
> Unfortunately, this does not work on CairoMakie. As pointed out by @ffreyer who posted this solution: "This works for me in GLMakie but CairoMakie still has two problems - text gets transformed differently and disappears, and the background pane of the rotated axis doesn’t draw over the normal axis. I think these are things that need to fixed in CairoMakie though."

If `text!` doesn’t work correctly with `rotate!` (which I don’t think it does) you should still be able to do things manually. If you rotate the whole scene (i.e. ax.scene or f.scene.children[1]) you can get the pixel position within that scene for a data position with `px_pos = project(ax.scene, position)`. You can use that to plot text at the correct position by plotting to `f.scene`, which is in pixel units. You need to add the offset from `ax.scene` though, so it becomes

```julia
positions = Point2f[(1, 1), (2, 1), (3, 1)]

fig, ax, p = scatter(positions, color = [:red, :green, :blue])
rotate!(ax.scene, Vec3f(0,0,1), pi/4)
xlims!(ax, -0.5, 2.0)
ylims!(ax, 1, 3)

px_positions = map(positions) do pos
    Makie.project(ax.scene, pos) + minimum(ax.scene.px_area[])
end
text!(
    fig.scene, ["First", "Second", "Third"], position = px_positions, 
    color = [:red, :green, :blue], rotation = [0.0, pi/4, -pi/4]
)

fig

```

or

```julia
px_positions = map(ax.scene.camera.projectionview, ax.scene.px_area) do _, px_area
    map(positions) do pos
        Makie.project(ax.scene, pos) + minimum(px_area)
    end
end

```

to make the text positions update automatically.

 ![Screenshot from 2021-12-22 01-13-22](https://global.discourse-cdn.com/julialang/original/3X/f/f/ff25a0350fe8db9dcdae2f92eab8f39deac68794.png)

---

<div class="post-metadata">

### Author: ![EvoArt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evoart/32/25357_2.png) [@EvoArt](https://discourse.julialang.org/u/EvoArt)
#### Post date: [December 22, 2021, 4:28pm UTC](https://discourse.julialang.org/t/using-makie-for-allotaxonometry/73453/3 "2021-12-22T16:28:38Z")

</div>

Would love to see the finished version, once you are happy with it.

---

<div class="post-metadata">

### Author: ![jstonge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jstonge/32/32122_2.png) [@jstonge](https://discourse.julialang.org/u/jstonge)
#### Post date: [December 22, 2021, 5:01pm UTC](https://discourse.julialang.org/t/using-makie-for-allotaxonometry/73453/4 "2021-12-22T17:01:21Z")

</div>

I will definitely share, hopefully as a small library that sits on `Makie`. With Makie’s flexibility, I have good hope that the project will work out!
