# Create individual plot and save as pdf

**URL:** <https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458>\
**Category:** New to Julia\
**Tags:** plots, gr, pdf-format\
**Created:** [February 14, 2022, 8:51pm UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458 "2022-02-14T20:51:16Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 14, 2022, 8:51pm UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/1 "2022-02-14T20:51:17Z")

</div>

Hi … i used to use this code in r studio to generate individual plots for each id and save them as combined pdf for the offline evaluation. can someone help me to translate code to julia.

```julia
pdf("path\\indplots.pdf")
for(i in unique(df$ID))
{
  print((ggplot(subset(df,df$ID == i),aes(x=TAD,y=DV))+
           geom_point()+
           xlab("TAD (hrs)")+
           ylab("Plasma concentration (mg/L)")+
           ggtitle(paste(as.character(i)))+
           theme(panel.grid.minor=element_blank(),
                 panel.grid.major=element_blank(),
                 panel.background = element_blank(),
                 plot.title=element_text(hjust=0.5),
                 legend.position="right",
                 legend.key=(element_rect(colour='white',fill='white')),
                 plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"),
                 legend.text=element_text(colour="black",size=10),
                 axis.text.x=element_text(colour="black",size=10),
                 axis.text.y=element_text(colour="black",size=10),
                 axis.title=element_text(colour="black",size=14),
                 axis.line.x = element_line( colour="black",size=1,linetype ="solid"),
                 axis.line.y = element_line( colour="black",size=1,linetype ="solid"))))
}
dev.off()

```

here i am focusing more on saving the individual plots as combined pdf ,than the codes to customise plot.  
Thanks.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 15, 2022, 12:36am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/2 "2022-02-15T00:36:47Z")

</div>

Maybe this?

> [@\[ANN\] PDFmerger.jl: produce multiple plots on separate pages in one PDF file](https://discourse.julialang.org/t/ann-pdfmerger-jl-produce-multiple-plots-on-separate-pages-in-one-pdf-file/74788):
>
> Hi all, I’m happy to announce the little [PDFmerger.jl](https://github.com/scheidan/PDFmerger.jl) package to merge PDF (Portable Document Format) files. It is tested on Linux, OS X, and Windows and does not require (manual) installation of external tools. Besides simple installation, it provides a convenient way to produce a PDF file containing multiple plots on different pages. This topic [came up before](https://discourse.julialang.org/t/save-multiple-plots-on-separate-pages-of-one-pdf/41953) and there is an [open issue](https://github.com/JuliaPlots/Plots.jl/issues/2908) for Plots.jl. using Plots using PDFmerger: append\_pdf! for t in 1:5 p = plot(rand(33), title="$t") …

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [February 15, 2022, 9:13am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/3 "2022-02-15T09:13:05Z")

</div>

That would be a `GR` solution:

```julia
using GR
beginprint("plots.pdf")
for i in 1:10 plot(randn(10,4)) end
endprint()

```

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 15, 2022, 10:06am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/4 "2022-02-15T10:06:47Z")

</div>

```julia
julia> using GR

julia> using Plots

julia> beginprint("plots.pdf")

julia> for i in 1:10 plot(randn(10,4)) end
ERROR: UndefVarError: plot not defined
Stacktrace:
 [1] top-level scope
   @ ./none:1

julia> endprint()

```

may i get some help, my plots stopped working here !.

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [February 15, 2022, 10:19am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/5 "2022-02-15T10:19:37Z")

</div>

`Plots.jl` doesn’t support it natively. But you can trick `Plots`:

```julia
using Plots

Plots.GR.beginprint("plots.pdf")
gr(show=true)

for i in 1:10 plot(randn(10)) end

Plots.GR.endprint()

```

Not very pretty, but works.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [February 15, 2022, 10:28am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/6 "2022-02-15T10:28:24Z")

</div>

I encountered the following error using your code (GR version: 0.63.1):

```julia
SystemError: opening file "C:\\Users\\ytian\\AppData\\Local\\Temp\\jl_SbuQV4fEbT.svg": No such file or directory

Stacktrace:
  [1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
    @ Base .\error.jl:168
  [2] #systemerror#62
    @ .\error.jl:167 [inlined]
  [3] systemerror
    @ .\error.jl:167 [inlined]
  [4] open(fname::String; lock::Bool, read::Bool, write::Nothing, create::Nothing, truncate::Nothing, append::Nothing)
    @ Base .\iostream.jl:293
  [5] open(fname::String, mode::String; lock::Bool)
    @ Base .\iostream.jl:355
  [6] open
    @ .\iostream.jl:355 [inlined]
  [7] _readfile(path::String)
    @ GR C:\Users\ytian\.julia\packages\GR\KPElO\src\GR.jl:3710
  [8] show()
    @ GR C:\Users\ytian\.julia\packages\GR\KPElO\src\GR.jl:3768
  [9] plot_data(flag::Bool)
    @ GR.jlgr C:\Users\ytian\.julia\packages\GR\KPElO\src\jlgr.jl:1553
 [10] plot_data
    @ C:\Users\ytian\.julia\packages\GR\KPElO\src\jlgr.jl:1198 [inlined]
 [11] plot(args::Matrix{Float64}; kv::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ GR.jlgr C:\Users\ytian\.julia\packages\GR\KPElO\src\jlgr.jl:1763
 [12] plot
    @ C:\Users\ytian\.julia\packages\GR\KPElO\src\jlgr.jl:1755 [inlined]
 [13] top-level scope
    @ .\In[2]:4
 [14] eval
    @ .\boot.jl:360 [inlined]
 [15] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base .\loading.jl:1116

```

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 15, 2022, 10:58am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/7 "2022-02-15T10:58:03Z")

