Graphics, Figures & TablesGrid with colored Cells

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

Grid with colored Cells

Post by tlapeg07 »

Hello,

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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

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

Grid with colored Cells

Post by tom »

Hi,

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.
tlapeg07
Posts: 2
Joined: Wed Jun 05, 2013 6:54 pm

Grid with colored Cells

Post by tlapeg07 »

Hello,

I've tried several ways, and in the end, it turned out to be easier in this way:

Code: Select all


\pgfmathsetmacro{\minval}{0}
\pgfmathsetmacro{\maxval}{2}
\pgfmathsetmacro{\colstep}{100/(\maxval-\minval)}
\begin{tikzpicture}[scale=0.3]
\foreach \x/\y/\z in {
1/0/1, 
22/0/1, 
40/0/1, 
53/0/1, 
...
187/99/1 
}
{
\pgfmathsetmacro{\ptcol}{\colstep*(\z-\minval)}
\fill[gray!\ptcol!white] (\x/4-0.25,\y/4-0.25) rectangle (\x/4,\y/4);
}
\draw (-0.25,-0.25) rectangle (200/4-0.25, 100/4-0.25);
\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.
Bye
Post Reply