pgf-tikz / pgfplots

pgfplots - A TeX package to draw normal and/or logarithmic plots directly in TeX in two and three dimensions with a user-friendly interface and pgfplotstable - a TeX package to round and format numerical tables. Examples in manuals and/or on web site.
http://pgfplots.sourceforge.net/
187 stars 33 forks source link

1d mesh is a collection of disconnected rectangles #446

Open Atcold opened 1 year ago

Atcold commented 1 year ago

I'd like a smooth 1d mesh, line the black curve, but coloured. All I'm able to get is the collection of rectangles shown in the figure below…

\documentclass{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\begin{document}\begin{tikzpicture}\begin{axis}
  \addplot [mesh,line width=20pt,domain=-1:1,samples=101] {sin(360*x)*x)};
  \addplot [no marks,line width=20pt,domain=-1:1,samples=101] {sin(360*x)*x-1};
\end{axis}\end{tikzpicture}\end{document}

image

ilayn commented 1 year ago

With the PS/PDF operations that TikZ operates, you cannot alter the line properties midway. If it starts in one config until the path is "inked" you are stuck with it. What you want to do here is to change the colors of the black line which is not possible.

If you chop it up like you did here then you would obviously lose all the nice curvy path drawing. So the remaining option is to reveal a shading via the path ala https://tex.stackexchange.com/questions/134283/tikz-shading-a-path-without-any-filling

Apparently more people chimed in and there are good answers compared to my neanderthal answer (percusse). So you might want to check it.

Or you draw the thick line as a surface and let pgfplots handle the mesh coloring but that requires more work on parametrization of the surface.

muzimuzhi commented 1 year ago

Linking related/identical question on TeX-SX, which has received an accepted answer using three extra mesh plots with manually shifted samples. https://tex.stackexchange.com/q/664854

Theoretically it's possible to provide a new option smooth mesh, but that need a deep understanding about \pgfplotsplothandlermesh, the plot handler beneath mesh option.


It seems the key is to automatically make up line joins. A non-trivial soft path manipulation task.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=3, line width=15pt]
  % miter line join
  \draw (0,0) -- (1,0) -- (1,1);

  % no line join
  \begin{scope}[xshift=1.5cm]
    \draw[blue] (0,0) -- (1,0);
    \draw[red]  (1,0) -- (1,1);
  \end{scope}

  % made-up line join
  \begin{scope}[xshift=3cm]
    % target to fill, for debug
    % \draw[draw opacity=.3] (0,0) -- (1,0) -- (1,1);
    \fill[blue]
      (0,0) ++(0pt,-2.5pt) -- ++(0pt,5pt) -- ++(1cm-2.5pt,0) -- ++(5pt,-5pt) -- cycle;
    \fill[red]
      (1,0) ++(-2.5pt,2.5pt) -- ++(5pt,-5pt) -- ++(0,1cm+2.5pt) -- ++(-5pt,0) -- cycle;
  \end{scope}
\end{tikzpicture}
\end{document}
image
Atcold commented 1 year ago

Theoretically it's possible to provide a new option smooth mesh, but that need a deep understanding about \pgfplotsplothandlermesh, the plot handler beneath mesh option.

Would anyone be interested in adding this? As you pointed out, I was already given a hack to handle this quirk.