Re: Le mie (dis)avventure con TikZ

#96764
Up
0
Down
::

lorenzo.pantieri” post=98928Ciao a tutti.
`\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}[font=\small]
\begin{axis} [width=\textwidth,
axis lines=middle,
xmin=-20,xmax=600,ymin=-5000,ymax=4000,
enlargelimits,
xtick=\empty,
ytick=\empty,
xlabel=$x$,
ylabel=$y$,grid]

\addplot [domain=0:400, samples=200,smooth,very thick,blue] {-0.1*x*x+60*x-5000};
\addplot [domain=400:600, samples=200,smooth,very thick,dashed] {-0.1*x*x+60*x-5000};

\draw (axis cs:400,-10000) — (axis cs:400,10000);
\node [below] at (axis cs:400,0) {$400$};

\node (a1) at (axis cs:550,3500) {Zona di utile};
\node (b1) at (axis cs:200,-4000) {Zona di perdita};

\node (a2) at (axis cs:350,2000) {};
\node (b2) at (axis cs:30,-1500) {};

\draw [->] (a1.south) — (a2.center);
\draw [->] (b1.north) — (b2.center);
\end{axis}
\end{tikzpicture}
\end{document}`
In questa figura vorrei colorare di blu la zona di utile (compresa tra la parabola, l’asse x e la retta verticale y=400) e in rosso la zona di perdita (compresa tra la parabola, l’asse y e l’asse x).

Come faccio?

Grazie anticipate,
Lorenzo

Come faccio?

Per questo tipo di necessità, la libreria [tt]fillbetween[/tt] è quello che serve.

In pratica basta dare dei nomi ai plot (in generale si può applicare ad ogni path) che delimitano la zona da colorare e successivamente inserire il colore con un costrutto: `
\addplot []fill between[of= and ];
` Quindi: `
\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}[font=\small]
\begin{axis} [width=\textwidth,
axis lines=middle,
xmin=-20,xmax=600,ymin=-5000,ymax=4000,
enlargelimits,
xtick=\empty,
ytick=\empty,
xlabel=$x$,
ylabel=$y$,grid]

\addplot [domain=0:400, samples=200,smooth,very thick,blue,name path global=parabola] {-0.1*x*x+60*x-5000};
\addplot [domain=400:600, samples=200,smooth,very thick,dashed] {-0.1*x*x+60*x-5000};

\draw[name path=retta] (axis cs:400,10000) — (axis cs:400,-5000)node[below]{$400$};% opzione 2
\node [below right] at (axis cs:400,0) {$400$};% opzione 1

% riempimento
\addplot [fill=blue!50!cyan!20]fill between[of=retta and parabola];

\node (a1) at (axis cs:550,3500) {Zona di utile};
\node (b1) at (axis cs:200,-4000) {Zona di perdita};

\node (a2) at (axis cs:350,2000) {};
\node (b2) at (axis cs:30,-1500) {};

\draw [->] (a1.south) — (a2.center);
\draw [->] (b1.north) — (b2.center);
\end{axis}
\end{tikzpicture}
\end{document}
` Ho anche proposto un paio di alternative per inserire l’etichetta 400 non sopra l’asintoto.

Ciao
Claudio

Go to top