这是呈现散点图的代码。我只想要进行两种典型的修改。首先,我想要在轴上添加标签。在 axis 环境中,我有align=center,
、ylabel = Number of Unsold \\ Candy Bars,
和xlabel = Number of Days,
。为什么标签没有打印出来?其次,我想让表示数据的点稍微小一点,以便可以看到两个刻度标记。(x 轴上 10 处的刻度标记和 y 轴上 120 处的刻度标记似乎被散点图的点遮住了。)
\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}
答案1
轴标签ylabel = Number of Unsold \\ Candy Bars
和xlabel = Number of Days
不会显示,因为您用ylabel = $y$
和覆盖了它们xlabel = $x$
。每个轴只能有一个标签。
要获得第二个标签,可以使用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}
或\node
:
\node at (3.5,-.75) {Number of Days};
\node[rotate=90,align=center] at (-1.25,3) {Number of Unsold \\ Candy Bars};
绘图标记的大小可通过选项mark size
来设置\addplot
。
代码:
\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
:
\node
: