1

I want to give under the graph legends and \caption{Growth of different types foodborne pathogens.}. Delete line papralel x axis

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}


\begin{document}
\begin{tikzpicture}
    % f1
    \begin{axis}[scale only axis,
        xlabel=Time,
        xmin=0, xmax=168,
        ylabel={log(Cfu/cm$^{2}$)},
        axis y line*=left,
        ymin=0, ymax=10,
        legend pos=south west]
    \addplot [black, fill=white, mark=square*]
        table[row sep=crcr]{
            0 9\\
            4 9.1\\
            8 9.8\\
            12 9.8\\
            24 9.162\\
            48 9\\
            72 9.1\\
            168 9.8\\};
             \addplot [black, fill=white, mark=*]
        table[row sep=crcr]{
            0 9.2\\
            4 8.1\\
            8 8.8\\
            12 8.8\\
            24 9.62\\
            48 9.3\\
            72 9.1\\
            168 8.8\\};
            \addplot [black, fill=white, mark=diamond*]
        table[row sep=crcr]{
            0 8.7\\
            4 9.2\\
            8 8.9\\
            12 9.6\\
            24 9.2\\
            48 8\\
            72 8.1\\
            168 8.8\\};
            \addplot [black, fill=white, mark=triangle*]
        table[row sep=crcr]{
           0 7\\
           4 7.1\\
           8 7.8\\
           12 7.8\\
           24 7.162\\
           48 7.6\\
           72 7.1\\
           168 7.8\\};
    \label{plot_one}
    \addlegendentry{plot 1}
    \end{axis}
    % f2
    \begin{axis}[scale only axis,
        hide x axis,
        xmin=0, xmax=168,
        ylabel={Relative Expression},
        axis y line*=right,
        ymin=0, ymax=6,
        legend pos=south west]
    \addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 4} 
    \addplot [black, mark=square*]
        table[row sep=crcr]{
            0 1.3\\
4 1.8\\
8 1.4\\
12 1.3\\
24 1.85\\
48 1.48\\
72 1.63\\
168 1.75\\};
             \addplot [black, mark=*]
        table[row sep=crcr]{
            0 1.6\\
4 3.8\\
8 2.4\\
12 1.3\\
24 2.85\\
48 1.48\\
72 2.63\\
168 3.75\\};
            \addplot [black, mark=diamond*]
        table[row sep=crcr]{
            0 0.3\\
4 0.8\\
8 0.4\\
12 0.3\\
24 0.85\\
48 0.48\\
72 0.63\\
168 0.75\\};
            \addplot [black, mark=triangle*]
        table[row sep=crcr]{
           0 0.8\\
4 1.2\\
8 1.24\\
12 2.3\\
24 2.85\\
48 2.48\\
72 2.63\\
168 2.75\\};
    \addlegendentry{plot 2}
    \end{axis}
\end{tikzpicture}
\end{document}
  • I'm having a lot of trouble understanding your question... can you please try to clarify it? Thank you! – darthbith Jan 5 '15 at 14:45
2

For caption you need to put the diagram in figure environment. To see it in preview, the option floats should be added. For legend outside of diagram pgfplot assume only option outer north east, so for the legend below you need to change legend style.

I not figured out what you like to put in legend, so I left legend for f1 as you provided in MWE in redesign it for f2 as I supposed the legend should be. My changes in your MWE are denoted with % <----

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage{caption} % <----
    \captionsetup{font = small} % <----

\usepackage[active,floats,tightpage]{preview}% <---
    \setlength\PreviewBorder{1em}

    \begin{document}
%---------------------------------------------------------------%
\begin{figure} % <---
    \centering % <---
\begin{tikzpicture}
%%%% f1
    \begin{axis}[scale only axis,
        xlabel=Time,
        xmin=0, xmax=168,
        ylabel={log(Cfu/cm$^{2}$)},
        axis y line*=left,
        ymin=0, ymax=10,
        legend style={at={(0.48,-0.15)},  % <---
                      anchor=north east}] % <---
    \addplot [black, fill=white, mark=square*]
        table[row sep=crcr]{
            0 9\\
            4 9.1\\
            8 9.8\\
            12 9.8\\
            24 9.162\\
            48 9\\
            72 9.1\\
            168 9.8\\};
             \addplot [black, fill=white, mark=*]
        table[row sep=crcr]{
            0 9.2\\
            4 8.1\\
            8 8.8\\
            12 8.8\\
            24 9.62\\
            48 9.3\\
            72 9.1\\
            168 8.8\\};
            \addplot [black, fill=white, mark=diamond*]
        table[row sep=crcr]{
            0 8.7\\
            4 9.2\\
            8 8.9\\
            12 9.6\\
            24 9.2\\
            48 8\\
            72 8.1\\
            168 8.8\\};
            \addplot [black, fill=white, mark=triangle*]
        table[row sep=crcr]{
           0 7\\
           4 7.1\\
           8 7.8\\
           12 7.8\\
           24 7.162\\
           48 7.6\\
           72 7.1\\
           168 7.8\\};
    \label{plot_one}
    \addlegendentry{plot 1}
    \end{axis}
%%%% f2
    \begin{axis}[scale only axis,
        hide x axis,
        xmin=0, xmax=168,
        ylabel={Relative Expression},
        axis y line*=right,
        ymin=0, ymax=6,
        legend columns=2,                % <---
        legend style={at={(0.52,-0.15)}, % <---
                      anchor=north west}]% <---
]
%    \addlegendimage{/pgfplots/refstyle=plot_one}
    \addplot [black, mark=square*]
        table[row sep=crcr]{
            0 1.3\\
4 1.8\\
8 1.4\\
12 1.3\\
24 1.85\\
48 1.48\\
72 1.63\\
168 1.75\\};
    \addlegendentry{plot 1} % <---
             \addplot [black, mark=*]
        table[row sep=crcr]{
            0 1.6\\
4 3.8\\
8 2.4\\
12 1.3\\
24 2.85\\
48 1.48\\
72 2.63\\
168 3.75\\};
    \addlegendentry{plot 2} % <---
            \addplot [black, mark=diamond*]
        table[row sep=crcr]{
            0 0.3\\
4 0.8\\
8 0.4\\
12 0.3\\
24 0.85\\
48 0.48\\
72 0.63\\
168 0.75\\};
    \addlegendentry{plot 3} % <---
            \addplot [black, mark=triangle*]
        table[row sep=crcr]{
0 0.8\\
4 1.2\\
8 1.24\\
12 2.3\\
24 2.85\\
48 2.48\\
72 2.63\\
168 2.75\\};
    \label{plot_four}
    \addlegendentry{plot 4} % <---
    \end{axis}
\end{tikzpicture}

    \caption{Growth of different types foodborne pathogens.} % <---
    \end{figure} % <---
\end{document}

enter image description here

  • Thak you very much Zarko, also how can I to delete line above/up to major X axis. I dont want line which conect number 10 and 6, two Y axis – user67733 Jan 6 '15 at 13:23

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.