Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Imagine a very simple PGFPlot, e.g.:

\begin{tikzpicture}
\begin{axis}
    \addplot+[no marks]{x^2};
\end{axis}
\end{tikzpicture}

How can I have the line "fade out" from the left to the right of the axis? I.e., how can I gradually vary the opacity of the line depending on the position on the x-axis?

I have managed to achieve this with the markers on a scatter plot with the following code:

\begin{tikzpicture}
\begin{axis}[
    scatter/@pre marker code/.code= {\expandafter\scope\expandafter[opacity=0.1+\pgfplotspointmetatransformed/1000]},
    scatter/@post marker code/.code={\endscope},
]
    \addplot+[scatter, only marks] {x^2};
\end{axis}
\end{tikzpicture}

But I cannot do it with a "no marks" line plot. Ideally, I would like the colour of the line to be defined by the cycle list (rather than having to use a custom made gradient).

share|improve this question

migrated from stackoverflow.com Aug 3 '12 at 17:57

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 10 down vote accepted

You can use a mesh plot and apply the same approach as you did for markers: by scaling by pgfplotspointmetatransformed.

The key idea is that the mesh implementation *re*evaluates tikz drawing options for every mesh segment. This is typically done to update the stroke/fill colors - but it works also for opacity or line width.

There is just one error message about \pgfplotspointmetatransformed being undeclared. However, the output is correct! The problem arises because the drawing options are evaluated in different contexts. If you make sure that \pgfplotspointmetatransformed has some useful global value, you do not run into the error message:

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\def\pgfplotspointmetatransformed{1000}
\begin{axis}
    \addplot+[no marks] {x^2*1.1};
    \addplot+[mesh,line width=8pt,opacity=\pgfplotspointmetatransformed/1000,no marks]{x^2};
\end{axis}
\end{tikzpicture}

\end{document}

Note that the default way that point meta is assigned is using shader=flat mean, i.e. by means of the mean of adjacent point meta data points. Use shader=flat corner to avoid averaging.

share|improve this answer
1  
That a really neat way to achieve an opacity gradient. The fade out from left to right get be done by the following options: color=blue,point meta=x,opacity=1-\pgfplotspointmetatransformed/1000 –  hakaze Aug 3 '12 at 20:59
    
Wow, thank you both, that is perfect! The only question I have is this: how can I automatically set "color=blue" (or whatever colour) according to the cycle list that is currently in use? I use a command to set the cycle list in all of my figures, so if I update the cycle list, I'd like this special plot to update its colour automatically too. –  Rich Aug 4 '12 at 10:47
    
The key mesh resets the color; it uses /tikz/color=mapped color internally. If you write mesh,blue, you get blue color... but it has already forgotten which color had been set in the cycle list :-/ –  Christian Feuersänger Aug 4 '12 at 11:09
    
I see. In that case, the best thing I can think to try is define all my colours as custom variables in my preamble (e.g. \color1, \color2, ...), use these variables in my cycle list. For mesh plots, use color=\colorX, where X is a manually chosen cycle list position. Not ideal but should do the trick! Thanks again. –  Rich Aug 4 '12 at 12:22

You can use a 1D mesh plot and a custom colormap which contains the opacity gradient. The gradient is set along the x-axis by means of the point meta key.

\pgfplotsset{colormap={opaqueblue}{color(0cm)=(blue) ; color(1cm)=(blue!0)}}

\begin{tikzpicture}
\begin{axis}[colormap name=opaqueblue]
    \addplot[thick, mesh, point meta=x] {x^2};
\end{axis}
\end{tikzpicture}
share|improve this answer
    
Cool idea! Are you sure that the blue!100 syntax causes opacity? I thought it would merely mix with some other color (white? black?) –  Christian Feuersänger Aug 3 '12 at 20:28
    
This is nice but valid only when you have a white background –  percusse Aug 3 '12 at 20:28
    
@ChristianFeuersänger,@percusse: you are right, I hadn't considered that. This way it is merely mixing blue and white. It becomes obvious when I set the background to another color. –  hakaze Aug 3 '12 at 20:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.