我正在建立一個程式碼來顯示下面兩個函數之間的區域。這很好,但還可以變得更好。
我的問題是:有沒有辦法將 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
為blue
和CCvermelho
to ):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}