Graphics, Figures & TablesAdding lines to plots

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Adding lines to plots

Post by jhapk »

Hi,

I have the following code

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

%% common properties for both plots
\pgfplotsset{
  xmin = 0,
  xmax = 600,
  ymin = 0.0,
  table/x=np,
  xlabel=Number of processors,
  scale only axis
}

\begin{tikzpicture}
  
  %% details for first plot with y-axis on left
  \begin{axis}[
      ymax= 600,
      ylabel= Speedup,
      axis y line=left,
      table/y=speedup
    ]
    \addplot {x}; %% this line is not working. I want a line y=x in the same plot
    \addplot [mark=*,dashed] table {scaling.dat};
  \end{axis}

  %% details for second plot with y-axis on right
  \begin{axis}[
      ymax= 1,
      ylabel=Efficiency,
      axis y line=right,
      table/y=efficiency
    ]
    \addplot [mark=*,dashed] table {scaling.dat};
  \end{axis}

\end{tikzpicture}
\end{document}
to plot the following data

Code: Select all

np  speedup   efficiency
1   1         1
10  9.26      0.926
20  18.73     0.9365
50  43.76     0.8752
100 85.56     0.8556
200 173.72    0.8686
565 441       0.735

I want to insert a reference line Y=X in this plot. Simply adding \addplot{x} in the code is not doing it. Also, I am not able to figure how to remove the arrows from the Y-axis which comes when I start using Two-ordinates.

Thanks
Last edited by jhapk on Tue Nov 02, 2010 10:47 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Adding lines to plots

Post by localghost »

jhapk wrote:[…] I want to insert a reference line Y=X in this plot. Simply adding \addplot{x} in the code is not doing it. […]
Which version of Gnuplot do you use? pgf/tikZ 2.00 is not prepared to work with Gnuplot 4.4.x and needs to be patched. pgf/tikZ 2.10 fixes this issue. It has been released some does ago and been uploaded to CTAN a bit later but isn't yet integrated into MiKTeX.
jhapk wrote:[…] Also, I am not able to figure how to remove the arrows from the Y-axis which comes when I start using Two-ordinates. […]
The arrows are default when using a left or right aligned ordinate (but don't ask me why). You need to modify the style of the y-axes as described in Section 4.8.9 of the pgfplots manual. See code below.

Code: Select all

\documentclass{minimal}
\usepackage{pgfplots}

% common properties for both plots
\pgfplotsset{%
  xmin=0,
  xmax=600,
  ymin=0,
  table/x=np,
  xlabel={Number of processors},
  scale only axis
}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[%  %% details for first plot with y-axis on left
      ymax=600,
      ylabel=Speedup,
      axis y line=left,
      every outer y axis line/.style={-},
      table/y=speedup
    ]
      \addplot {x}; %% this line is not working. I want a line y=x in the same plot
      \addplot [mark=*,dashed] table {scaling.dat};
    \end{axis}
    \begin{axis}[%  %% details for second plot with y-axis on right
      ymax=1,
      ylabel=Efficiency,
      axis y line=right,
      every outer y axis line/.style={-},
      table/y=efficiency
    ]
      \addplot [mark=*,dashed] table {scaling.dat};
    \end{axis}
  \end{tikzpicture}
\end{document}
To my surprise a global setup didn't work. So you have to do the setting for each axis environment.


Thorsten
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Adding lines to plots

Post by jhapk »

Hi,

when I type gnuplot on my terminal, it gives

Code: Select all

Version 4.2 patchlevel 5 
Is that not good enough? I am not sure how to check my tikz version.

Thanks
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: Adding lines to plots

Post by localghost »

This version is OK, not the current one (4.4.2), but a working one. So the issue might be that you call the compiler without the option for shell escape. The forum search will yield useful results concerning that matter. The next steps depend on your LaTeX distribution and your operating system.
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Adding lines to plots

Post by jhapk »

I am using the 2009 texlive version and have tried both Ubuntu 9.10 and Fedora 12. A simplified version of my present code is the following

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\pagestyle{empty}
\pgfplotsset{
  xmin = 0,xmax = 565,
  ymin= 0, ymax= 565,
  xlabel=Number of processors,
  legend pos = south east,
  ylabel= Speedup,
  table/x=np,
  table/y=speedup
}
\begin{tikzpicture}
  \begin{axis}
    \addplot {x}; %% this is not working
    \addplot [mark=square*,dashed] table {scaling.dat};
    \legend{Ideal, Algorithm}
  \end{axis}
\end{tikzpicture}
\end{document}
It is giving me the following output
scaling.png
scaling.png (16.53 KiB) Viewed 10240 times
Why is the legend not have filled symbols? Also, I have tried compiling the code with both -shell-escape and without, but I am not getting the y=x line in the plot. I am not sure what to do.

The data file is here

Code: Select all

np   speedup
1    1
10   9.26
20   18.73 
50   43.76 
100  85.56 
200  173.72
565  441
Thanks
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Adding lines to plots

Post by jhapk »

I figured how to add the y=x line. I had to insert the following line in the code

Code: Select all

\addplot gnuplot [domain=0:649, no marks, black] {x};
and run it with pdflatex -shell-escape once.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Adding lines to plots

Post by localghost »

jhapk wrote:[…] I am not sure how to check my tikz version. […]
You can check that by adding the \listfiles command as very first line to your example and looking at the file list in the log.
jhapk wrote:I figured how to add the y=x line. I had to insert the following line in the code […] and run it with pdflatex -shell-escape once.
You have been a little bit faster with your solution. But nevertheless I want to present what I have been working out in the meantime.

Code: Select all

\documentclass{minimal}
\usepackage{pgfplots}

% common properties for both plots
\pgfplotsset{%
  xmin=0,
  xmax=600,
  ymin=0,
  table/x=np,
  xlabel={Number of processors},
  scale only axis
}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[%  %% details for first plot with y-axis on left
      ymax=600,
      ylabel=Speedup,
      axis y line=left,
      every outer y axis line/.style={-},
      table/y=speedup,
      domain=0:600
    ]
      \addplot[smooth] {x};
      \addplot[mark=*,dashed] table {scaling.dat};
    \end{axis}
    \begin{axis}[%  %% details for second plot with y-axis on right
      ymax=1,
      ylabel=Efficiency,
      axis y line=right,
      every outer y axis line/.style={-},
      table/y=efficiency
    ]
      \addplot[mark=*,dashed] table {scaling.dat};
    \end{axis}
  \end{tikzpicture}
\end{document}
Please don't forget to mark the topic as solved if there are no more questions open.
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Adding lines to plots

Post by jhapk »

Hi localghost,
your solution looks good. I still have one unresolved issue. When I insert \legend in your code, I get the following output.
scaling.png
scaling.png (15.84 KiB) Viewed 10224 times
The legend symbols don't agree with the lines in the figure. How do I fix this?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Adding lines to plots

Post by localghost »

That's indeed quite unpleasant. And I can comprehend this behaviour with the current pgfplots version (1.4.1). So I assume that this is a bug thus worth a note to the package maintainer. And it is reproducible with the conceivably simplest example.

Code: Select all

\documentclass{minimal}
\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}
      \addplot[dashed,mark=*] {x};
      \addlegendentry{Function};
    \end{axis}
  \end{tikzpicture}
\end{document}
A possible workaround would be to choose another mark for the plot.
Post Reply