我的问题:绘制的线并不正好位于轴上......
这是我的输出:
以下是生成它的代码:
\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ axis on top=true,
axis x line=middle,
axis y line=middle,
inner axis line style={=>},
width=6cm,height=6cm,
ymin=0,ymax=1.2,
xmin=0,xmax=1.2,
axis line style = thick,
xtick={0,1},
ytick={0,1},
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(current axis.north west)},above=0.5mm},
xlabel={$p$},
ylabel={$q$}]
\draw[line width=4, red] (0,0)--(100,0);
\end{axis}
\end{tikzpicture}
\end{document}
答案1
图表边框会将图表全部剪掉,因此您只能看到一半的线。要解决这个问题,您有两种可能:
- 在图外画这条线(见另一个答案)
- 使用图表
\clip=false
中的选项axis
由于这条线比图宽长得多,因此您还需要相应地定义其长度。一种解决方案\addplot
是,这条线的宽度如下图所示:
\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} % <---
\begin{document}
\begin{tikzpicture}
\begin{axis}[ axis on top=true,
axis lines=middle, % <---
%inner axis line style={=>}, % not used
width=6cm,height=6cm,
ymin=0,ymax=1.2,
xmin=0,xmax=1.2,
%axis line style = thick,
xtick={0,1},
ytick={0,1},
every axis x label/.style={at={(current axis.right of origin)}, anchor=west},
every axis y label/.style={at={(current axis.north west)},above=0.5mm},
xlabel={$p$},
ylabel={$q$},
clip=false % <---
]
\addplot +[line width=4pt, no marks, red] coordinates {(0,0) (1,0)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest} %added
\begin{document}
\begin{tikzpicture}
\begin{axis}[ axis on top=true,
axis x line=middle,
axis y line=middle,
inner axis line style={=>},
width=6cm,height=6cm,
ymin=-0.05,ymax=1.2, %made ymin negative, to include red line
xmin=0,xmax=1.2,
axis line style = thick,
xtick={0,1},
ytick={0,1},
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(current axis.north west)},above=0.5mm},
xlabel={$p$},
ylabel={$q$}]
\draw[line width=4, red] (0,0)--(1,0); %changed form 100 to 1
\end{axis}
\end{tikzpicture}
\end{document}
答案3
只需将您的行代码移到axis
其前面和外面即可:
\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\draw[line width=4pt, red] (0,0)--(100,0);
\begin{axis}[ axis on top=true,
axis x line=middle,
axis y line=middle,
inner axis line style={=>},
width=6cm,height=6cm,
ymin=0,ymax=1.2,
xmin=0,xmax=1.2,
axis line style = thick,
xtick={0,1},
ytick={0,1},
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(current axis.north west)},above=0.5mm},
xlabel={$p$},
ylabel={$q$}]
%\draw[line width=4pt, red] (0,0)--(100,0);
\end{axis}
\end{tikzpicture}
\end{document}