2

I tried to get symbolic values x1, x2, x3, x4 on x axis and C(x1),...,C(x4) on y axis.

\begin{tikzpicture}
    \begin{axis}[
        xlabel={$x$},
        ylabel={$y$},
        axis lines=center,
        symbolic x coords={$x_1$, $x_2$, $x_3$, $x_4$},
        symbolic y coords={$C(x_1)$, $C(x_2)$, $C(x_3)$,                
        $C(x_4)$}]
    \end{axis}
\end{tikzpicture}

but the result is:

enter image description here

How can I get the right plot?

Thank you very much.

  • 1
    you need to enter as many entries as the ticks. Here you have 5 ticks but 3 labels. – percusse Nov 5 '17 at 17:02
  • Hello @percusse, why have I 3 labels? It seems that there are x and y labels. – Gennaro Arguzzi Nov 5 '17 at 17:06
  • Count the tick marks on your axes. There are 5 each. But your labels are 3 and 4. So pgfplots is reusing some of them. – percusse Nov 5 '17 at 17:10
  • Sorry @percusse, where are 3 and 4? I see only the 5 ticks. Can you explain me please? I'm a beginner of Latex. – Gennaro Arguzzi Nov 5 '17 at 17:13
  • How many arguments did you supply to symbolic x coords? – percusse Nov 5 '17 at 17:23
2

The following MWE

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel={$x$},
        ylabel={$y$},
        axis lines=center,
        %symbolic x coords={$x_1$,$x_2$,$x_3$, $x_4$},
        ]
    \end{axis}
\end{tikzpicture}
\end{document}

results in

Package pgfplots Warning: You have an axis with empty range (in direction y). Replacing it with a default range and clearing all plots. on input line 12.

and

enter image description here

symbolic x coords={$x_1$, $x_2$,$x_3$,$x_4$} means that $x_1$ is used as the normalized value 0 and $x_2$ is used as the normalized value 1. So adding this option will replace the tick values 0.2 and 0.4 by $x_1$ and 0.6, 0.8 and 1 by $x_2$:

enter image description here


If the desired result is

enter image description here

you could set the minimum and maximum for x and y, the maximum for the ticks and the tick distances and add 0 to the symbolic coords:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel={$x$},
        ylabel={$y$},
        axis lines=center,
        symbolic x coords={0,$x_1$,$x_2$,$x_3$,$x_4$},
        xmin={[normalized]0},
        xmax={[normalized]5},
        xtickmax={[normalized]4},
        xtick distance=1,
        symbolic y coords={0,$C(x_1)$,$C(x_2)$,$C(x_3)$,$C(x_4)$},
        ymin={[normalized]0},
        ymax={[normalized]5},
        ytickmax={[normalized]4},
        ytick distance=1
        ]
    \end{axis}
\end{tikzpicture}
\end{document}

But here is also a suggestion without symbolic coords:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel={$x$},
        ylabel={$y$},
        axis lines=center,
        xmin=0,xmax=5,
        xtick={1,...,4},
        xticklabel={$x_{\pgfmathprintnumber[precision=0]{\tick}}$},
        ymin=0,ymax=5,
        ytick={1,...,4},
        yticklabel={$C(x_{\pgfmathprintnumber[precision=0]{\tick}})$},
        ]
    \end{axis}
\end{tikzpicture}
\end{document}
  • Hello @esdd, thank you for your solution. It is very clear. – Gennaro Arguzzi Nov 5 '17 at 20:20

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.