# Plotting a filled contour and saving as eps or svg

**URL:** <https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152>\
**Category:** General Usage\
**Tags:** images, plotting\
**Created:** [September 19, 2018, 1:48am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152 "2018-09-19T01:48:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 19, 2018, 1:48am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/1 "2018-09-19T01:48:06Z")

</div>

I would like to simply plot a filled contour for given `x`, `y`, and `z` variables (or a function `z(x,y)`), and save it to eps (or svg). My goal is to make a [GitHub repo](https://github.com/briochemc/JuliaPlotting/blob/master/plot_a_contour_of_peaks.jl) of simple plotting examples that export to eps, svg, pdf, etc., that I can then use as a basis for making publication-quality figures. I believe getting such simple examples out there will help the Julia users who struggle for simple tasks like me 🙂 Anyway, here is my current non-working MWE using julia v0.7 (it’s in the repo too):

```julia
using Pkg
Pkg.activate(".")

function peaks(x, y)
    z = 3 * (1 - x)^2 * exp(-x^2 - (y + 1)^2)
    z += -10 * (x / 5 - x^3 - y^5) * exp(-x^2 - y^2)
    z += -1/3 * exp(-(x+1)^2 - y^2)
    return z
end

x = range(-3, stop = 3, length = 100)
y = range(-2, stop = 2, length = 80)
levels = collect(-1:0.2:1)

using Plots
# using PyPlot
# using Plotly, ORCA
plt = contourf(x, y, peaks; levels = levels)
savefig(plt, "test.eps")

```

Now I can comment/uncomment the package (or backend) being used. The code above is in a file called `plot_a_contour_of_peaks.jl` and lives in a project where I have added the packages `Plots`, `PyPlot`, `Plotly`, and `ORCA`.

I then run (julia version 0.7)

```bash
julia> include("plot_a_contour_of_peaks.jl")

```

and get the following errors. I’m looking for help to figure out how to solve these errors!

- using `Plots` (for which the default backend is `GR.jl` if I understand correctly)

- using `PyPlot.jl`

- using `Plotly` and `ORCA`:

```julia
    ERROR: LoadError: UndefVarError: contourf not defined
    Stacktrace:
     [1] top-level scope at none:0
     [2] include at ./boot.jl:317 [inlined]
     [3] include_relative(::Module, ::String) at ./loading.jl:1038
     [4] include(::Module, ::String) at ./sysimg.jl:29
     [5] include(::String) at ./client.jl:398
     [6] top-level scope at none:0
    in expression starting at /Users/benoitpasquier/Projects/JuliaPlotting/plot_a_contour_of_peaks.jl:18

```

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [September 19, 2018, 2:27am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/2 "2018-09-19T02:27:18Z")

</div>

The first error reads as though it didn’t recognise the .eps filetype, so you could try `savefig` with a .svg / .html or .(postscript?) typed filename  
Third error says that the `contourf` function wasn’t found, so the plotly docs may have some more info on contours

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 19, 2018, 4:40am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/3 "2018-09-19T04:40:25Z")

</div>

Thank you for chiming in!

> The first error reads as though it didn’t recognise the .eps filetype, so you could try `savefig` with a .svg / .html or .(postscript?) typed filename

Yes, I can save this as a `.svg` and `.html` file. However, the `.eps` format seems to currently be the preferred format for publication in the scientific literature. But maybe I am wrong about that? I am also sure there are some workarounds. But I’d like to reiterate what I am trying to do here: I would like a simple MWE of plotting a filled contour in Julia and exporting it to a _flawless_ figure in `.eps` format ready for being included in a LaTeX document for publication. The reason I emphasized “flawless” is because there is a problem with the `GR` backend that I forgot to mention—it does not do filled contours. Instead, `GR`’s `contourf` shows a heatmap. (I was just told about this on slack a few hours ago.)

> Third error says that the `contourf` function wasn’t found, so the plotly docs may have some more info on contours

I’ll try to work this one out.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 19, 2018, 5:57am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/4 "2018-09-19T05:57:19Z")

</div>

So I got `Plotly.jl` to save an `.eps` but the background is all black for some reason. Note that this requires the `ORCA.jl` package, which does not work with `using Plots` and the `plotly()` backend—It needs to be `using Plotly` directly and the arguments for `ORCA.savefig` must be of the `PlotlyBase.Plot` types specifically.

