4

I want to produce a horizontal colour bar with colours varying from black and grey to white. I searched the net for a small code but I couldn't find any.

Could you please help produce something like this?

enter image description here

6
\documentclass[border=3mm,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,10,20,...,90}
    \fill[black!\i] (\i mm,0) rectangle ++(10mm,10mm);
\end{tikzpicture}
\end{document}

enter image description here

2
  • Thank you so much. Could you please explain parts of the code \foreach, \i mm,0, rectangle ++(10mm,10mm), etc.? Can I replace them with other options?
    – enthu
    Oct 17 '14 at 9:51
  • 2
    @EnthusiasticStudent I could explain them, but I think it would be better if you take a look at first tutorials from TiKZ documentation. It's on your system texdoc pgfmanual. Another possibility could be searching in this site, there's nothing not previously explained here in my code.
    – Ignasi
    Oct 17 '14 at 10:47
7

pgfplots generates color bars if you add the colorbar key to the axis environment. A horizontal colorbar is possible by means of colorbar horizontal.

Colorbars are (only) useful if there is some kind of "color data" in your axis: data which is mapped into a colormap. This is the case for scatter, mesh, surf, contour, and some other plot types. A simple example is show below.

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.9}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        colormap/blackwhite,
        colorbar horizontal,
    ]
    \addplot[scatter] {rand};   
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        colormap/blackwhite,
        colorbar horizontal,
        colorbar sampled,
    ]
    \addplot[scatter] {rand};   
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

The example(s) use a black/white colormap and colorbar horizontal.

Note that I made two plots: one has colorbar sampled, a special key to generate non-continuous color bars as in your image.

More details can be found in the pgfplots manual.

2
  • Thank you so much for your answer. But I just wanted that colour bar, I mean, a colour bar which is not included in a plot.
    – enthu
    Oct 18 '14 at 19:26
  • Thanks for the note! In this case, I still hope that my answer helps others which search for related keywords. Oct 19 '14 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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