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.

I have a ybar interval pgfplot with variable width intervals. On top of each interval I am drawing a line with the interval's width (using a second plot with jump mark left). However, I cannot find a way to tell pgf to draw those lines in the color of the current colormap. Instead, they are always drawn using the color specified using draw=.

In my rendering below, I want the red lines to be in a color representing their y-value instead:
bad rendering

One thing that also catches my attention is that the labels alongside the colorbar have nothing to do with the y values in my actual plot, but they should.

MWE:

\documentclass{standalone}

\usepackage{filecontents}
\begin{filecontents}{data.csv}
height,width
1,884
2,5768
3,835
4,2661
5,492
6,1349
7,486
8,1117
9,498
10,854
10,0
\end{filecontents}
\usepackage{pgfplots, pgfplotstable}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
    ymode=log,
    colorbar,
]
\addplot[
    ybar interval,
    fill=cyan,
    draw=none,
] table [
    col sep=comma,
    create on use/xaccum/.style={
        create col/expr=\pgfmathaccuma+\prevrow{width}
    },
    x=xaccum, y=height,
] {data.csv};
\addplot[
    jump mark left,
    draw=red, %this should be a color based on the y value
] table [
    col sep=comma,
    create on use/xaccum/.style={
        create col/expr=\pgfmathaccuma+\prevrow{width}
    },
    x=xaccum, y=height,
] {data.csv};

\end{axis}
\end{tikzpicture}
\end{document}
share|improve this question
    
Maybe you can look this question: colorbar-question –  selwyndd21 Jan 11 at 15:48
    
I had seen that question before, but I don't see how it applies here. Could you please elaborate on that? –  derabbink Jan 11 at 15:51
    
I see. that answer is too complicated for me, too. –  selwyndd21 Jan 11 at 15:54

2 Answers 2

up vote 2 down vote accepted

The line style is changed in changing the data format.

The colobar + mesh cannot support jump plot, so we chage the data to manually make an jump data.

The Final results are shown below: enter image description here

\documentclass{standalone}

\usepackage{filecontents}
\begin{filecontents}{data.csv}
height,width
1,884  
1,0  % new data
2,5768
2,0  % new data
3,835
3,0  % new data
4,2661
4,0  % new data
5,492
5,0  % new data
6,1349
6,0  % new data
7,486
7,0  % new data
8,1117
8,0  % new data
9,498
9,0  % new data
10,854
10,0  % new data
10,0
10,0  % new data
\end{filecontents}
\usepackage{pgfplots, pgfplotstable}

\begin{document}

% Define new pgfmath function for the logarithm
% to base 10 that also works with fpu library
\pgfmathdeclarefunction{lg10}{1}{%
    \pgfmathparse{ln(#1)/ln(10)}%
}

\begin{tikzpicture}

\begin{axis}[
    ymode=log,
    colorbar, 
    colorbar style={
      ytick={0,1}, %  only show 10^0 and 10^1 tick in colorbar
      % yticklabel retreive the 10^# function in data
      yticklabel=\pgfmathparse{10^\tick}\pgfmathprintnumber\pgfmathresult,
    },
]
\addplot[
    mesh, thick, % use mesh function
    point meta=explicit, % use the "meta expr" defined below
] table [
    col sep=comma,
    create on use/xaccum/.style={
        create col/expr=\pgfmathaccuma+\prevrow{width}
    },
    meta expr=lg10(\thisrow{height}), % transform "order" into "linear" for colorbar
    x=xaccum, y=height,
] {data.csv};

\end{axis}
\end{tikzpicture}

Steps are as follow:

  1. Change the input data: Manually fix the x axis to make constant line.

    3,835
    3,0  % new data
    

    The width is set 0 to make cumulative axis remain same number.

  2. Plot the mesh line with colobar: use the colorbar+ mesh to plot the color line.

  3. Define number to order function: this function are based on The question: colorbar with log.

    \pgfmathdeclarefunction{lg10}{1}{%
      \pgfmathparse{ln(#1)/ln(10)}%
    }
    

    colorbar support the data only in linear format, not in log type. Leaves the linear order as input data for colorbar.

  4. Write linear data to colorbar: use the point meta=explicit,+meta expr=lg10(\thisrow{height}) to feed colorbar with the linear data.

  5. Retrieve ytick format: transform back to exponential format in the yticklabel.

    colorbar style={
      yticklabel=\pgfmathparse{10^\tick}\pgfmathprintnumber\pgfmathresult,
    },
    
  6. leaves the 10^0 and 10^1: because your data range is small, I supress the ugly intermediate y ticks.

    ytick={0,1}, %  only show 10^0 and 10^1 tick in colorbar
    
share|improve this answer
    
Close enough. I can live just fine with the vertical lines :) Thanks! –  derabbink Jan 12 at 16:31

Though I cannot completely draw what you want, I put it in the answer zone.

My output is as follow: enter image description here

  • As you mentioned, draw cannot accept colormap.

If you want to color the line, mesh is your choice. But the jump mark left, conflict with mesh (change the order, you will get different result).


  • Fill color under the line.

To do so, two plottings are combined using the const plot with the option \closedcycle.

The colorbar-question provides the answer to fill colors, shading. It should be defined before the plotting

\pgfdeclareverticalshading{stefan}{50bp}{
  color(0bp)=(violet);
  color(10bp)=(blue);
  color(20bp)=(green);
  color(30bp)=(yellow);
  color(40bp)=(orange);
  color(50bp)=(red)
}

It define the color spacing 0 to 50, every 10 change one color.

Then change the colors in colorbar:

    colormap={new}{
      color(0)=(violet);color(0.2)=(blue);
      color(0.4)=(green);color(0.6)=(yellow);
      color(0.8)=(orange);color(1)=(red)},

it divide [0,1] into 5 pieces.


  • Change the ytick in colorbar

        point meta min=1,point meta max=10,
    

    Noting that the colorbar is for linear interpolating, not for log-type interpolation. That maybe the reason why point meta=y doesn't work fine in your case. I set the minimum and maximum instead.


whole input are list below:

    \documentclass{standalone}

    \usepackage{filecontents}
    \begin{filecontents}{data.csv}
    height,width
    1,884
    2,5768
    3,835
    4,2661
    5,492
    6,1349
    7,486
    8,1117
    9,498
    10,854
    10,0
    \end{filecontents}
    \usepackage{pgfplots, pgfplotstable}

    \begin{document}
    \begin{tikzpicture}
    \pgfdeclareverticalshading{stefan}{50bp}{ % define shading color
      color(0bp)=(violet);
      color(10bp)=(blue);
      color(20bp)=(green);
      color(30bp)=(yellow);
      color(40bp)=(orange);
      color(50bp)=(red)
    }

    \begin{axis}[
        ymode=log,
        colorbar,
        colormap={new}{  % define colorbar color
          color(0)=(violet);color(0.2)=(blue);
          color(0.4)=(green);color(0.6)=(yellow);
          color(0.8)=(orange);color(1)=(red)},
        ymin=1,ymax=10,  
    ]
    \addplot[
        const plot,    % combine line and filling
        shading=stefan,% use the shading color
        point meta min=1,point meta max=10,% set colorbar y min and max
        draw=black,line width={0.1pt},     % set lines color and thickness
    ] table [
        col sep=comma,
        create on use/xaccum/.style={
            create col/expr=\pgfmathaccuma+\prevrow{width}
        },
        x=xaccum, y=height,
    ] {data.csv}  \closedcycle; % close the area to fill colors
    \end{axis}
    \end{tikzpicture}
    \end{document}
share|improve this answer
    
It is indeed in the answer zone: The top of each interval has the desired color. However, my style guide won't allow me to use this :( –  derabbink Jan 12 at 9:48
    
It seems that the pgfplots does not support the const plotting with colormap. Maybe the data can be transform into another format, for example (1,884) --(2,884) -- (2,5768) can make an right angle, intead of using the constant plot. –  selwyndd21 Jan 12 at 11:25

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.