Consider the following MWE:
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis equal,
axis lines = middle,
xlabel = $x$,
ylabel = $y$,
xmin = -0.4, xmax = 0.4,
ymin = -0.4, ymax = 0.4,
grid = both,
grid style = {line width=.1pt, draw=gray!10},
major grid style = {line width=.2pt, draw=gray!50},
samples = 200,
legend pos = north west,
width = 10cm,
height = 8cm
]
% Plot the curve
\addplot[domain=-0.4:0.4, thick, blue] {x^2*sin(1/x)};
\addlegendentry{$y = x^2\sin(1/x)$}
% Tangent line at (1, -2)
\addplot[domain=-0.5:0.5, thick, red] {x^2};
\addplot[domain=-0.5:0.5, thick, red] {-x^2};
\addlegendentry{$y=\pm x^2$}
\end{axis}
\end{tikzpicture}
\end{document}
Currently, I am getting:
But I would like the curve to show like in the following image:
What am I missing?




