11

The following code is from the "gallery of examples in the manual - PGFPlots".

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        title={$x \exp(-x^2-y^2)$},
        domain=-2:2,enlarge x limits,
        view={0}{90},
    ]
   \addplot3[contour gnuplot={number=14},thick]
          {exp(0-x^2-y^2)*x};
    \end{axis}
\end{tikzpicture}
\end{document}

Unfortunately the code does not work and I get the following error message:

! Package pgfplots Error: sorry, plot file{example_175_contourtmp0.table} could
 not be opened.

See the pgfplots package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.17           {exp(0-x^2-y^2)*x};

? 
! Emergency stop.
 ...                                              

l.17           {exp(0-x^2-y^2)*x};

End of file on the terminal!

I have no idea what could be the problem. It would be great if anbody can offer a solution.

  • 3
    The error message of pgfplots is quite misleading, I fear. It should have been "I was unable to read the output of gnuplot". Problems can be: there is no gnuplot on the system or, (b) gnuplot could not be executed (see the answer of @aeronotauto). The command contour gnuplot is supposed to make a system call (which needs -shell-escape) in order to invoke gnuplot -- in the hope that this command is available. Does you .log file contain any indication of gnuplot invocations or system calls (try searching for these keywords in your .log file)? – Christian Feuersänger May 04 '15 at 19:16
  • I found runsystem(gnuplot "example_175_contourtmp0.script")...disabled (restricted). in the .log file. Maybe this helps you. – user144921 May 04 '15 at 19:37
  • Yes: that means that you need to add -shell-escape (or, in some TeX installations on windows, -enable-write18) to you latex invocation. Please refer to the answer of @aeronotauto for details. – Christian Feuersänger May 06 '15 at 15:51
  • I tried -shell-escape, --shell-escape and -enable-write18. But the error still appears. – user144921 May 08 '15 at 18:19
  • always with ...disabled (restricted) in your log file? – Christian Feuersänger May 10 '15 at 18:35

3 Answers3

10

Copy/pasting that code into ShareLaTeX, it compiles with no errors and produces:

contourPlot

What version are you using? And as a random note, compat=newest is generally not recommended.

EDIT:

I was able to reproduce your problem by compiling with only pdflatex filename. Since LaTeX needs to run gnuplot, you have to allow LaTeX to launch third party executables. For this, you need an argument like --shell-escape. Does it work if you compile your example with pdflatex --shell-escape filename?

EDIT 2:

If you have the files contours_contourtmp0.script and contours_contourtmp0.dat but not contours_contourtmp0.table then chances are, LaTeX was unable to execute gnuplot. You can sort of use the presence of contours_contourtmp0.table to confirm that gnuplot was executed correctly. Also, the example in the user guide will only work if the executable gnuplot exists...I don't know what the Windows version is called, so a Windows user will have to confirm this - that could also be the problem.

aeroNotAuto
  • 2,390
  • I use MikTex 2.9 and WinEdt 7.0. – user144921 May 04 '15 at 17:15
  • @user144921 sorry, i meant version of pgfplots, but i don't really think that's the issue. of course, i must ask the obligatory "are you sure the document you are compiling does not call example_175_contourtmp0.table anywhere?" is that your whole file? i noticed the traceback specifies line 17. – aeroNotAuto May 04 '15 at 17:19
  • Maybe the problem is that there is no file called "example_175_contourtmp0.table". Only a file named "example_175_contourtmp0" which is a DAT-file is created. – user144921 May 04 '15 at 17:25
  • And I think the pgfplots-version is "pgfplots 2015/01/31 v1.12". – user144921 May 04 '15 at 17:27
  • @user144921 i updated the answer, as i was able to reproduce your error – aeroNotAuto May 04 '15 at 17:54
  • I am sorry, but what do you mean with "compile your example with shell-escape filename"? – user144921 May 04 '15 at 18:06
  • @user144921 i typically work on a linux system, so i frequently compile using a terminal command - it's likely different for you. does this question help you do it in windows? – aeroNotAuto May 04 '15 at 18:16
  • Ok, now I added "Switches: --shell-escape example_175.tex" but the error stays exactly the same. – user144921 May 04 '15 at 18:24
  • @user144921 i'm pretty sure --shell-escape or something like it is the issue you're having. i presume you're supposed to enter --shell-escape into the switches field, and not the .tex file name. but this example also requires you to have gnuplot on your system, so that could be a related reason for the failure. i updated my answer to reflect this. you might be better off posting another question that specifically asks for help with using latex to execute gnuplot on windows as a third party application – aeroNotAuto May 04 '15 at 18:46
  • 1
    @user144921 In winedt go to OptionsExecution Modes and in the window that opens choose pdflatex and add --shell-escape like in this picture . Don't forget to press OK button. –  May 05 '15 at 02:02
2

Please go to gnuplot homepage Find the download link to SourceForge. Download Windows version EXE file that installs automatically on Windows. It is for all versions of Windows, the same. Save it on your computer.

Run it as administrator (right click run as administrator). When shows installation options do not forget to tick the checkbox for "Add Path to Environment Variables." Let the software does it rather than doing it manually.

Also use its default definition for the terminal.

Allow it to run for the first time when it asks after finishing install by checking the "Run gnuplot" box. Then close it. Close everything and reboot your computer.

Now run your tex editor such as my TexnicCenter as administrator, recompile your tex file and it should eliminate the error. In case add --shell-escape as advised by others. It is not necessary for DVI compile, though; it can creates unresolved warning from gnuplot. In such case use -enable-write18, instead as described in page 37 of "Manual for Package pgfplots" by Dr. Christian Feuersanger.

0

In 2023 I think the recommended best practice is to use the luatex engine if possible. Then you just need to change contour gnuplot to contour lua and it Just Works without needing to run an external problem.

Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195