# Can't get marginal plot to work

**URL:** https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685
**Category:** New to Julia
**Tags:** plotting, plots, recipe
**Created:** [November 17, 2021, 8:49pm UTC](https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685 "2021-11-17T20:49:47Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dodoplus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dodoplus/32/30148_2.png) [@dodoplus](https://discourse.julialang.org/u/dodoplus)
#### Post date: [November 17, 2021, 8:49pm UTC](https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685/1 "2021-11-17T20:49:47Z")

</div>

I’m trying to plot a contour with marginal plots.

I’ve created a user recipe:

```julia
@userplot JPlot

@recipe function f(h::JPlot)
    m, x, y = h.args
    
    mx = vec(sum(m, dims=1))
    my = vec(sum(m, dims=2))
    
    link := :both
    grid := false
    layout := @layout [t _
                       c{0.8w, 0.8h} r]
    
    # main
    @series begin
        seriestype := :contour
        subplot := 2        
        x, y, m
    end
    
    seriestype := :line
    
    # top
    @series begin
        subplot := 1
        x, mx
    end
    
    # right

    @series begin
        subplot := 3
        orientation := :horizontal        
        y, my
    end
end

```

which I tested with:

```julia
using Distributions

mv = MvNormal([0, 0], [1 0; 0 1])
xs = range(-3, 3, length=100)
ys = range(-3, 3, length=100)
m = [pdf(mv, [x, y]) for y in ys, x in xs]
jplot(m, xs, ys)

```

This is what I get:

 ![Screen Shot 2021-11-17 at 22.48.57](https://global.discourse-cdn.com/julialang/original/3X/6/c/6ca2cc2fe941c83be3f505d4f426c193e9f03a21.jpeg)

What happened to the marginal plot on the right?

Thanks  
DD

---

<div class="post-metadata">

### Author: ![dodoplus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dodoplus/32/30148_2.png) [@dodoplus](https://discourse.julialang.org/u/dodoplus)
#### Post date: [November 18, 2021, 5:43am UTC](https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685/2 "2021-11-18T05:43:35Z")

</div>

Update: the problem seems to be with `orientation := horizontal`

This is fine:

 ![Screen Shot 2021-11-18 at 7.41.44](https://global.discourse-cdn.com/julialang/original/3X/f/5/f54d00e4e1fc3f2bd89122b4aefad04dc5a00bdf.png)

But this isn’t:

 ![Screen Shot 2021-11-18 at 7.42.13](https://global.discourse-cdn.com/julialang/original/3X/2/a/2ab4a03a81f6d4ba40fbdd86e4a3c8818e7d56a3.png)

what’s going on?

---

<div class="post-metadata">

### Author: ![Matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Matthew](https://discourse.julialang.org/u/Matthew)
#### Post date: [November 19, 2021, 1:49pm UTC](https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685/4 "2021-11-19T13:49:02Z")

</div>

> Update: the problem seems to be with `orientation := horizontal`

It seems that `orientation := horizontal` is an attribute specific to bar plots. The `flip` attribute may be more appropriate.

`plot(my, ys, yflip =true)`

**Sources** :  
[flip](https://docs.juliaplots.org/latest/generated/attributes_axis/), [orientation](https://docs.juliaplots.org/latest/generated/attributes_series/)

---

<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: [November 19, 2021, 1:59pm UTC](https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685/5 "2021-11-19T13:59:40Z")

</div>

`yflip` seems to only reverse the y-scale, while a 90 degrees rotation of the plot is here required?

It seems to be a bug, but it should be possible to plot rotated data without rotating the plot.

---

<div class="post-metadata">

### Author: ![Matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Matthew](https://discourse.julialang.org/u/Matthew)
#### Post date: [November 19, 2021, 2:12pm UTC](https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685/6 "2021-11-19T14:12:41Z")

</div>

![Screen Shot 2021-11-19 at 9.11.19 AM](https://global.discourse-cdn.com/julialang/original/3X/a/6/a609bd2235debd682cfa3987b43f8b7ca877bf6a.png)

Is this not the desired output? Though I agree that it does seem to be a bug.

---

<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: [November 19, 2021, 2:18pm UTC](https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685/7 "2021-11-19T14:18:45Z")

</div>

Yes, it is. But not sure it is a result of only `:yflip` as you have swapped the order of `my` and `y`, which amounts to rotate the plot 90-deg.

---

<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: [November 19, 2021, 2:31pm UTC](https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685/9 "2021-11-19T14:31:31Z")

</div>

One could also replace the `right`-block in the OP’s recipe by:

```julia
    seriestype := :shape
    linecolor := :red
    # right
    @series begin
        subplot := 3
        [my; reverse(my)], [y; reverse(y)]
    end

```

and the output looks alright:

 ![Plots_gr_Rotated_data_using_shape](https://global.discourse-cdn.com/julialang/original/3X/2/d/2d7036ec752747cff98a29875a57397329563af3.png)

---

<div class="post-metadata">

### Author: ![dodoplus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dodoplus/32/30148_2.png) [@dodoplus](https://discourse.julialang.org/u/dodoplus)
#### Post date: [November 19, 2021, 2:42pm UTC](https://discourse.julialang.org/t/cant-get-marginal-plot-to-work/71685/10 "2021-11-19T14:42:19Z")

</div>

> [@rafael.guerra](#):
>
> `seriestype := :shape`

It solves the problem, but this is definitely a hack…
