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 am trying to create a tikzpicture similar to what I have made in MATLAB. MATLAB gives me the following result after running histogram:

myhistnorm = [ 0.1231   0   0   0   0   0   0   0.0001  0.0001  0.0004  0.001   0.0048  0.0313  0.1562  0.3464  0.2684  0.0645  0.0036  0 ]

I have used the following bin:

mybin = [ 1e-15 1e-14 1e-13 1e-12 1e-11 1e-10 1e-9 1e-8 1e-7 1e-6 1e-5 1e-4 1e-3 1e-2 1e-1 1e+0 1e+1 1e+2 1e+3 ]

The image of the figure, which is created in MATLAB, can be found in the following figure: Non-normalized histogram created in MATLAB

Although I created a normalized histogram using TikZ, it differs from the non-normalized figure that MATLAB has created in some other aspects.

\begin{tikzpicture}
\begin{axis}[
    width=\figurewidth,
    height=\figureheight,
    axis on top,
    scale only axis,
    xmode=log,
    ymode=log,
    ybar interval,
    x tick label as interval=false,
    xlabel = {$\boldsymbol{\nabla}\cdot{\bf u}_{int}$},
    xtick={},
    xtickten={-16,-14,-12,...,4},
    yticklabel={},
    xmin=1e-16, xmax=1e+4,
    ymax=1,
    grid=none,
    ymajorgrids,
]
\addplot [fill=gray!50] table [x=Lower, y=Count] {
Lower Upper Count
9.9e-15 1e-14    0.1231
1e-14 1e-13      0.0000
1e-13 1e-12      0.0000
1e-12 1e-11      0.0000
1e-11 1e-10      0.0000
1e-10 1e-9       0.0000
1e-9  1e-8       0.0000
1e-8  1e-7       0.0001
1e-7  1e-6       0.0001
1e-6  1e-5       0.0004
1e-5  1e-4       0.0010
1e-4  1e-3       0.0048
1e-3  1e-2       0.0313
1e-2  1e-1       0.1562
1e-1  1e+0       0.3464
1e+0  1e+1       0.2684
1e+1  1e+2       0.0645
1e+2  1e+3       0.0036
1e+3  1e+10      0.0000
};
\end{axis}
\end{tikzpicture}    

The result of compiling the above code is shown in the following figure Normalized histogram created by TikZ

My question is that why the bin between 1e-14 to 1e-8 are displayed differently than what I have entered in the table in the code? They should be all zero and only the first bin should have 0.1231 counts. Also, the bin (1e+2)-(1e+3) is also missing in the figure.

Thanks.

share|improve this question
2  
The way I created a PGFPlots histogram was export the data to plaintext and have PGFPlots compute the count values. If you have that data, consider using the hist plot type, hist/bins options and hist/density for normalization. –  marczellm Jan 9 '13 at 20:14
add comment

1 Answer

up vote 3 down vote accepted

The problem comes from the fact that zero values can't be handled properly in a logarithmic context. PGFPlots filters the entries with counts of zero out of the list, and then proceeds as if they had never been there, resulting in the wide first bar.

To work around this, you can manually specify a minimum y value, and assign the bars with a count of zero a value that's below the minimum value instead. That way, the width of the first bar will be calculated correctly, but the zero-bars won't show up in the plot. The behaviour isn't entirely intuitive, so you might want to consider posting a feature request to handle data points with values of zero even in logarithmic contexts for determining the bar width of interval plots.

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis on top,
    scale only axis,
    xmode=log,
    ymode=log,
    ybar interval,
    x tick label as interval=false,
    xtick={},
    xtickten={-16,-14,-12,...,4},
    yticklabel={},
    xmin=1e-16, xmax=1e+4,
    ymin=1e-5,ymax=1,
    grid=none,
    ymajorgrids,
    log origin=infty
]
\addplot [fill=gray!50] table [x=Lower, y=Count] {
Lower Upper Count
9.9e-15 1e-14    0.1231
1e-14 1e-13      1e-6
1e-13 1e-12      0.0000
1e-9  1e-8       0.0000
1e-8  1e-7       0.0001
1e-7  1e-6       0.0001
1e-6  1e-5       0.0004
1e-5  1e-4       0.0010
1e-4  1e-3       0.0048
1e-3  1e-2       0.0313
1e-2  1e-1       0.1562
1e-1  1e+0       0.3464
1e+0  1e+1       0.2684
1e+1  1e+2       0.0645
1e+2  1e+3       0.0036
1e+3  1e+10      0.0000
};
\end{axis}
\end{tikzpicture}   
\end{document}
share|improve this answer
    
Thanks Jake. Everything works fine for me except "log origin=infty". When I take it out it compiles the document otherwise it doesn't. –  Ahm Jan 9 '13 at 21:20
    
@Ahmad: Ah yes, that's a feature that has only been introduced in recent versions of PGFPlots. Sorry for that. –  Jake Jan 9 '13 at 21:21
    
I see. Could you also help me in changing the label of yticks? I want to show the y-axis in percent therefore I added "yticklabel={$0.001\%$, $0.01\%$, $0.1\%$, $1\%$, $10\%$, $100\%$}," to the code but it puts the whole thing for all the yticks. –  Ahm Jan 9 '13 at 21:33
1  
@Ahmad: You have to use yticklabels to define a list, not yticklabel. –  Jake Jan 9 '13 at 21:41
    
Do you think I can add a bar in the bin (1e-16)-(1e-15) with value of 100% without producing any gap between the bars? I added: \addplot [fill=gray!90] table [x=Lower, y=Count] { Lower Upper Count 1e-16 1e-15 1 }; However, there will be a gap for bins with a value greater than 1e-8. I want to simply overlay another bar from another dataset to the plot. Do you think it is possible? –  Ahm Jan 9 '13 at 23:02
add comment

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.