The code adds some completely useless invisible (or rather white) stuff. The lines
\clip(0pt,403pt) -- (389.957pt,403pt) -- (389.957pt,99.6166pt) -- (0pt,99.6166pt) -- (0pt,403pt);
\color[rgb]{1,1,1}
\fill(3.76406pt,399.236pt) -- (380.923pt,399.236pt) -- (380.923pt,253.19pt) -- (3.76406pt,253.19pt) -- (3.76406pt,399.236pt);
\fill(53.4497pt,394.719pt) -- (374.901pt,394.719pt) -- (374.901pt,289.325pt) -- (53.4497pt,289.325pt) -- (53.4497pt,394.719pt);
draw a white background that is larger than the actual picture. TikZ sees that and thinks it is part of the picture. Simply removing/uncommenting these lines removes most of the whitespace.
Near the end of the first scope,
\color[rgb]{1,1,1}
\fill(3.76406pt,249.426pt) -- (386.193pt,249.426pt) -- (386.193pt,103.381pt) -- (3.76406pt,103.381pt) -- (3.76406pt,249.426pt);
does the same.
Additionally (near the end of the second scope
),
\pgftext[center, base, at={\pgfpoint{220.95pt}{106.392pt}}]{\sffamily\fontsize{9}{0}\selectfont{\textbf{ }}}
adds a blank node below the picture, again enlarging the bounding box.
Removing all those lines gives a tight bounding box.
As far as I know, TikZ cannot do the cropping for you, as it can't know whether the white stuff is intentional or not (there might for example be a dark background behind the image so that white is visible).
The code provided produces this output in my system:
![enter image description here](https://i.stack.imgur.com/8e48Q.png)
which shows some problem with negative values for the x-coordinate. I decided then to work only for positive values of the x-coordinate and then use a reflection. For the desired patterns, you can use the patterns
library:
\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows,patterns}
\pagestyle{empty}
\definecolor{ttqqcc}{rgb}{0.2,0,0.8}
\definecolor{ffqqtt}{rgb}{1,0,0.2}
\definecolor{ttqqff}{rgb}{0.2,0,1}
\definecolor{uququq}{rgb}{0.25,0.25,0.25}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->] (-3.63,0) -- (3.67,0);
\foreach \x in {-3,-2,-1,1,2,3}
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt)
node[below] {\footnotesize $\x$};
\draw[color=black] (3.48,0.07) node [anchor=south west] { $x$};
\draw[->] (0,-1.93) -- (0,5.57);
\foreach \y in {-1,1,2,3,4,5}
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt)
node[left] {\footnotesize $\y$};
\draw[color=black] (0.06,5.19) node [anchor=west] { $y$};
\draw (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-3.63,-1.93) rectangle (3.67,5.57);
\draw[pattern color=ffqqtt,pattern=north east lines,fill opacity=0.1, smooth,samples=50,domain=0:1.0]
plot(\x,{\x^4-4*\x^2+3}) -- (1,0) -- (0,0) -- cycle;
\draw[pattern color=ttqqcc,pattern=crosshatch,fill opacity=0.1, smooth,samples=50,domain=1.0:1.7320508075688772]
plot(\x,{\x^4-4*\x^2+3}) -- (1.73,0) -- (1,0) -- cycle;
\begin{scope}[xscale=-1]
\draw[pattern color=ffqqtt,pattern=north east lines,fill opacity=0.1, smooth,samples=50,domain=0:1.0]
plot(\x,{\x^4-4*\x^2+3}) -- (1,0) -- (0,0) -- cycle;
\draw[pattern color=ttqqcc,pattern=crosshatch,fill opacity=0.1, smooth,samples=50,domain=1.0:1.7320508075688772]
plot(\x,{\x^4-4*\x^2+3}) -- (1.73,0) -- (1,0) -- cycle;
\end{scope}
\draw[smooth,samples=100,domain=-3.6342419080068153:3.665758091993186] plot(\x,{(\x)^4-4*(\x)^2+3});
\fill [color=uququq] (-1.73,0) circle (1.5pt);
\fill [color=uququq] (-1,0) circle (1.5pt);
\fill [color=uququq] (1,0) circle (1.5pt);
\fill [color=uququq] (1.73,0) circle (1.5pt);
\end{tikzpicture}
\end{document}
![enter image description here](https://i.stack.imgur.com/eJsGI.png)
For this kind of plots, the pgfplots
package seems a more natural choice:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-3,xmax=3,
ymin=-1.5,ymax=5,
axis on top]
\addplot[color=red,domain=-2.8:2.8,samples=100] {(\x)^4-4*(\x)^2+3};
\addplot+[mark=none,
domain=-1:1,
samples=100,
pattern=north east lines,
pattern color=red]{(\x)^4-4*(\x)^2+3} \closedcycle;
\addplot+[mark=none,
domain=1:1.7320508075688772,
samples=100,
pattern=crosshatch,
draw=red,
pattern color=blue!60]{(\x)^4-4*(\x)^2+3} \closedcycle;
\addplot+[mark=none,
domain=-1.7320508075688772:-1,
samples=100,
pattern=crosshatch,
draw=red,
pattern color=blue!60]{(\x)^4-4*(\x)^2+3} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
![enter image description here](https://i.stack.imgur.com/K7Fwk.png)
Best Answer
You can use additionaly the
pgfplots
package.The first way is using the coordinates directly in the code to produce the graphic.
The second way is use a table, from a
.dat
file.The
table.dat
file is a text document (you can create it with notepad), and the data is written inside it, in columns, like this:Every data is tab-separated.
Both schemes produce this:
In addition, you can describe a legend and labels for both axis.
The result: