# How can I move an axis in a 3d plot, so that it is shown "outside" of the grid?

**URL:** <https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365>\
**Category:** Visualization\
**Tags:** plotting, plots, gr\
**Created:** [March 20, 2023, 10:49pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365 "2023-03-20T22:49:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 20, 2023, 10:49pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/1 "2023-03-20T22:49:36Z")

</div>

Hi all,  
I’m making a plot using `Plots.jl` with the default GR backend.

When I run this code

```julia
    # density_matrix contains the data I want to plot
    p = plot(density_matrix, st = :surface, xlabel="Binary weight", ylabel="Power",
        zlabel="Density",
        zlims=(0,200),
        c=cgrad(:heat, scale = :exp),

    ) # create surface
    gui()

```

I obtain this nice plot:  
 ![dispersion-usual](https://global.discourse-cdn.com/julialang/original/3X/3/1/312fdfac54d0af419d6ce3b52255effde4916bb9.png)  
However, if I **change the camera** , like here:

```julia
    p = plot(density_matrix, st = :surface, xlabel="Binary weight", ylabel="Power",
        zlabel="Density",
        zlims=(0,200),
        c=cgrad(:heat, scale = :exp),
        camera = (-45,50) # NOTE HERE
    ) # create surface
    gui()

```

I get:  
 ![dispersion-tilted](https://global.discourse-cdn.com/julialang/original/3X/4/2/42997d6fab0a5ec8ee2e9edd4ba04c119f1b8313.png)  
Now, I want this angle for the camera, but as you see the axis are all misplaced. How can I move them?

Thanks!

---

<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 21, 2023, 4:13pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/2 "2023-03-21T16:13:27Z")

</div>

I may have found a trick I think you can try to overcome the current limitation of Plots.jl gr() for negative camera azimuths, which hides the axes behind the surface.

```julia
using Plots; gr(dpi=600)

f(x,y) = x.^3 .* y'

x = range(0,3,100)
y = range(0,40,200)
θ = 60 # camera azimuth [deg]

# Positive camera azimuth, axes OK:
Plots.surface(x, y, f, zlim=(0,30), clim=(0,30), camera=(θ,50), framestyle=:box)

# Axes hidden for negative camera azimuth:
Plots.surface(x, y, f, zlim=(0,30), clim=(0,30), camera=(-θ,50), framestyle=:box)

# Trick to get the axes to the front:
g(y,x) = f(x,y)
Plots.surface(y, x, g, xflip=true, zlim=(0,30), clim=(0,30), camera=(90 - θ,50), framestyle=:box, title="equivalent of camera=(-$θ, 50)")

```

 ![Plots_gr_3d_hidden_axis_negative_camera_azimuth](https://global.discourse-cdn.com/julialang/original/3X/f/9/f90f43ad08cd5fd7dc97964c4fb77faeabf4b0d0.jpeg)

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 21, 2023, 6:50pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/3 "2023-03-21T18:50:06Z")

</div>

Thanks, it kinda works but correct me if I’m wrong: I also need to switch `xlabel` and `ylabel`, right?

---

<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 21, 2023, 6:50pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/4 "2023-03-21T18:50:58Z")

</div>

Of course you do.

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 21, 2023, 6:51pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/5 "2023-03-21T18:51:55Z")

</div>

Then it works, thanks! 🎉 Seems like quite a bug of the GR backend tho. Where should I report it?

---

<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 21, 2023, 6:53pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/6 "2023-03-21T18:53:45Z")

</div>

You can create a new issue here:

> **[Issues · JuliaPlots/Plots.jl](https://github.com/JuliaPlots/Plots.jl/issues)**
>
> Powerful convenience for Julia visualizations and data analysis - Issues · JuliaPlots/Plots.jl

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 21, 2023, 6:58pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/7 "2023-03-21T18:58:34Z")

</div>

Thanks, I will, also because with this workaround I perform KDE on the other dimension (`"Power"` instead of `"Binary weight"`), which is something I wanted to preserve  
**Edit** : nevermind, I realised this happened also with the “usual” plot, so I probably misinterpreted the plot or performed the KDE wrongly, anyway it shouldn’t be an issue of `Plots.jl`

---

<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 21, 2023, 7:15pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/8 "2023-03-21T19:15:47Z")

</div>

Perhaps I am missing something, but if we have as input a `density_matrix` (instead of a function as in my example), then we only need to plot the transpose with the `xflip` and ` 90-θ` azimuth.

Try for example:

```julia
Plots.surface(transpose(density_matrix), xflip=true, zlim=(0,30), clim=(0,30), camera=(90 - θ,50), framestyle=:box, title="equivalent of camera=(-$θ, 50)")

```

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 21, 2023, 8:03pm UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/9 "2023-03-21T20:03:19Z")

</div>

It’s what I did:

```julia
    transposed_matrix = transpose(density_matrix)
    p = plot(transposed_matrix, st = :surface, title=plot_title,
        ylabel="Binary weight", xlabel="Power", # inverted as well
        zlabel="Density",
        xflip=true,
        dpi=500,
        # zlims=(0,200),
        c=cgrad(:heat, scale = :exp),
        camera = (45,50)
    ) # create surface

```

plus I switched the labels.

---

<div class="post-metadata">

**Author:** ![mcmuffin6o](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mcmuffin6o](https://discourse.julialang.org/u/mcmuffin6o)\
**Post date:** [December 1, 2023, 8:02am UTC](https://discourse.julialang.org/t/how-can-i-move-an-axis-in-a-3d-plot-so-that-it-is-shown-outside-of-the-grid/96365/10 "2023-12-01T08:02:35Z")

</div>

This is better than the hack I just came up with in 99% of situations, but if anyone is picky like me about not wanting to edit the data (and potentially mess it up) before plotting it, you can try [this function I made](https://discourse.julialang.org/t/3d-plots-change-axis-intersection-inverse-axis/51109/11).

BE FOREWARNED: IT TRULY IS AN UGLY, DIRTY HACK AND @rafael.guerra’s SOLUTION IS LIKELY THE LESSER OF TWO EVILS.