I am giving up for now. If somebody has a MWE of julia code in v0.7+ plotting a filled contour given `x`, `y`, `z`, and `levels`, and exporting it into a flawless `.eps` (flawless = usable for publication)—I am looking for that.

But so far it seems my best solution is to learn how to transfer julia-created data to a python readable format and then (learn and) use python to plot things. Or figure out a way to transfer that data to MATLAB because MATLAB just works the expected way in this case:

```nohighlight
contourf(X, Y, Z, levels)
export_fig("test.eps")

```

I find myself spending way too much time trying to figure out how to plot basic figures, and this is not encouraging considering that I fear the real problems and complications will come when I will want to add details to these plots.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 19, 2018, 6:38am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/5 "2018-09-19T06:38:32Z")

</div>

So I did not give up in the end, and figured out a way with PyPlot. New MWE:

```julia
using Pkg
Pkg.activate(".")

function peaks(x, y)
    z = 3 * (1 - x)^2 * exp(-x^2 - (y + 1)^2)
    z += -10 * (x / 5 - x^3 - y^5) * exp(-x^2 - y^2)
    z += -1/3 * exp(-(x+1)^2 - y^2)
    return z
end

x = range(-3, stop = 3, length = 100)
y = range(-2, stop = 2, length = 80)
levels = collect(-4:0.25:4)
vmin = minimum(levels)
vmax = maximum(levels)

using Plots
pyplot() # Set the backend to PyPlot
plt = contourf(x, y, peaks, levels = levels, clim = (vmin, vmax))
display(plt)
savefig(plt, "test.eps")
savefig(plt, "test.png")
savefig(plt, "test.svg")
savefig(plt, "test.html")

```

And now the real fight begins for tweaking it to my liking 😕

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 19, 2018, 6:41am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/6 "2018-09-19T06:41:35Z")

</div>

> [@briochemc](#):
>
> publication-quality figures

I don’t know if you are aware of PGFPlotsX:

> **[GitHub - KristofferC/PGFPlotsX.jl: Plots in Julia using the PGFPlots LaTeX...](https://github.com/KristofferC/PGFPlotsX.jl)**
>
> Plots in Julia using the PGFPlots LaTeX package. Contribute to KristofferC/PGFPlotsX.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 19, 2018, 6:49am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/7 "2018-09-19T06:49:37Z")

</div>

I thought I read somewhere that PGFPlots did not do well with filled contours - and I cannot find an example of it either…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 19, 2018, 7:53am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/8 "2018-09-19T07:53:33Z")

</div>

