Adding labels to surfaces in the same figure

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:

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

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,

One way using insets:

Plots gr code
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"]
)
3 Likes

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

1 Like

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

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() code
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$")
1 Like