1

I have this two graphs made with pgfplot in ShareLaTex, my advisor wants me to show them in a single graph, the two plots will be recognized by solid and dashed lines respectively. also the legend will define which plot stands for p=0 and which for p=1.

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}

\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
%\usepgfplotslibrary{external}
%\tikzexternalize

\begin{document}

\begin{figure}
Case I : $p=0$ and $n=0$ 

\begin{tikzpicture}
\begin{axis}[ 
title={ Energy density $\rho$},
xlabel={r/R},
    ylabel={$\frac{\kappa}{C} \rho$},
    xmin=0, xmax=1.2,
    ymin=0, ymax=10,
    xtick={0,0.2,0.4,0.6,0.8,1,1.2},
    ytick={0,1,2,3,4,5,6,7,8,9,10}]

\addplot[
    color=blue]
    coordinates {
    (0,8.71)(0.1,8.61)(0.2,8.33)(0.3,7.90)(0.4,7.35)(0.5,6.73)(0.6,6.08)(0.7,5.42)(0.8,4.78)(0.9,4.18)(1,3.61)
    };
    \end{axis}
\end{tikzpicture}
\caption{Hello world}\label{fig:myplot}
\end{figure}


Case II : $ p=1$ and $n=0$

\begin{tikzpicture}
\begin{axis}[ 
title={ Energy density $\rho$},
xlabel={r/R},
    ylabel={$\frac{\kappa}{C} \rho$},
    xmin=0, xmax=1.2,
    ymin=0, ymax=10,
    xtick={0,0.2,0.4,0.6,0.8,1,1.2},
    ytick={0,1,2,3,4,5,6,7,8,9,10}]

\addplot[
    color=blue]
    coordinates {
    (0,9.3725549039
)(0.1,9.3285550312
)(0.2,9.1985115839
)(0.3,8.9879707942
)(0.4,8.7051855159
)(0.5,8.3598886397
)(0.6,7.9619965855
)(0.7,7.5204841058
)(0.8,7.0425813594
)(0.9,6.5333394278
)(1,5.9955267351
)};

 \\   
\end{axis}

\end{tikzpicture}
\end{document}
2

Just remove the "central" part of your code, the one that stops one axis and starts the second one. Then you can easily add the legend using the \legend command.

In the axis options you'll find some keys for customising your legend.

Output

figure 1

Code

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}

\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
%\usepgfplotslibrary{external}
%\tikzexternalize

\begin{document}
\begin{figure}[h]
    \centering
\begin{tikzpicture}
\begin{axis}[ 
title={ Energy density $\rho$},
xlabel={r/R},
    ylabel={$\frac{\kappa}{C} \rho$},
    xmin=0, xmax=1.2,
    ymin=0, ymax=10,
    xtick={0,0.2,0.4,0.6,0.8,1,1.2},
    ytick={0,1,2,3,4,5,6,7,8,9,10},
    legend style={
        cells={anchor=west},
        draw=none, fill=none, 
        font=\scriptsize,
        legend pos= south west,
    }]

\addplot[
    color=blue]
    coordinates {
    (0,8.71)(0.1,8.61)(0.2,8.33)(0.3,7.90)(0.4,7.35)(0.5,6.73)(0.6,6.08)(0.7,5.42)(0.8,4.78)(0.9,4.18)(1,3.61)
    };

\addplot[
    color=blue, dashed]
    coordinates {
    (0,9.3725549039
)(0.1,9.3285550312
)(0.2,9.1985115839
)(0.3,8.9879707942
)(0.4,8.7051855159
)(0.5,8.3598886397
)(0.6,7.9619965855
)(0.7,7.5204841058
)(0.8,7.0425813594
)(0.9,6.5333394278
)(1,5.9955267351
)};

\legend{Case I: $p=0$ and $n=0$, Case II: $p=1$ and $n=0$}
\end{axis}
\end{tikzpicture}
\caption{My combined plot}\label{fig:combined}
\end{figure}
\end{document}
  • Hi, I need one more info. When I try to copy this code and insert other coordinates for different plot, why does not it compile? I need to put other 6 plots just below this graph, but for some resons, latex only compiles this particular plot, any idea why? – bhjghjh Jun 8 '15 at 10:17
  • @tazkerahaque Uhm that's weird. Give me one plot here in the comments as an example. I mean, the coordinates and so on. – Alenanno Jun 8 '15 at 10:49
  • sorry , I add the code in the form of an answer, can you please check it? thanks – bhjghjh Jun 8 '15 at 11:02
  • @tazkerahaque It works for me. Check the next pages. You can even combine the plots in this code. – Alenanno Jun 8 '15 at 11:05
  • yes that's my problem , I want it in a single page, to be precise, I need two plots per page – bhjghjh Jun 8 '15 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.