</div>

> [@jheinen](#):
>
> ```julia
> using Plots
> 
> Plots.GR.beginprint("plots.pdf")
> gr(show=true)
> 
> for i in 1:10 plot(randn(10)) end
> 
> Plots.GR.endprint()
> 
> ```

still same error

```julia
julia> using Plots

julia> Plots.GR.beginprint("plots.pdf")

julia> gr(show=true)
Plots.GRBackend()

julia> for i in 1:10 plot(randn(10)) end
ERROR: UndefVarError: plot not defined
Stacktrace:
 [1] top-level scope
   @ ./none:1

julia> Plots.GR.endprint()

```

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [February 15, 2022, 11:04am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/8 "2022-02-15T11:04:35Z")

</div>

Please use the following:

```julia
for i in 1:10 GR.plot(randn(10)) end

```

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [February 15, 2022, 11:15am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/10 "2022-02-15T11:15:34Z")

</div>

> [@photor](#):
>
> Please use the following:
> 
> ```julia
> for i in 1:10 GR.plot(randn(10)) end
> 
> ```

No, that was not the idea. You probably forgot to restart Julia and “used” both GR and Plots, which cannot work.

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 15, 2022, 11:16am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/11 "2022-02-15T11:16:09Z")

</div>

Thank you, it works now.  
my purpose is to get a separate for each indiviadul . but this code gives me a plot od all values in single page . please rectify the code  
this is the code i have used

```julia
df = DataFrame(id = 1:5, a = 10:14, b = 5:9)
Plots.GR.beginprint("plots.pdf")
gr(show=true)
for i in df.id GR.plot(df.a,df.b) end
Plots.GR.endprint()

```

i want df.a vs df.b plot for each id in a separate pages of pdf file.  
Thank you

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [February 15, 2022, 11:20am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/12 "2022-02-15T11:20:34Z")

</div>

```julia
using DataFrames
using Plots
gr(show=true)
df = DataFrame(id = 1:5, a = 10:14, b = 5:9)
Plots.GR.beginprint("plots.pdf")
for i in df.id plot(df.a,df.b .* i) end
Plots.GR.endprint()

```

The result is **one** PDF with **5** pages.

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 15, 2022, 11:28am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/13 "2022-02-15T11:28:33Z")

</div>

i copied the excat code , not undersating why plots are not working

```julia
julia> using DataFrames

julia> using Plots

julia> gr(show=true)
Plots.GRBackend()

julia> df = DataFrame(id = 1:5, a = 10:14, b = 5:9)
5×3 DataFrame
 Row │ id a b     
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 1 10 5
   2 │ 2 11 6
   3 │ 3 12 7
   4 │ 4 13 8
   5 │ 5 14 9

julia> Plots.GR.beginprint("plots.pdf")

julia> for i in df.id plot(df.a,df.b .* i) end
ERROR: UndefVarError: plot not defined
Stacktrace:
 [1] top-level scope
   @ ./none:1

julia> Plots.GR.endprint()

```

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [February 15, 2022, 11:33am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/14 "2022-02-15T11:33:08Z")

</div>

Are you in a notebook? Did you really restart the Julia kernel? Or, do you “auto-load” packages in your startup files?

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 15, 2022, 11:48am UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/15 "2022-02-15T11:48:51Z")

</div>

im on atom. i have comletely closed and open the julia . still the same result

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 15, 2022, 12:49pm UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/16 "2022-02-15T12:49:05Z")

</div>

this worked after changing the plot to Plots.plot.  
but all the 5 pages have same plot.  
plot i want is ,  
10 on x , 5 on y for id 1 in first page,  
11 on x , 6 on y for id 2 in second page  
.  
.  
14 on x , 9 on y for id 5 in last page.

just plots for individual id’s.

is there a way to get that. Thank you

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [February 15, 2022, 12:58pm UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/17 "2022-02-15T12:58:29Z")

</div>

Sorry - can’t help furthermore as I cannot reproduce the behaviour on any platform.

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 15, 2022, 12:59pm UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/18 "2022-02-15T12:59:43Z")

</div>

thank you very much for your support.

can you say , how you got plots.  
each id was printed in separate page ?

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [February 15, 2022, 1:21pm UTC](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/19 "2022-02-15T13:21:36Z")

</div>

Could you help figure out the problem in this post:

> [@Create individual plot and save as pdf](https://discourse.julialang.org/t/create-individual-plot-and-save-as-pdf/76458/6):
>
> I encountered the following error using your code (GR version: 0.63.1): SystemError: opening file "C:\\Users\\ytian\\AppData\\Local\\Temp\\jl\_SbuQV4fEbT.svg": No such file or directory Stacktrace: [1] systemerror(p::String, errno::Int32; extrainfo::Nothing) @ Base .\error.jl:168 [2] #systemerror#62 @ .\error.jl:167 [inlined] [3] systemerror @ .\error.jl:167 [inlined] [4] open(fname::String; lock::Bool, read::Bool, write::Nothing, create::Nothing, truncate::Nothing, append::…

Thanks!
