# Confidence interval with Plots and pgfplots()

**URL:** https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440
**Category:** General Usage
**Tags:** pgfplotsx
**Created:** [November 24, 2019, 2:47am UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440 "2019-11-24T02:47:36Z")
**Posts on this page:** 16
**Page:** 2

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [December 5, 2019, 11:05pm UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/21 "2019-12-05T23:05:32Z")

</div>

@BeastyBlacksmith , I managed to install everything, it seems. But ribbon is still not defined. Can you replicate the code I initially posted at the top of this page?

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [December 6, 2019, 2:12pm UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/22 "2019-12-06T14:12:52Z")

</div>

Sorry, the code was indeed broken. The fix will come with the next release.  
Or you can use

```julia
add https://github.com/BeastyBlacksmith/Plots.jl#pgfplotsx

```

if you need it now.  
Note, that if you intended to give different ribbon size in `+y`- and `-y`-direction, that this is currently not supported by Plots as you may have noticed when using the PyPlot backend.

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [December 6, 2019, 5:24pm UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/23 "2019-12-06T17:24:02Z")

</div>

I correct myself: Asymmetric ribbons are supported and the syntax is

```julia
plot( x, y, ribbon=(dy-, dy+))

```

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [December 7, 2019, 3:42am UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/24 "2019-12-07T03:42:05Z")

</div>

yes, the ribbon can be asymmetric, and this is great. I don’t know if this is supposed to be this way, but just running

```julia
aa = rand(10)
plot(collect(1:10),aa)

```

plots the entire area between the graph and some straight line, instead of drawing just a simple line. I am happy to get into the details from a manual, but this seems kind of weird.

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [December 7, 2019, 2:21pm UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/25 "2019-12-07T14:21:17Z")

</div>

Thanks for reporting that bug. That is also fixed on my branch now and comes with next release ( if you are on that branch you can get the fix by `] up` ).

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [December 7, 2019, 9:47pm UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/26 "2019-12-07T21:47:35Z")

</div>

great. the last step (which is being surprisingly hard to understand looking at the documentation of PGFPlotsX.jl) is running it on latex.  
I run

```julia
tt = collect(1:10)
plot(tt,tt.^2)
savefig("folders/myfile.tex")

```

Which packages do I need in the preamble to be able to \input{myfile.tex}?

It seems that what creates problem is that the figure creates this  
\begin{tikzpicture}[/tikz/background rectangle/.style={fill={rgb,1:red,1.0;green,1.0;blue,1.0}, draw opacity={1.0}}, show background rectangle].  
The part in [/tikz/background rectangle…] is the part that creates problem. Once it is removed the figure is plotted without problems.

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [December 8, 2019, 4:04am UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/27 "2019-12-08T04:04:12Z")

</div>

Other bugs:

1. When you run plot and then overlay plot! multiple times, you need to run the ribbon last or it messes the legend
2. @layout doesn’t work well. l = @layout [a b; c d] stuck the plots one below each other or it even overlaps two plots
3. xaxis!(…) is not added to the plot unless it is run together with the plot

thanks again for managing this package, it produced awesome plots

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [December 8, 2019, 12:05pm UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/28 "2019-12-08T12:05:22Z")

</div>

you either can get a standalone version by `plot(tt, tt.^2, tex_output_standalone = true)` or you can query the preamble by `Plots.pgfx_preamble( plot )`.  
Will have a look at the other issues during the week. ( The layouting is already in the meta-issue, but right now I don’t have a good solution). If you want a 2x2 Grid `layout = 4` should work. And you can increase margins by setting `left/right/top/bottom_margin` if you have overlaps.  
Hope, this helps in the meantime.

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [December 9, 2019, 10:12pm UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/29 "2019-12-09T22:12:56Z")

</div>

Where should I type “Plots.pgfx\_preamble( plot )”? Does it remove the problematic part next to \begin{tikzpicture} or does it make that part no longer problematic?

By reading a lot of the pyplot() blogs it seems that the accepted solution for multiple levels of plots and labels is creating empty plots with only a title and to stack them all together. This solution does not work well with pgfplotsx(). In particular, the empty plot still draws the ticks in the x and y-axis.

