# Colorbar\_title position & orientation

**URL:** <https://discourse.julialang.org/t/colorbar-title-position-orientation/42715>\
**Category:** Visualization\
**Tags:** question, plots, gr, gnuplot\
**Created:** [July 8, 2020, 8:58am UTC](https://discourse.julialang.org/t/colorbar-title-position-orientation/42715 "2020-07-08T08:58:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [July 8, 2020, 8:58am UTC](https://discourse.julialang.org/t/colorbar-title-position-orientation/42715/1 "2020-07-08T08:58:48Z")

</div>

When dealing with several 2.5 d plots at the same time, it’s nice to indicate what the colorbar stands for.  
To do so, we have the `colorbar_title` attribute, now even supported by `GR`. I assume, with long titles in mind, it puts the `colorbar_title` in vertical orientation right next to the colorbar, e.g. this example:  
 ![cbtitle](https://global.discourse-cdn.com/julialang/original/3X/c/e/ce0d92c1c36da29b536e6232dd71188abb3f30d3.png)

This is not always the most legible way. Especially with shorter titles, it would be nice to be able to tell `colorbar_title` to be in `:horizontal` orientation.  
On top of this, it would be great to have a second line just above the colorbar to annotate units. The units could even have a bit smaller font szie than the title text.  
So what I am looking for is something like this:  
 ![CBT_MyTitle](https://global.discourse-cdn.com/julialang/original/3X/e/f/eff77e27f14a48902f4a3804c1f14e9e39b72ef2.png)

Is some of this already implemented and I have overlooked it?  
What would be a robust way to place the `colorbar_title` in horizontal orientation ontop of the colorbar?  
With `robust` I mean a better response to the positioning than me using `annotate = (x,y,"MyCBTitle")`  
Which is not so useful when later composing plots with `@layout`.  
Any suggestions are very welcome.

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [July 8, 2020, 1:04pm UTC](https://discourse.julialang.org/t/colorbar-title-position-orientation/42715/2 "2020-07-08T13:04:56Z")

</div>

Hello, I know that you are looking for something that works with Plots. I do not have a solution with Plots, however I can do what you are asking for with [Gnuplot.jl](https://github.com/gcalderone/Gnuplot.jl). Take a look at the [examples](https://lazarusa.github.io/gnuplot-examples/menu2/#heat_map)(and yes you need to play around a little with some parameters, but not much).

1. Single plot:

```julia
using Gnuplot, Random
Random.seed!(123)
test = rand(50,50)
@gp "set auto fix" "set size square" :-
@gp :- test "w image notit" """set cblabel "CBTitle \\n (my unit)" """ :-
@gp :- "set cblabel offset -6.5, 10 font ',8' textcolor lt 3 rotate by 0" :-
@gp :- palette(:plasma) :-
@gp :- "set tmargin at screen 0.90"
save(term = "pngcairo size 400,400", output = "colorbar_labels_single.png")

```

![colorbar_labels_single](https://global.discourse-cdn.com/julialang/original/3X/5/1/5179375124c0bcbde9f8e94eff554b9821e6dc3e.png)  
2. Multiplot

```julia

```

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [July 8, 2020, 1:06pm UTC](https://discourse.julialang.org/t/colorbar-title-position-orientation/42715/3 "2020-07-08T13:06:43Z")

</div>

```julia
using Gnuplot, Random
Random.seed!(123)
test = rand(50,50)
@gp "set multiplot layout 1,2"
# on top
@gp :- 1 "set auto fix" "set size square" :-
@gp :- test "w image notit" """set cblabel "CBTitle \\n (my unit)" """ :-
@gp :- "set cblabel offset -6.5, 10 font ',8' textcolor lt 3 rotate by 0" :-
@gp :- "set tmargin at screen 0.85" :-
# on the side
@gp :- 2 "set auto fix" "set size square" :-
@gp :- test "w image notit" """set cblabel "CBTitle (my unit)" """
@gp :- "set cblabel offset 0, 0 font ',8' textcolor lt 4 rotate by 90" :-
@gp :- palette(:viridis) :-
@gp :- "set rmargin at screen 0.9"
save(term = "pngcairo size 800,400", output = "colorbar_labels_layout.png")

```

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

---

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [March 3, 2022, 4:33pm UTC](https://discourse.julialang.org/t/colorbar-title-position-orientation/42715/4 "2022-03-03T16:33:25Z")

</div>

Thanks for your contribution.  
And a nice reminder of why I like gnuplot.

However, I’m still looking for a solution with Plots and GR.

Is there a way to get the colorbar a _subplot_ so it could be rearanged with `@layout`?

Perhaps on the lines of the following.  
For the plot `p` in:

```nohighlight
p = plot(rand(100, 100), seriestype = :histogram2d)

```

I am looking for a handle on the colorbar:

```nohighlight
cb = p[1][1].colorbar

```

So I’d be able to put it where I want

```nohighlight
an = annotate(text("CBT"))

l = @layout [ an{0.1h}
               heatmap cb ]

plot(p, cb, an, layout = l)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 3, 2022, 7:15pm UTC](https://discourse.julialang.org/t/colorbar-title-position-orientation/42715/5 "2022-03-03T19:15:02Z")

</div>

> [@met-j](#):
>
> However, I’m still looking for a solution with Plots and GR.

One way:

```julia
using Measures, Plots; gr()

z = rand(100, 100)
l = @layout [a{0.95w} [b{0.03h}; c]]
p1 = heatmap(axes(z,1), axes(z,2), z, cbar=false)
p2 = plot([NaN], lims=(0,1), framestyle=:none)
annotate!(-0.2, -0.5, text("Random\n[m/s]", 9, "Computer Modern"))
p3 = heatmap([NaN;;], framestyle=:none, clims=extrema(z), cbar=true)
plot(p1,p2,p3, layout=l, margins=0mm)

```

 ![Plots_gr_colorbar_horizontal_title](https://global.discourse-cdn.com/julialang/original/3X/e/3/e371fcd99270f12bd1b26bcc65879dcae1cd855e.png)
