如何改变网格线的颜色以及如何改变刻度线的位置?

如何改变网格线的颜色以及如何改变刻度线的位置?

我正在编写一个代码来显示下面两个函数之间的区域。这很好,但可以做得更好。

图像

我的问题是:有没有办法将 x 刻度标签(x 轴上的数字)稍微向左移动,以便辅助线不会与其相交?对 y 刻度标签(稍微低一点)也做同样的事情?我如何更改辅助线的颜色以及辅助线网格矩阵的 x 和 y 间隔?

\documentclass{article}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
% Eixos
\begin{axis}[
grid,
axis x line=center,
axis y line=center,
xtick={-1,0,1},
ytick={-1,0,1},
xlabel={$x$},
ylabel={$y$},
xlabel style={below right},
ylabel style={above left},
xmin=-0.5,
xmax=1.1,
ymin=-0.5,
ymax=1.1]

% Função de cima contínua
\addplot[name path=f,domain=0:1,CCazul] {x};
% Função de baixo cintínua
\addplot[name path=g,domain=0:1,CCvermelho] {x^2};

% Função de cima pontilhada
\addplot[dashed, name path=fpont1,domain=-.5:0,CCazul] {x};
\addplot[dashed, name path=fpont2,domain=1:1.1,CCazul] {x};
% Função de baixo pontilhada
\addplot[dashed, name path=gpont1,domain=-.5:0,CCvermelho] {x^2};    
\addplot[dashed, name path=gpont2,domain=1:1.1,CCvermelho] {x^2};   

% Path
\path[name path=axis] (axis cs:0,0) -- (axis cs:1,0);

%Fill between
\addplot [
thick,
color=black,
fill=black, 
fill opacity=0.05
]
fill between[
of=f and g,
soft clip={domain=0:1},
];

% Labels dos nós
\node [color=CCazul] at (axis cs:  .55,  .8) {$f(x) = x$};
\node  [color=CCvermelho] at (axis cs:  0.9,  .4) {$g(x) = x^2$};
\end{axis}
\end{tikzpicture}

\end{document}

谢谢。

答案1

您可以指定xticklabel style={below left}偏移 x 刻度标签,y 轴也是如此。

要更改网格线的外观,请指定grid style={...}

要更改网格线之间的间隔,您可以指定minor tick num,或者如果两者的间隔不一样,minor x tick num则指定和。minor y tick num

示例(由于您忽略了这些颜色的定义,因此我将CCazul其改为blueCCvermelho改为):red

\documentclass[tikz]{standalone}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both,
grid style={red!15},
axis x line=center,
axis y line=center,
xtick={-1,0,1},
ytick={-1,0,1},
minor tick num=4,
xticklabel style={below left},
yticklabel style={below left},
xlabel={$x$},
ylabel={$y$},
xlabel style={below right},
ylabel style={above left},
xmin=-0.5,
xmax=1.1,
ymin=-0.5,
ymax=1.1]

\addplot[name path=f,domain=0:1,blue] {x};

\addplot[name path=g,domain=0:1,red] {x^2};

\addplot[dashed, name path=fpont1,domain=-.5:0,blue] {x};
\addplot[dashed, name path=fpont2,domain=1:1.1,blue] {x};

\addplot[dashed, name path=gpont1,domain=-.5:0,red] {x^2};    
\addplot[dashed, name path=gpont2,domain=1:1.1,red] {x^2};   

% Path
\path[name path=axis] (axis cs:0,0) -- (axis cs:1,0);

%Fill between
\addplot [
thick,
color=black,
fill=black, 
fill opacity=0.05
]
fill between[
of=f and g,
soft clip={domain=0:1},
];

\node[blue] at (axis cs:  .55,  .8) {$f(x) = x$};
\node[red] at (axis cs:  0.9,  .4) {$g(x) = x^2$};
\end{axis}
\end{tikzpicture}
\end{document}

输出

相关内容