0

I don't know why the data on x-axis over leaping. I don't have any idea.

\documentclass[varwidth=true, border=2pt]{standalone}  \usepackage{pgfplots}\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        symbolic x coords={Coke-Classic,Diet Coke, Dr.Peppre,Pepsi Cola,Sprite,},
        xtick=data
      ]
        \addplot[ybar,fill=blue] coordinates {
            (Coke-Classic,   38)
            (Diet Coke,  16)
            (Dr.Peppre,   10)
            (Pepsi Cola, 26)
            (Sprite, 10)
        };
    \end{axis}
\end{tikzpicture}   \end{document}
Opal
  • 81,889
  • 28
  • 189
  • 210

1 Answers1

1

There are two possible solutions, that came to my mind. Both are relatively easy (only one line has to be added):

Solution 1

To avoid overlapping x-labels, you can explicitly define the size of one x-unit:

\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        symbolic x coords={Coke-Classic,Diet Coke, Dr.Peppre,Pepsi Cola,Sprite,},
        xtick=data,
        x=2cm
      ]
        \addplot[ybar,fill=blue] coordinates {
            (Coke-Classic,   38)
            (Diet Coke,  16)
            (Dr.Peppre,   10)
            (Pepsi Cola, 26)
            (Sprite, 10)
        };
    \end{axis}
\end{tikzpicture}
\end{document}

The only thing I changed is adding x=2cm to the axis-options.

The result looks as follows:

ybar plot with x=2cm

Solution 2

The solution above increases the width of the plot. If you don't want this, you can instead rotate the tick labels:

\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        symbolic x coords={Coke-Classic,Diet Coke, Dr.Peppre,Pepsi Cola,Sprite,},
        xtick=data,
        xticklabel style={rotate=45}
      ]
        \addplot[ybar,fill=blue] coordinates {
            (Coke-Classic,   38)
            (Diet Coke,  16)
            (Dr.Peppre,   10)
            (Pepsi Cola, 26)
            (Sprite, 10)
        };
    \end{axis}
\end{tikzpicture}
\end{document}

Here I just replaced the x=2cm with xticklabel style={rotate=45} (45 is the rotation angle, you can use any other angle, just use what you think looks best). ybar plot with rotated xtick labels

Community
  • 1
  • 1
luator
  • 4,769
  • 3
  • 30
  • 51