I am generally wary of filled contours because of [low data-ink ratio](https://infovis-wiki.net/wiki/Data-Ink_Ratio), but it [should be possible natively in pgfplots](https://tex.stackexchange.com/a/130888). If you want to contribute a demo graph, please make a PR or just post it here and I will do it.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [September 19, 2018, 9:03am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/9 "2018-09-19T09:03:26Z")

</div>

Out of a certain experience i have some doubts, that

> [@briochemc](#):
>
> _flawless_ figure in `.eps` format

exists, but still, you need vector graphics with reasonable support for fonts.  
SVG 2 EPS exists in various flavours - based on librsvg or inkscape. [GitHub - lobingera/Rsvg.jl: adaptation of the rsvg lib](https://github.com/lobingera/Rsvg.jl) can be used for eps output, within julia. And yes, there might be font issues.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 19, 2018, 10:33pm UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/10 "2018-09-19T22:33:21Z")

</div>

Just to be clear, I meant flawless in showing the data, not the fonts. (I personally do not really care that much about the fonts as long as I can have a LaTeX one.) What I mean by flawless is that I want my filled contour plot to:

- match the colorbar exactly
- be vectorized (not a rasterized image),
- with the contour fillings inside of polygons that are at least as fine as the data
- not show artefact lines appearing between fillings or inside polygons (A major issue with MATLAB’s plotting since version 2014b I think)

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 19, 2018, 10:43pm UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/11 "2018-09-19T22:43:06Z")

</div>

I strongly disagree with the data-ink ratio argument against filled contours. Evaluating the color of a line is much harder than the color of a somewhat thicker filling. Actually in (most?) cases, the filling can be physically interpreted as a coloring of the data. On the other hand, non-filled contours require an added interpretation for the reader—that the data between contours must be between values of contours surrounding it. Additionally non-filled contours can be ambiguous (e.g., a single contour will not give any information on the gradients). Finally, overall, filled-contours are easier to interpret, and (personally) aesthetically gentler on the eyes.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 20, 2018, 7:25am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/12 "2018-09-20T07:25:45Z")

</div>

> [@briochemc](#):
>
> Evaluating the color of a line is much harder than the color of a somewhat thicker filling.

Certainly. This is why many packages just make them B&W and add labels on the contour lines by default.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [September 20, 2018, 12:53pm UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/13 "2018-09-20T12:53:12Z")

</div>

just for my curiosity: What tool does fit to all your points? And can you reference an example?

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 20, 2018, 11:35pm UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/14 "2018-09-20T23:35:26Z")

</div>

> [@lobingera](#):
>
> just for my curiosity: What tool does fit to all your points? And can you reference an example?

Well I figured it out using PyPlot I think, and I keep updating some tiny examples in the repo I linked in the first post [(this one)](https://github.com/briochemc/JuliaPlotting/blob/master/plot_a_contour_and_a_path_on_top.jl). My goal is to use this repo as a little personal archive of julia MWEs for plotting that do what I want and can output to `.eps` for publication.

Otherwise before that I was using MATLAB, but it involved doing a lot of tweaking. (For example, you can find examples of what I did with MATLAB in [this paper, Figures 2, 5, 6, 8, 9, 10, 11, A1, and C1](https://www.biogeosciences-discuss.net/bg-2018-379/bg-2018-379.pdf))

[edit: I did not mean this to be a reply to Tamas but I don’t know how to remvove that part of my post]

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [September 20, 2018, 11:57pm UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/15 "2018-09-20T23:57:04Z")

</div>

That’s peanuts for GMT (whose output format is PostScript). Just a bit more elaborated [example](https://github.com/GenericMappingTools/GMT.jl/blob/master/WL_example_3.pdf)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 20, 2018, 11:58pm UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/16 "2018-09-20T23:58:00Z")

</div>

My take is that EPS is obsolete in the scientific literature and everywhere accepts PDF now.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 21, 2018, 2:41am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/17 "2018-09-21T02:41:38Z")

</div>

> [@dpsanders](#):
>
> My take is that EPS is obsolete in the scientific literature and everywhere accepts PDF now.

I guess you are right… I did not know about this. If you don’t mind me asking, what are the advantages of PDF over EPS? I just thought that I wanted a vectorized format (but not SVG because that’s not generally accepted by journals), so I have been using EPS for a little while now. But I shall print everything in PDF from now on 🙂 Thanks for the suggestion!

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 21, 2018, 2:45am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/18 "2018-09-21T02:45:48Z")

</div>

> [@joa-quim](#):
>
> That’s peanuts for GMT (whose output format is PostScript). Just a bit more elaborated [example](https://github.com/GenericMappingTools/GMT.jl/blob/master/WL_example_3.pdf)

Oh I did not know about GMT, thanks for the suggestion. However, I think your example is actually quite representative of what I do **not** want: Contours in the colorbars that do not match the data being plotted, and the data is a rasterized heatmap rather than vectorized filled contours.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [September 21, 2018, 2:52am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/19 "2018-09-21T02:52:04Z")

</div>

I didn’t intend to say that the example was exactly what you were looking for. It doesn’t even have contours. If you want contours see [simple example](https://genericmappingtools.github.io/GMT.jl/latest/examples/#Simple-contours-1). That one doesn’t fill the contours but could be made to do it.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 21, 2018, 5:29am UTC](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152/20 "2018-09-21T05:29:54Z")

</div>

> [@briochemc](#):
>
> what are the advantages of PDF over EPS?

EPS is mostly Postscript + some extras, so [this comparison](https://en.wikipedia.org/wiki/PDF#PostScript) may be informative. PS is effectively a programming language with global state etc, that renders the output, whereas PDF, which can be considered its successor, is effectively data about paths, allowing programs to interpret and edit it in an easier way. Also, PDF has some other nifty features for fonts, transparency, color management, etc.

[Next page](https://discourse.julialang.org/t/plotting-a-filled-contour-and-saving-as-eps-or-svg/15152.md?page=2)
