0

I want my legend only show that, mark with filling black-BHI growth, mark with filling white-BHI ph change. Also, name of Y axis 2 must be right, but in concretely my graph name of Y axis 2 with Y axis 1. Also Lines %f2 begin from Y axis 1

\documentclass{article}
\usepackage{pgfplots,tikz}
\usepackage{caption}
  \DeclareCaptionLabelSeparator{fullstop}{.\quad}
  \captionsetup[figure]{labelsep=fullstop}
\pgfplotsset{width=120mm}
\begin{document}

\begin{figure}
  \begin{tikzpicture}
% f1
\begin{axis}[
color=black,
scale only axis,
xmin=0,
xmax=168,
%xtick={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,48,72,168},
xtick={0,12,24,48,72,168}, %%%<------------- here
xlabel=\ {Time},
ymin=0,
ymax=10,
ytick={0,1,2,3,4,5,6,7,8,9,10},% your code
yticklabels={0,1,2,3,4,5,6,7,8,9,10},% your code
ylabel=\ {log(Cfu/cm$^{2}$)},
y axis line style={black},
ytick style={black},
yticklabel style={black},
axis x line*=bottom,
axis y line*=left, 
tick align = outside,]
\addplot [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark size=2.5pt,
mark=square*,
mark options={solid,fill=white}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark size=2.5pt,
mark=*,
mark options={solid,fill=white}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark size=2.5pt,
mark=diamond*,
mark options={solid,fill=white}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark size=2.5pt,
mark=triangle*,
mark options={solid,fill=white}]
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\\};
\legend {BHI growth, BHI ph change}
\end{axis}
% f2
\begin{axis}[
color=black,
scale only axis,
xmin=0,
xmax=168,
ymin=0,
ymax=6,
ytick={0,1,2,3,4,5,6},
yticklabels={0,1,2,3,4,5,6},
axis x line*=none,
axis y line*=right,
ylabel=\ {Relative Expression}
ylabel style=black,
hide x axis,
tick align =outside,]
\addplot [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark=square*,
mark options={solid,fill=black}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark=*,
mark options={solid,fill=black}]
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 [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark=diamond*,
mark options={solid,fill=black}]
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\\};
\addplot [
color=black,
solid,
line width=0.5pt,
mark size=2.5pt,
mark=triangle*,
mark options={solid,fill=black}]
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\\};
\legend {BHI growth, BHI ph change}
    \end{axis}
  \end{tikzpicture}
  \caption{Growth of different types foodborne pathogens.}
\end{figure}
\end{document}
2

1 Answer 1

1

Under the assumption that two different ordinates in the same plot is probably a bad idea here's a partial solution to your problem.

I've tried to boil down your code to a more minimal example. Adding \pgfplotsset{compat=newest} to the preamble and changing the \begin[axis] options took care of the position of the y-labels.

I took hints from this answer regarding overlapping legends and this answer regarding two (or three) ordinate plots (which is indeed helpful, even though you didn't think so).

To be honest, I don't understand your problem with the legend.

\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}$) (red)},
        axis y line*=left,
        ymin=0, ymax=10,
        legend pos=south west]
    \addplot [red, mark=*]
        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\\};
    \label{plot_one}
    \addlegendentry{plot 1}
    \end{axis}
    % f2
    \begin{axis}[scale only axis,
        hide x axis,
        xmin=0, xmax=168,
        ylabel={Relative Expression (blue)},
        axis y line*=right,
        ymin=0, ymax=6,
        legend pos=south west]
    \addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1}
    \addplot [blue, mark=*]
        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 2}
    \end{axis}
\end{tikzpicture}
\end{document}

Screenshot of result

1
  • 1
    Thanks for accepting the answer. Depending on the outcome you can also wait with accepting until other answers come in, maybe someone has a better answer...
    – Habi
    Jan 5, 2015 at 12:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .