2

I want to create a ycomb plot with visible range color gradient. When I create the scatter plot, the color gradient works, but it doesn't work with the comb plot, as you can see in the attached picture. How can I make the color gradient to work for comb plot?

enter image description here

\pgfplotsset{
    dirac/.style={
        mark=triangle*,
        mark options={yscale=1.3},
        ycomb,
        scatter,
        visualization depends on={(y-abs(y))/abs(y-abs(y)) \as \sign},
        scatter/@pre marker code/.code={\scope[rotate=180*\sign,yshift=-2pt]}
    }
}
\begin{tikzpicture}
\begin{axis}[
    axis x line=middle,
    axis line style = {->},
    width = 7cm,
    height = 5cm,
    xmax = 1.5,
    xmin = -1.5,
    ymax = 1.2,
    ymin = -1.2,
    xlabel = {$f$},
    every axis x label/.style={
    at={(ticklabel* cs:1.0)},
    anchor=west,
    },
    xtick=\empty,
    axis y line =none,
    clip=true,
    label style = {font=\footnotesize},
    colormap = {visiblelight}{
        color=(red) 
        color=(orange)
        color=(yellow)
        color=(green)
        color=(cyan)
        color=(blue)
        color=(violet)
    }
    ]

    \def\tau{1};
    \def\amp{2};
    \addplot[dirac,domain=-1:1,samples=13,scatter src=x]
    {\amp*exp(\tau*x)/(exp(2*\tau*x)+1)};
    \addplot+[scatter,mark=*,only marks,scatter src=x,domain=-1:1,samples=15]
    {sin(deg(x))};
\end{axis}
\end{tikzpicture}

2
  • 1
    Welcome to TeX.SX! I am not 100% sure if I understand your question right, but don't you get what you want by simply commenting the line that starts with scatter/@pre marker code? Commented Mar 3, 2022 at 19:22
  • Actually not, even without custom style and using ycomb in the addplot command this happens. It seems that the colormap feature is not available for comb plot. BTW, I found a workaround using quiver plots. I will edit the post and add my solution.
    – Farzad
    Commented Mar 4, 2022 at 20:27

1 Answer 1

3

After a lot of struggle, I found a workaround using quiver plot and scatter plot. I divided the plot into two different plots: With quiver plot I drew the vertical lines, and with scatter plot I drew the arrow heads.

enter image description here

Here is the code:

\begin{tikzpicture}
\begin{axis}[
    axis x line=middle,
    axis line style = {->},
    width = 12cm,
    height = 7cm,
    xmax = 1.3,
    xmin = -1.3,
    ymax = 1.2,
    ymin = -1.2,
    xlabel = {$f$},
    every axis x label/.style={
    at={(ticklabel* cs:1.0)},
    anchor=west,
    },
    xtick=\empty,
    axis y line =none,
    clip=false,
    label style = {font=\footnotesize},
    colormap = {visiblelight}{
        color=(red) 
        color=(orange)
        color=(yellow)
        color=(green)
        color=(cyan)
        color=(blue)
        color=(violet)
    }
    ]
    \def\tau{2.1};
    \def\amp{2};
    \def\nop{21};
    \addplot[
        quiver={u=0,v=-\amp*exp(\tau*x)/(exp(2*\tau*x)+1),colored},
        domain=-1:1,samples=\nop,scatter src=x]
    {\amp*exp(\tau*x)/(exp(2*\tau*x)+1)};

    \addplot+[scatter,scatter src=x,mark=triangle*,mark options={yscale=1.3},only marks,domain=-1:1,samples=\nop]
    {\amp*exp(\tau*x)/(exp(2*\tau*x)+1)};
\end{axis}
\end{tikzpicture}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .