6

I am trying to put two boxes in blue and red beside the legend text in a histogram plot. I have tried several things including the one in this link. Unfortunately, the code in the link does not compile for me. I have pgf version 1.7. It is very unfortunate that the code in that link does not work for me. It looks very flexible and powerful.

Here is the code I used:

\begin{tikzpicture}
\begin{axis}[
    width=\figurewidth,
    height=\figureheight,
    scale only axis,
    xmode=log,
    ymode=log,
    xlabel = {Velocity Divergency},
    x tick label as interval=false,
    xtick={},
    xtickten={-18,-16,...,4},
    yticklabels={$0.0001\%$, $0.001\%$, $0.01\%$, $0.1\%$, $1\%$, $10\%$, $100\%$},
    xmin=1e-18, xmax=1e+4,
    ymin=1e-5,ymax=1,
    grid=none,
    ymajorgrids,
    log origin=infty,
    bar shift=0pt,
    % align right:
    legend style={
                  cells={anchor=west},
                  legend pos=outer north east,
    }
]
\addplot [fill=blue!100,
    ybar interval] table [x=Lower, y=Count] {
Lower Upper Count
9.9e-15 1e-14      0.1231
1e-14   1e-13      1e-15
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+4       1e-15
};
\addlegendentry[blue]{Interp}
\addplot [fill=red!100, ybar interval] table [x=Lower, y=Count] {
Lower Count
1e-17 1
1e-16 1
};
\addlegendentry[red]{Div-free}
\end{axis}
\end{tikzpicture}

The above code results in the following figure:enter image description here

Could someone please help me add two solid rectangles beside the legend texts so that the reader easily finds the corresponding dataset, similar to the following?

enter image description here

  • 1
    Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. – Benedikt Bauer Jan 10 '13 at 08:17

1 Answers1

7

The answer here is very simple, since pgfplots defines a key called area legend (refer to the documentation page 173).

Then your code plug into a mwe:

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

\begin{document}

\begin{tikzpicture} \begin{axis}[ width=11cm, height=7cm, scale only axis, xmode=log, ymode=log, xlabel = {Velocity Divergency}, x tick label as interval=false, xtick={}, xtickten={-18,-16,...,4}, yticklabels={$0.0001%$, $0.001%$, $0.01%$, $0.1%$, $1%$, $10%$, $100%$}, xmin=1e-18, xmax=1e+4, ymin=1e-5,ymax=1, grid=none, ymajorgrids, log origin=infty, bar shift=0pt, area legend, % notice the key here % align right: legend style={ cells={anchor=west}, legend pos=outer north east, } ] \addplot [fill=blue!100, ybar interval] table [x=Lower, y=Count] { Lower Upper Count 9.9e-15 1e-14 0.1231 1e-14 1e-13 1e-15 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+4 1e-15 }; \addlegendentry[blue]{Interp} \addplot [fill=red!100, ybar interval] table [x=Lower, y=Count] { Lower Count 1e-17 1 1e-16 1 }; \addlegendentry[red]{Div-free} \end{axis} \end{tikzpicture} \end{document}

The result:

enter image description here

Note

In my mwe I didn't use your \figurewidth and \figureheight, but it's easy for you to replace them again.