0

Here is code that renders a scatter plot. There are only two typical modifications that I want. First, I want labels on the axes. In the axis environment, I have align=center,, ylabel = Number of Unsold \\ Candy Bars,, and xlabel = Number of Days,. Why are the labels not being printed? Second, I want to make the dots representing the data to be a bit smaller so that the two tick marks can be visible. (The tick marks at 10 on the x-axis and 120 on the y-axis seem to be obscured by the dots for the scatter plot.)

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}

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

\begin{document}


\begin{tikzpicture}

\begin{axis}[grid=none, align=center, title=\textbf{George's Unsold Candy Bars},
    ylabel = Number of Unsold \\ Candy Bars,
    xlabel = Number of Days,
    xmin=0,xmax=11,ymin=0,ymax=130,
    xtick={10},ytick={120},
    yticklabel style={anchor=east},
    axis on top,axis lines=middle,
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south east}
    ]

\addplot[only marks] coordinates{(0,120) (1,108) (2,96) (3,84) (4,72) (5,60) (6,48) (7,36) (8,24) (9,12) (10,0)};

\end{axis}

\coordinate (O) at (0,0);
\node (origin) at ($(O)+(225:10pt)$) {$0$};

\end{tikzpicture}

\end{document}
  • xlabel={abc} is the right way, i.e. use braces. – henry Jun 9 '15 at 15:27
  • @henry See the code at "Modifying some features in a scatter plot (Version 2)." Alenanno does not use braces and the labels are printed along the axes. – user74973 Jun 9 '15 at 17:46
  • 1
    you have your xlabel and ylabel defined twice. Get rid of the second definition xlabel=$x$,ylabel=$y$, – Maarten Dhondt Jun 9 '15 at 17:59
  • @Maarten Dhondt Thanks. To be consistent with other graphs in a file, I do want the x and y typeset in these exact locations with respect to the arrowheads. How would I do that using a \node command or a \coordinate command? – user74973 Jun 9 '15 at 18:04
  • 1
    You could probably get this done with an extra x ticks and extra y ticks hack or indeed with \nodes. But I'm pretty sure an axis shouldn't represent two different units ($x$ and Number of Days) at the same time. – Maarten Dhondt Jun 9 '15 at 18:17
1

The axis labels ylabel = Number of Unsold \\ Candy Bars and xlabel = Number of Days aren't displayed because you overwrite them with ylabel = $y$ and xlabel = $x$. You can only have one label per axis.

To get a second label one could use extra ticks:

extra x ticks       = {5.5},
extra x tick style  = {xtick style={draw=none},xticklabel style={yshift=-5mm}},
extra x tick labels = {Number of Days},
extra y ticks       = {65},
extra y tick style  = {ytick style={draw=none},yticklabel style={xshift=-5mm,rotate=90}},
extra y tick labels = {Number of Unsold \\ Candy Bars}

or \nodes:

\node at (3.5,-.75) {Number of Days};
\node[rotate=90,align=center] at (-1.25,3) {Number of Unsold \\ Candy Bars};

The size of the plot marks is set with mark size as a \addplot option.

Code:

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}

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

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[   grid                = none, 
                            title               = \textbf{George's Unsold Candy Bars},
                            align               = center,
                            xmin                = 0,
                            xmax                = 11,
                            ymin                = 0,
                            ymax                = 130,
                            xtick               = {10},
                            ytick               = {120},
                            axis lines          = middle,
                            xlabel              = $x$,
                            ylabel              = $y$,
                            xlabel style        = {at={(ticklabel* cs:1)},anchor=north west},
                            ylabel style        = {at={(ticklabel* cs:1)},anchor=south east},
                            extra x ticks       = {5.5},
                            extra x tick style  = {xtick style={draw=none},xticklabel style={yshift=-5mm}},
                            extra x tick labels = {Number of Days},
                            extra y ticks       = {65},
                            extra y tick style  = {ytick style={draw=none},yticklabel style={xshift=-5mm,rotate=90}},
                            extra y tick labels = {Number of Unsold \\ Candy Bars}
                        ]

            \addplot[mark size=0.5,only marks] coordinates{(0,120) (1,108) (2,96) (3,84) (4,72) (5,60) (6,48) (7,36) (8,24) (9,12) (10,0)};
        \end{axis}

        \node at (225:10pt) {$0$};
    \end{tikzpicture}

    \vspace*{2cm}

    \begin{tikzpicture}
        \begin{axis}[   grid         = none, 
                            title        = \textbf{George's Unsold Candy Bars},
                            align        = center,
                            xmin         = 0,
                            xmax         = 11,
                            ymin         = 0,
                            ymax         = 130,
                            xtick        = {10},
                            ytick        = {120},
                            axis lines   = middle,
                            xlabel       = $x$,
                            ylabel       = $y$,
                            xlabel style = {at={(ticklabel* cs:1)},anchor=north west},
                            ylabel style = {at={(ticklabel* cs:1)},anchor=south east}
                        ]

            \addplot[mark size=0.5,only marks] coordinates{(0,120) (1,108) (2,96) (3,84) (4,72) (5,60) (6,48) (7,36) (8,24) (9,12) (10,0)};
        \end{axis}

        \node at (225:10pt) {$0$};
        \node at (3.5,-.75) {Number of Days};
        \node[rotate=90,align=center] at (-1.25,3) {Number of Unsold \\ Candy Bars};
    \end{tikzpicture}

\end{document}

extra ticks:

enter image description here

\node:

enter image description here

  • Very nice! If I have several graphs in a file with labels for the axes besides x and y, I want there to be consistency with them. In particular, I want the distances between labels and the axes to be consistent. (Maybe I should say "consistent algorithm" to determine such distances. The distances would be different for a graph in which there were no numbers along the y-axis than for a graph with 5-digit numbers along the y-axis.) I wouldn't get that with your code. So, I would like to have xlabel = {Number of Days} and ylabel = {Number of Unsold \\ Candy Bars}. – user74973 Jun 9 '15 at 22:45
  • In my post, I position the x and y with xlabel style={at={(ticklabel* cs:1)},anchor=north west}, and with ylabel style={at={(ticklabel* cs:1)},anchor=south west}. I do not know what the manual is saying about the * version of the ticklabel version in at={(ticklabel* cs:1)}. How would I position the x and y in the same locations using a node command? – user74973 Jun 9 '15 at 22:45

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.