我怎样才能将刻度线的颜色从灰色改为黑色,就像轴颜色一样?
\documentclass[a4paper,11pt,fleqn]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{pgf,tikz,pgfplots}
\begin{document}
\begin{tikzpicture}[every pin/.style = {pin edge={Latex-,thin,black}}]
\begin{axis}[scale only axis,
axis x line=middle,
axis y line=middle,
grid = major,
inner axis line style={=>},
width=10cm,height=5cm,
ymin=0,ymax=1.1,
xmin=-5,xmax=5,
axis line style = thick,
xtick={-4,-3,-2,-1,1,2,3,4},
major tick style = thick,
ytick={0.2,0.4,0.6,0.8,1},
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(current axis.north)},above=0.5mm},
xlabel={$x$},
ylabel={$f(x)$}]
\addplot[red,only marks] coordinates {(-4,0.2)(-2,0.5)(-1,0.8)(2,0.9)(3,1)};
\addplot[red,very thick,domain=-5:-4] {0} ;
\addplot[red,very thick,domain=-4:-2] {0.2} ;
\addplot[red,very thick,domain=-2:-1] {0.5} ;
\addplot[red,very thick,domain=-1:2] {0.8} ;
\addplot[red,very thick,domain=2:3] {0.9} ;
\addplot[red,very thick,domain=3:5] {1} ;
\end{axis}
\end{tikzpicture}
\end{document}
答案1
可以使用以下方法全局更改刻度样式pgfplotsset
:
\pgfplotsset{tick style={black}}
有关更多勾选选项,请参阅文档第 4.15 节。
答案2
干得好!
刻度线的颜色很简单,只需major tick style
相应调整即可。我使用了major tick style = {thick, black}
。
要使图上方的刻度标签(您忘了说在您的问题中),但保持其下方的轴网格比较棘手。
我发现了类似的东西这里pgfplots
。您必须配置绘制每个图层的顺序。
要做到这一点你可以使用这个:
\pgfplotsset{
standard/.style={set layers=tick labels on top},
layers/tick labels on top/.define layer set=
{axis background,
axis grid,
axis lines,
main,
axis tick labels,
axis ticks,
axis descriptions,
axis foreground}
{/pgfplots/layers/standard}
}
图层在 中的显示顺序define layer set
就是 pfgplots 绘制它们的顺序。请注意,首先是axis grid
,然后是实际绘图 ( main
),然后是axis tick labels
和axis ticks
。
结果是图表上方的黑色刻度标记和刻度标签:
\documentclass[a4paper,11pt,fleqn]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{
standard/.style={set layers=tick labels on top},
layers/tick labels on top/.define layer set=
{axis background,
axis grid,
axis lines,
main,
axis tick labels,
axis ticks,
axis descriptions,
axis foreground}
{/pgfplots/layers/standard}
}
\begin{document}
\begin{tikzpicture}[every pin/.style = {pin edge={Latex-,thin,black}},small dot/.style={fill=black,circle,scale=0.3}]
\begin{axis}[standard,scale only axis,
axis x line=middle,
axis y line=middle,
grid = major,
inner axis line style={=>},
width=10cm,height=5cm,
ymin=0,ymax=1.1,
xmin=-5,xmax=5,
axis line style = thick,
xtick={-4,-3,-2,-1,1,2,3,4},
major tick style = {thick, black},
ytick={0.2,0.4,0.6,0.8,1},
% axis on top,% <- HERE
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(current axis.north)},above=0.5mm},
xlabel={$x$},
ylabel={$f(x)$},
% axis on top <- AND HERE
]
\addplot[red,only marks] coordinates {(-4,0.2)(-2,0.5)(-1,0.8)(2,0.9)(3,1)};
\addplot[red,very thick,domain=-5:-4] {0} ;
\addplot[red,very thick,domain=-4:-2] {0.2} ;
\addplot[red,very thick,domain=-2:-1] {0.5} ;
\addplot[red,very thick,domain=-1:2] {0.8} ;
\addplot[red,very thick,domain=2:3] {0.9} ;
\addplot[red,very thick,domain=3:5] {1} ;
\end{axis}
\end{tikzpicture}
\end{document}