TeX - LaTeX Asked by Markus G. on June 24, 2021
I’m trying to plot an image, which is basically a matrix of intensity values. I want the colour scheme to range from black for the lowest to white for the highest value, with a defined colour in the middle. (I based my approach on this answer to a similar question.)
Here is a MWE:
documentclass[tikz]{standalone}
begin{document}
begin{tikzpicture}[scale=1]
foreach y [count=n] in { %this is normally a VERY large matrix
{0,50},
{75,100},
} {foreach x [count=m] in y {fill[white!x!red!x!black] (1*n,-1*m) rectangle ++(1,1);}}
end{tikzpicture}
end{document}
The problem is that my values in absolute terms range from 0 to about 400 and in my current approach I need to normalise them to a range from 0 to 100. This technically works, however, this means that I loose 3/4 of my colour information. Unfortunately, the data is very sensitive and I am now looking for a way to use a larger range.
My new approach is dividing the colour scheme into two parts, so that I can use at least values from 0 to 200. However, this does not seem to work, and I am not that proficient in using if-commands in LaTeX. (I am aware of this answer by Christian Feuersänger to the same question reference above, but suffice it to say that I do not fully understand it and therefore struggle with adapting it to my case.)
documentclass[tikz]{standalone}
usepackage{calc}
usepackage{ifthen}
begin{document}
begin{tikzpicture}[scale=1]
foreach y [count=n] in {
{0,100},
{150,200},
}
{foreach x [count=m] in y {
fill[ifnumx<100 {white!x!red} else {red!{x/2}!black}fi] (1*n,-1*m) rectangle ++(1,1);
}}
end{tikzpicture}
end{document}
Can someone tell me, what my mistake is or maybe even how to solve this more efficiently?
Not sure if you want to achieve this exact result, but the code with corrections is this one:
documentclass[tikz]{standalone}
usetikzlibrary{calc}
usepackage{xcolor}
usepackage{ifthen}
begin{document}
begin{tikzpicture}[scale=1]
foreach y [count=n] in {
{0,100},{150,200}}
{foreach x [count=m] in y {
pgfmathtruncatemacromyx{x/2}%
xdefmyColor{ifnumx<100 white!x!redelse red!myx!blackfi}%
fill[color={myColor}] (1*n,-1*m) rectangle ++(1,1);
}}
end{tikzpicture}
end{document}
Edit:
In order to not lose the range (0--400) you can add some green in the red color (half green.. to make it yellowish) and keep the RGB definition (0--255)... So, your range will be 2times 255 = [0--510]):
documentclass[tikz]{standalone}
usetikzlibrary{calc}
usepackage{xcolor}
usepackage{ifthen}
begin{document}
begin{tikzpicture}[scale=1]
foreach y [count=n] in {
{0,100, 200},{150,200,400}}
{foreach x [count=m] in y {
pgfmathtruncatemacromyx{ifnumx>255{x/2}else{x}fi}%
pgfmathtruncatemacromyy{ifnumx>255 127else 0fi}%
definecolor{myColorm}{RGB}{myx,myy,0}%
fill[color={myColorm}] (1*n,-1*m) rectangle ++(1,1);
}}
end{tikzpicture}
end{document}
Edit2:
For more "gradient" approach use the functions:
pgfmathtruncatemacromyx{ifnumx>255{255}else{x}fi}%
pgfmathtruncatemacromyy{ifnumx>255{x-255}else 0fi}%
Correct answer by koleygr on June 24, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP