# Adding labels to surfaces in the same figure

**URL:** https://discourse.julialang.org/t/adding-labels-to-surfaces-in-the-same-figure/79708
**Category:** Visualization
**Tags:** plots, gr, pgfplotsx
**Created:** [April 19, 2022, 8:49pm UTC](https://discourse.julialang.org/t/adding-labels-to-surfaces-in-the-same-figure/79708 "2022-04-19T20:49:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![gnicolosi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnicolosi/32/48031_2.png) [@gnicolosi](https://discourse.julialang.org/u/gnicolosi)
#### Post date: [April 19, 2022, 8:49pm UTC](https://discourse.julialang.org/t/adding-labels-to-surfaces-in-the-same-figure/79708/1 "2022-04-19T20:49:23Z")

</div>

Hi all, how do I add labels to the surfaces below? Using `label=` as when plotting curves in 2d does not work. There’s very few information on attributes to `surface()` and I can’t find a solution anywhere else for this simple issue.

That’s how I created it:

```julia
using Plots, LaTeXStrings
surface(0:0.05:4, 0.05:0.1:0.95, uₜᵣᵤₑ, color=:blues)
surface!(0:0.05:4, 0.05:0.1:0.95, uₚₗₒₜ, zlabel=L"$u(t,x_0)$", colorbar=false, camera=(60,30), fillalpha=0.8)
title!(L"Exact and Approximated Surfaces $M=2$")

```

![Capture](https://global.discourse-cdn.com/julialang/original/3X/c/9/c9d51c4d601482b81f7341f99b5db37b412f6188.png)

All I want is a tiny box in the corner indicating what each surface represents. Let me know if I am not following some best practice when it comes to plotting.

Thank you,

---

<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: [April 20, 2022, 6:11am UTC](https://discourse.julialang.org/t/adding-labels-to-surfaces-in-the-same-figure/79708/3 "2022-04-20T06:11:04Z")

</div>

One way using insets:

 ![Plots_gr_3d_surface_inset_annotation](https://global.discourse-cdn.com/julialang/original/3X/4/9/4984a04999d104c5c16ed38bd8dbddfbe9b32369.jpeg)

> **Plots gr code**
>
> ```julia
> using Plots, LaTeXStrings
> uₜᵣᵤₑ(x,y) = x + 0.5y; uₚₗₒₜ(x,y) = 2x + y
> x, y = 0:0.05:4, 0.05:0.1:0.95
> surface(x, y, uₜᵣᵤₑ, c=cgrad([:red,:red]), camera=(60,30), cb=false,fa=0.8)
> surface!(x, y, uₚₗₒₜ, c=cgrad([:blue,:blue]), zlabel=L"$u(t,x_0)$",fa=0.8)
> title!(L"Exact and Approximated Surfaces $M=2$")
> scatter!([0 0 0], [-1 NaN -1], lims=(0,1),
> inset=(1,bbox(0.05,0.1,0.15,0.15)), subplot=2, msw=0, marker=:square,
> legendfontsize=8, framestyle=:none, fg_color_legend=nothing, legend=:left,
> color=[:red :white :blue], label=" "^4 .* [L"x + 0.5y" "" L"2x + y"]
> )
> 
> ```

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [April 20, 2022, 6:47am UTC](https://discourse.julialang.org/t/adding-labels-to-surfaces-in-the-same-figure/79708/4 "2022-04-20T06:47:59Z")

</div>

This is a nice solution. I like the small tweaks that add spacing to make the layout look cleaner.

---

<div class="post-metadata">

### Author: ![gnicolosi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnicolosi/32/48031_2.png) [@gnicolosi](https://discourse.julialang.org/u/gnicolosi)
#### Post date: [April 21, 2022, 3:20pm UTC](https://discourse.julialang.org/t/adding-labels-to-surfaces-in-the-same-figure/79708/5 "2022-04-21T15:20:15Z")

</div>

Thank you for the clever solution! I wonder why `label=` does not work as it does when using `plot()`. Nonetheless, it worked just fine.  
Gabriel

---

<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: [April 21, 2022, 3:53pm UTC](https://discourse.julialang.org/t/adding-labels-to-surfaces-in-the-same-figure/79708/6 "2022-04-21T15:53:43Z")

</div>

> [@gnicolosi](#):
>
> I wonder why `label=` does not work as it does when using `plot()`

It works if `using Plots; pgfplotsx()` backend, but the labels will be outside the plot. I remember seeing an open issue in github on this matter, but cannot find it anymore.

 ![Plots_pgfplotsx_3d_surface_label_annotation](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc49ab55eae96cbf61547117c2c7fd18189cda86.jpeg)

> **Plots pgfplotsx() code**
>
> ```julia
> using LaTeXStrings, Plots; pgfplotsx()
> uₜᵣᵤₑ(x,y) = x + 0.5y; uₚₗₒₜ(x,y) = 2x + y
> x, y = 0:0.05:4, 0.05:0.1:0.95
> surface(x, y, uₜᵣᵤₑ, colormap_name="viridis", camera=(60,30), cb=false,fa=0.8, label=L"x + 0.5y");
> surface!(x, y, uₚₗₒₜ, colormap_name="hot", zlabel=L"$u(t,x_0)$", fa=0.8, label=L"2x + y")
> title!(L"Exact and Approximated Surfaces $M=2$")
> 
> ```