Also, some marker types like star5 do not get colored: they appear light gray no matter which marker color is chosen.

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [December 10, 2019, 9:56am UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/30 "2019-12-10T09:56:35Z")

</div>

> [@juliauser](#):
>
> Where should I type “Plots.pgfx\_preamble( plot )”? Does it remove the problematic part next to \begin{tikzpicture} or does it make that part no longer problematic?

I would suggest typing it at the REPL and will give you the code that needs to be put before `\begin{document}` in the LaTeX file in order for the plot to work as written in the `tikzpicture` environment. So execute the command where `plot` is a name for a plot object like e.g. `plot = Plots.plot(1:5)` ( choose a better name than me ^^;).

> [@juliauser](#):
>
> By reading a lot of the pyplot() blogs it seems that the accepted solution for multiple levels of plots and labels is creating empty plots with only a title and to stack them all together. This solution does not work well with pgfplotsx(). In particular, the empty plot still draws the ticks in the x and y-axis.

I don’t really get, what you are trying to do here. Can you give me the series of commands you ran and what your expected outcome is?

> [@juliauser](#):
>
> When you run plot and then overlay plot! multiple times, you need to run the ribbon last or it messes the legend

That is fixed on my branch now.

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [December 10, 2019, 7:11pm UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/31 "2019-12-10T19:11:57Z")

</div>

Here is an example

```julia
plot1 = scatter(rand(10),rand(10))
plot2 = scatter(rand(10),rand(10))
subtitle1 = plot(title = "Subtitle1", grid = false, showaxis = false, bottom_margin = -10Plots.px,top_margin = 1Plots.px)
subtitle2 = plot(title = "Subtitle2", grid = false, showaxis = false, bottom_margin = -10Plots.px,top_margin = 1Plots.px)
title = plot(title = "Title", grid = false, showaxis = false, bottom_margin = -10Plots.px,top_margin = 1Plots.px)
l = @layout [a{0.01h}; b{0.1h,0.5w} c{0.1h,0.5w}; d{1h,0.5w} e{1h,0.5w}]
plot(title,subtitle1,subtitle2,plot1,plot2,layout = l,size=(3000,10000))

```

What this should do is plotting plot1 and plot2 next to each, put subtitle1 and subtitle2 on top of each and title in the middle on top of the figure.

This is a workaround in the pyplot() world, but of course it would be great if there was a less convoluted way of having layers of titles

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [December 11, 2019, 9:20am UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/32 "2019-12-11T09:20:39Z")

</div>

So at the end you want something like [this](https://tex.stackexchange.com/questions/51002/how-can-a-title-be-placed-for-a-group-of-pgfplots), if I got you right.  
So the longterm solution will probably be to make Plots more transparent for the backend, such that you could add that `node` entry by hand. And maybe I should think about implementing the layouting in terms of nodes instead of groupplots anyway.  
For now (and likely at least until March next year) using the `@layout` macro won’t work with the `pgfplotsx`-backend.

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [December 11, 2019, 9:51am UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/33 "2019-12-11T09:51:25Z")

</div>

Also consider postprocessing the latex file, if you include it in a LaTeX document anyway.

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [December 18, 2019, 3:35pm UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/34 "2019-12-18T15:35:54Z")

</div>

Yes, post processing is an option but I think there is a huge benefit from not having to touch the code. Let me ask a quick question.

what is the way you recommend for having 2 plots next to each other, each with a title, and then an overall title for the picture? This seems an important starting point to me.

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [December 24, 2019, 9:17am UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/35 "2019-12-24T09:17:53Z")

</div>

A small detail:  
in yerror the CI are black, but the horizontal ticks at the extremes of the CI are transparent. It would be nice if they were the same color as the marker used for the point estimate (like in pyplot()). Please let me know if this is not clear.

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [December 31, 2019, 11:31am UTC](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440/36 "2019-12-31T11:31:54Z")

</div>

> [@juliauser](#):
>
> Also, some marker types like star5 do not get colored: they appear light gray no matter which marker color is chosen.

I can’t confirm that. Try ` scatter(1:5, marker=:star5, markerstrokecolor = :red)`.  
I also don’t see the `yerror` issue, can you provide a MWE?

[Previous page](https://discourse.julialang.org/t/confidence-interval-with-plots-and-pgfplots/31440.md?page=1)
