x-2-quadratic-function.tex 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. \documentclass[varwidth=true, border=2pt]{standalone}
  2. \usepackage{pgfplots}
  3. \usepackage{tikz}
  4. \usetikzlibrary{patterns}
  5. \tikzset{
  6. hatch distance/.store in=\hatchdistance,
  7. hatch distance=10pt,
  8. hatch thickness/.store in=\hatchthickness,
  9. hatch thickness=2pt
  10. }
  11. \makeatletter
  12. \pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
  13. {\pgfqpoint{0pt}{0pt}}
  14. {\pgfqpoint{\hatchdistance}{\hatchdistance}}
  15. {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
  16. {
  17. \pgfsetcolor{\tikz@pattern@color}
  18. \pgfsetlinewidth{\hatchthickness}
  19. \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  20. \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
  21. \pgfusepath{stroke}
  22. }
  23. \begin{document}
  24. \begin{tikzpicture}
  25. \begin{axis}[
  26. legend pos=north east,
  27. axis x line=middle,
  28. axis y line=middle,
  29. grid = major,
  30. width=8cm,
  31. height=8cm,
  32. grid style={dashed, gray!30},
  33. xmin= 0, % start the diagram at this x-coordinate
  34. xmax= 2, % end the diagram at this x-coordinate
  35. ymin= 0, % start the diagram at this y-coordinate
  36. ymax= 2, % end the diagram at this y-coordinate
  37. xlabel=$x$,
  38. ylabel=$y$,
  39. %xticklabels={-2,-1.6,...,2},
  40. %yticklabels={-8,-7,...,8},
  41. tick align=outside,
  42. enlargelimits=true,
  43. tension=0.08]
  44. % plot it
  45. \addplot[domain=0:2, red, thick,samples=500] {x*x};
  46. \addplot[pattern=flexible hatch,
  47. area legend,
  48. pattern color=blue, domain=0:1,samples=500] {x*x} \closedcycle;
  49. \addlegendentry{$f(x)=x^2$}
  50. \addlegendentry{$\int_0^1 x^2 \mathrm{d} x$}
  51. \end{axis}
  52. \end{tikzpicture}
  53. \end{document}