3

I have some trouble with an histogram filling when I plot it with ymode=log. Here is a MWE

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      xmode=normal,
      ymode=log,
      log origin=infty]
    % Draw series plot
    \addplot[red,fill=red!70,fill opacity=0.2,const plot] coordinates {
        (2, 20335)
        (2.048, 15937)
        (2.096, 12430)
        (2.144, 9469)
        (2.192, 6848)
        (2.24, 4913)
        (2.288, 3428)
        (2.336, 2276)
        (2.384, 1582)
        (2.432, 884)
        (2.48, 494)
        (2.528, 269)
        (2.576, 134)
        (2.624, 65)
        (2.672, 18)
        (2.72, 1)
        (2.768, 2)
        (2.816, 3)
        (2.864, 0)
        (2.912, 0)
        (2.96, 0)
        (3.008, 0)
        (3.056, 0)
        (3.104, 0)
        (3.152, 0)
        (3.2, 0)
    }
    \closedcycle;
  \end{axis}
\end{tikzpicture}
\end{document}

which gives me that result

enter image description here

The last bin arround 2.8 jumps due to the fact that the next bin value is 0 and thus not "plottable" in log scale. I know how to solve this issue by replacing (2.864, 0) to something like (2.864, 1e-6). Nevertheless, I would like to automatically change zero value within an histogram by let's say an epsilon shift to avoid such issue (my plots are generated by a third-party software and I would like to avoid editing the produced LaTeX file).

So far, I have try to use y filter/.code trick in order to add a small epsilon value to each y value by writing something like

\addplot[red,fill=red!70,fill opacity=0.2,const plot, 
         y filter/.code=\pgfmathparse{\pgfmathresult+1e-6}]

but this shift every bin by 1.

Xavier Garrido
  • 305
  • 1
  • 11
  • The problem with your epsilon trick is that the y filter works in the logarithmically transformed domain, which is why you end up with a line very close to 1e0. In your real application, do you use \addplot coordinates (with brackets around the coordinates) or \addplot table? If the latter is the case, you can use \addplot table [y expr=\thisrowno{1}+1e-6] ... to add the epsilon to the untransformed values. – Jake Jun 24 '14 at 08:17
  • I use \addplot coordinates as shown in the example. I am not sure I get the logarithmic transform explanation : you mean that pgfplots is actually doing log10(\pgfmathresult)+1e-6 ? This does not lead to 1. Should I use an exponential transform ? – Xavier Garrido Jun 24 '14 at 17:00
  • It's doing ln(<y coordinate>) + 1e-6. The coordinates with a value of zero are filtered out, which results in <nothing> + 1e-6, which, when transformed back, is very close to 1. Can you teach the third party software to generate a more "normal" table format (without the brackets)? – Jake Jun 24 '14 at 17:33
  • I can and then I can add your [y expr=\thisrowno{1}+1e-6] to the \addplot table expression. So this solved my problem. How to proceed to validate your comment/answer ? Can you post your comment as answer in such a way I validate it ? – Xavier Garrido Jun 24 '14 at 20:34
  • Try using the restrict y to domain=ymin:ymax option (documented in the pgfplots manual). I think there is no need to apply a filter/expression operation. – alfC Jun 27 '14 at 19:50
  • @alfC this breaks all the filling area. The plot looks like a cubist painting à la Picasso ;) – Xavier Garrido Jun 28 '14 at 19:58
  • @XavierGarrido, I see, well, that is another problem http://tex.stackexchange.com/questions/84863/2d-graphs-in-a-3d-plot. Can you convert the 0 to nan and skip nans (there is an option for that). – alfC Jun 28 '14 at 23:14

1 Answers1

3

The problem with your epsilon trick is that the y filter works in the logarithmically transformed domain, which is why you end up with a line very close to 1e0: It's doing ln(<y coordinate>) + 1e-6. The coordinates with a value of zero are filtered out, which results in <nothing> + 1e-6, which, when transformed back, is very close to 1.

If you can format the data table to use a "conventional" format (no brackets around the coordinates), you can use \addplot table [y expr=\thisrowno{1}+1e-6] ... to add the epsilon to the untransformed values.

Jake
  • 232,450