使用axis lines=middle
后面的括号\begin{axis}
,我可以让箭头出现在图形的 x 轴和 y 轴上,方向为正,方向为负。如何添加负方向的箭头?
平均能量损失
\documentclass[12pt]{article}
\usepackage{amsmath, amssymb}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{positioning,calc}
\usepackage{pgfplots}
\pgfplotsset{vasymptote/.style={before end axis/.append code={\draw[dashed,<->,>=latex] ({rel axis cs:0,0} -| {axis cs:#1,0}) -- ({rel axis cs:0,1} -| {axis cs:#1,0}); }}}
\begin{tikzpicture} %cscx
\begin{axis}[grid=both,
axis lines=middle,
ticklabel style={fill=white},
x=.7cm,y=.7cm,clip=false,
xmin=-3.5,xmax=4.5,xtick={-2,2,4},
ymin=-4.5,ymax=4.5,ytick={-4,-2,2,4},
minor tick={-3,-1,1,3},
vasymptote={-pi+0.5},vasymptote={0.5},vasymptote={pi+0.5},
xlabel=\(x\),ylabel=\(y\),
xlabel style={at={(ticklabel* cs:1)},anchor=west},
ylabel style={at={(ticklabel* cs:1)},anchor=south},
samples=200]
\addplot[domain=-2.39:0.25,<->,>=latex] {cosec(deg(x-0.5))};
\addplot[domain=0.75:3.39,<->,>=latex] {cosec(deg(x-0.5))};
\node[label={270:{\(y=\csc x\)}},inner sep=2pt] at (axis cs:0.5,-4.5) {};
\end{axis}
\end{tikzpicture}
答案1
\pgfplotsset
例如,您可以在调用中添加新样式myaxis
,在那里定义箭头样式,然后将样式的名称添加到轴选项。
另外,我还添加了arrows.meta
Tikz 中箭头提示的新库。
输出
代码
\documentclass[12pt]{article}
\usepackage{amsmath, amssymb}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{arrows.meta,positioning,calc}
\usepackage{pgfplots}
\pgfplotsset{
vasymptote/.style={before end axis/.append code={\draw[dashed,<->,-{Latex}] ({rel axis cs:0,0} -| {axis cs:#1,0}) -- ({rel axis cs:0,1} -| {axis cs:#1,0}); }},
myaxis/.style={axis line style={<->, {Latex}-{Latex}}}
}
\begin{document}
\begin{tikzpicture} %cscx
\begin{axis}[grid=both,
axis lines=middle,
myaxis,
ticklabel style={fill=white},
x=.7cm,y=.7cm,clip=false,
xmin=-3.5,xmax=4.5,xtick={-2,2,4},
ymin=-4.5,ymax=4.5,ytick={-4,-2,2,4},
minor tick={-3,-1,1,3},
vasymptote={-pi+0.5},vasymptote={0.5},vasymptote={pi+0.5},
xlabel=\(x\),ylabel=\(y\),
xlabel style={at={(ticklabel* cs:1)},anchor=west},
ylabel style={at={(ticklabel* cs:1)},anchor=south},
samples=200]
\addplot[domain=-2.39:0.25,<->,>=latex] {cosec(deg(x-0.5))};
\addplot[domain=0.75:3.39,<->,>=latex] {cosec(deg(x-0.5))};
\node[label={270:{\(y=\csc x\)}},inner sep=2pt] at (axis cs:0.5,-4.5) {};
\end{axis}
\end{tikzpicture}
\end{document}