LaTeX forum ⇒ Graphics, Figures & TablesGrid with colored Cells

Information and discussion about graphics, figures & tables in LaTeX documents.
Posts: 2
Joined: Wed Jun 05, 2013 6:54 pm

Grid with colored Cells

Postby tlapeg07 » Wed Jun 05, 2013 7:03 pm


I would like to build a grid and to specify the color of each cell. The grid is very large so I want to do it with file data. I have absolutely no idea of how to do it. Do you have any idea ?

The idea is to obtain in LaTeX what you obtain with Excel when you use the conditional formatting on a grid of values. Here I do not want to draw the values, but only the corresponding color (there is no place to write the numbers.)

Thank you in advance.


User avatar
Posts: 73
Joined: Thu Apr 18, 2013 4:02 am

Postby tom » Tue Jun 11, 2013 4:32 am


That's an interesting question. You could make your life easier and use e.g. R to plot a heat map. If you rather do it in TeX, I recommend using pgf (TiKZ). You can start from here and read your data as shown here.

Let me know if you get stuck.

Posts: 2
Joined: Wed Jun 05, 2013 6:54 pm

Postby tlapeg07 » Wed Jun 12, 2013 9:03 am


I've tried several ways, and in the end, it turned out to be easier in this way:
  1. \pgfmathsetmacro{\minval}{0}
  2. \pgfmathsetmacro{\maxval}{2}
  3. \pgfmathsetmacro{\colstep}{100/(\maxval-\minval)}
  4. \begin{tikzpicture}[scale=0.3]
  5. \foreach \x/\y/\z in {
  6. 1/0/1,
  7. 22/0/1,
  8. 40/0/1,
  9. 53/0/1,
  10. ...
  11. 187/99/1
  12. }
  13. {
  14. \pgfmathsetmacro{\ptcol}{\colstep*(\z-\minval)}
  15. \fill[gray!\ptcol!white] (\x/4-0.25,\y/4-0.25) rectangle (\x/4,\y/4);
  16. }
  17. \draw (-0.25,-0.25) rectangle (200/4-0.25, 100/4-0.25);
  18. \end{tikzpicture}

However, it is difficult to show more than 5000 cells (=5000 lines of data).
To increase the number of cells that I can show, I used a gray to white gradient color. Thus, I show gray and light gray cells, but there is no need to specify the white cells, which correspond then to the background.

Hope it helps someone else.

Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 8 guests