1

I have a bar chart on one y axis and a line on the second y axis which both belong to the same x axis. However, the x axis shows two entries for some values (23, 42, 200). How to remove the second one in order to have only one?

\documentclass{report}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        symbolic x coords={11, 23, 42, 200},
        xlabel={Datapoint},
        major x tick style = transparent,
        ymin=15, ymax=32,
        axis y line*=left,
        bar width=20pt,
        ylabel={Average Return \%},
        nodes near coords,
    ]
        \addplot[ybar, fill={rgb:black,1;white,2}] coordinates {
            (11,  28)
            (23,  20)
            (42,  24)
            (200, 29)
        };
    \end{axis}
    \pgfplotsset{every axis y label/.append style={rotate=180,yshift=9.5cm}}
    \begin{axis}[
        symbolic x coords={11, 23, 42, 200},
        xlabel={Datapoint},
        major x tick style = transparent,
        ymin=15, ymax=32,
        axis y line*=right,
        ylabel={Standard Deviation \%},
        nodes near coords,
        legend style={at={(0.2,-0.2)}, anchor=north west},
    ]
        \addlegendimage{/pgfplots/refstyle=Hplot}
        \addlegendentry{$Average\ Return$}
        \addplot[smooth, dashed, mark=*, grey] coordinates {
            (11,  25)
            (23,  28)
            (42,  19)
            (200, 21)
        };
        \addlegendentry{$Standard\ Deviation$}
   \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Stefan Pinnow
  • 29,535
  • 1
    Welcome to TeX.SX. Is it required for you to have two y axes? – Stefan Pinnow Aug 12 '16 at 16:30
  • Related question (not duplicate, though). – auden Aug 12 '16 at 16:34
  • 1
    There are a few unrelated errors (i.e., it's gray, not grey, and one or two others like that). – auden Aug 12 '16 at 16:38
  • Also, for me, your "standard deviation" heading seems to have jumped off the page (also unrelated). I don't think you want the asterisk in the line axis y line*=right. – auden Aug 12 '16 at 16:40
  • I would like to have two y axes as I want to show the standard deviation on the right hand side. This because more charts are created afterwards with different values. – Stambuoch Aug 12 '16 at 17:08
  • Otherwise, I could write Percentage on the left y axis and add both plots to one y axis. But as far as my knowledge goes to amend the code, the x axis still shows the same. – Stambuoch Aug 12 '16 at 17:18

1 Answers1

1

Found a way to solve the problem. Typed xtick={11,23,42,200} for both after the symbolic command.

  • I'm glad that helped you, but that isn't working for me. Good luck with your graph! – auden Aug 12 '16 at 18:32
  • Your answer is merely comment than answer. If you like to make it answer, than provide complete MWE which solve your problem. – Zarko Aug 12 '16 at 18:38
  • 2
    A better way would be to put xtick=data in the options of the first axis, and xtick=\empty in the options of the second. That way, you don't have to hard code the values, and the labels don't get printed twice directly on top of each other. – Jake Aug 12 '16 at 19:30