LaTeX forum ⇒ Graphics, Figures & TablesUsing Gnuplot in Tikz environment Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
Latrace
Posts: 2
Joined: Tue Jul 10, 2012 6:14 pm

Using Gnuplot in Tikz environment

Postby Latrace » Thu Aug 09, 2012 5:21 am

Hello,

There has been a post related to this subject in the past, but it didn't really address my problem.

The general problem I have is plotting functions in LaTeX. I could of course import an image from Maple or Mathematica, but now I'm acquainted a bit with the TikZ environment which can make very nice images, I'd like to try it by only using my editor (I work on a Mac, with Texpad as an editor).

One possibility I read about was using pgfplots (something already included in TikZ) to draw functions, another was using gnuplot (which I downloaded, I have version 4.6.0). Here are two minimal working examples (using pgfplots and gnuplot respectively):

\begin{tikzpicture}
	\draw[->] (0,0) -- (4,0) node [right] {$x$};
	\draw[->] (0,0) -- (0,4) node [right] {$y$};
	\draw plot[id=x, domain=0:4] function{sqrt(9 - x*x)};
\end{tikzpicture}


\begin{tikzpicture}
\begin{axis}[axis x line=middle, axis y line=middle, xlabel=$x$,ylabel=$y$]
\addplot[domain=0:5] {sqrt(9-x^2)};
\end{axis}
\end{tikzpicture}


One thing that concerns me is the roughness of both curves; they are not smooth. I read somewhere that a good reason to use gnuplot is it's capability to produce smooth curves (many points). How can I get the curves to be smooth?

Another thing I'm wondering about is how to get the pgfplots-plot to look exactly like the gnuplot-plot (without the numbers and ticks on the axes). I tried fiddling around a bit with this, but no success.

Thanks a lot,
Latrace
Attachments
Screen Shot 2012-08-08 at 23.10.29.png
The result using pgfplots
Screen Shot 2012-08-08 at 23.10.29.png (16.18 KiB) Viewed 6131 times
Screen Shot 2012-08-08 at 23.01.06.png
The result using gnuplot
Screen Shot 2012-08-08 at 23.01.06.png (10.77 KiB) Viewed 6131 times

Recommended reading 2021:

LaTeXguide.org • LaTeX-Cookbook.net
LaTeX Beginner's Guide LaTeX Cookbook
User avatar
Stefan Kottwitz
Site Admin
Posts: 9947
Joined: Mon Mar 10, 2008 9:44 pm

Using Gnuplot in Tikz environment  Topic is solved

Postby Stefan Kottwitz » Thu Aug 09, 2012 10:57 am

Hi Latrace,

welcome to the board!

You could specify the number of samples which shall be used for plotting. Furthermore, you could use smooth paths for connecting the plot points, instead of straight lines:

\draw plot[id=x, domain=0:4, samples=100, smooth] function{sqrt(9 - x*x)};


Stefan
LaTeX.org admin

Latrace
Posts: 2
Joined: Tue Jul 10, 2012 6:14 pm

Re: Using Gnuplot in Tikz environment

Postby Latrace » Thu Aug 09, 2012 5:40 pm

Thank you! That gives a nice curve.

Latrace

feuersaenger
Posts: 34
Joined: Sun Oct 16, 2011 5:56 pm

Using Gnuplot in Tikz environment

Postby feuersaenger » Tue Aug 14, 2012 8:27 pm

Hi Latrace,

The first part of your question concerning the roughness has already been addressed by Stefan (and since PGFPlots also accepts any TikZ option including smooth and samples=N, it applies to both pgfplots and tikz).

I want to complement his answer by adding information for the axis fine tuning in pgfplots.

In order to remove tick marks, you can add xtick=\empty. The special value \empty results in an empty list of tick positions.

The label positions can be adjusted like any other TikZ node (and the xlabel style key).

Furthermore, your TikZ example uses the same unit vectors for both X and Y - pgfplots can do the same using axis equal and axis equal image (the latter is what you want here). You could also write x=1cm,y=1cm, but it is typically easier to provide width,height,axis equal.

Here is the result:

P.png
P.png (3.28 KiB) Viewed 6076 times


\documentclass{article}
\usepackage{tikz,pgfplots}

% this here is, in general, good practice - use the most recent
% pgfplots stable here:
\pgfplotsset{compat=1.5}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
	axis x line=middle, 
	axis y line=middle, 
	xlabel=$x$,ylabel=$y$,
	xtick=\empty,  % magic value for "no ticks"
	ytick=\empty,
	xlabel style={
		at={(current axis.right of origin)},anchor=west,
	},
	ylabel style={
		at={(current axis.above origin)},anchor=south west,
	},
	% "upper" will leave the lower limits intact as found in the
	% coordinates:
	enlargelimits={upper},
	%
	% to get X and Y units of the same length:
	axis equal image,%
	% note that 'axis equal' would enlarge all limits in order to maintain
	% the image width (in contrast to axis equal image)
	]
	\addplot[domain=0:3,smooth,samples=100] {sqrt(9-x^2)};
\end{axis}
\end{tikzpicture}
\end{document}


Best regards

Christian

PS
Note that I used domain=0:3 because the square root of negative values is undefined.

JustCurious
Posts: 3
Joined: Wed Mar 15, 2023 9:32 pm

Using Gnuplot in Tikz environment

Postby JustCurious » Wed Mar 15, 2023 9:44 pm

I've recently installed TexLive and cannot get proper plots using tkz-ftc. Prior to finding this site, I learned the problem has to do with accessing gnuplot. Ignoring that for now, the example shown on this page seems to have the same issue when"Run Latex" is clicked. Is there a reason for that?

JustCurious
Posts: 3
Joined: Wed Mar 15, 2023 9:32 pm

Using Gnuplot in Tikz environment

Postby JustCurious » Thu Mar 16, 2023 5:59 am

I've added a reply since my system's issue was solved by the paragraph below, but I think there's still a problem on this site?

"Changing shell-escape from restricted to unrestricted mode worked. In TexLive you can do so by adding shell_escape = t to the texmf.cnf file in your texlive folder. Alternatively, you can modify the shell_escape_commands option in texmf-dist/web2c/texmf.cnf." "from topic Using gnuplot in tikzpicture environment using pgf-package" at the Tex StackExchange site.

User avatar
Stefan Kottwitz
Site Admin
Posts: 9947
Joined: Mon Mar 10, 2008 9:44 pm

Using Gnuplot in Tikz environment

Postby Stefan Kottwitz » Thu Mar 16, 2023 9:54 am

Good question but there's no problem on this site, in the forum. First, we simply use the TeXlive.net backend instead of having TeX or gnuplot here on this site. Second, gnuplot is an external program, and running external programs is disabled by default for security reasons. While it's fine for you and me to compile with shell-escape enabled, so TeX can run external programs on our own machine when we compile, it's probably not a good idea do allow running external programs on a public server where the whole world can access it, and could call Linux programs from the machine, or upload a script (with the filecontents environment) and run that, etc.

Stefan
LaTeX.org admin

JustCurious
Posts: 3
Joined: Wed Mar 15, 2023 9:32 pm

Using Gnuplot in Tikz environment

Postby JustCurious » Thu Mar 16, 2023 6:03 pm

@Stefan:
Thanks for the clarification. I'm using your beginner's book on Latex and, assuming I'm still alive and aware of my surroundings, I intend to satisfy my curiosity about your upcoming book, "LATEX Graphics with TikZ", by buying it also. (The cookbook looks a little too detailed for the merely curious!)

User avatar
Stefan Kottwitz
Site Admin
Posts: 9947
Joined: Mon Mar 10, 2008 9:44 pm

Using Gnuplot in Tikz environment

Postby Stefan Kottwitz » Thu Mar 16, 2023 11:37 pm

Great to hear that you read my book! As you are curious, I can send you the LaTeX Cookbook as ebook for free (PDF, EPUB, Kindle, downloadable via account at the publisher website), just write me an email to stefan@latex.org for details. That helps with curiosity until the TikZ book is released in June or July. ;-) I still have some copies that I reserved as a present for new DANTE members one year ago https://twitter.com/TeXgallery/status/1 ... 7441569792.

Stefan
LaTeX.org admin


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 3 guests