TikZ: colors stored in matrix with coordinates inferred

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I'd like to use data stored in an external CSV file to fill nodes inside a tikzpicture matrix. If my data is stored as x/y/z, I can write the following loop (put together from this and this):



documentclassstandalone
usepackagetikz
usepackagexcolor
usepackagecatchfile
newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.dat
1/1/2, 1/2/8, 1/3/2,
2/1/8, 2/2/10, 2/3/8,
3/1/2, 3/2/8, 3/3/2
endfilecontents*

begindocument
begintikzpicture
loaddatajobname.dat
foreach x/y/c in loadeddata
pgfmathsetmacrokc*10
fill[color=red!k] (x,y) rectangle +(1,1);

endtikzpicture
enddocument


My data, instead, is stored as a matrix of z values (color intensities), e.g.



beginfilecontents*jobname.csv
2, 8, 2
8, 10, 8
2, 8, 2
endfilecontents*


Please help me adapt the code so that I can read the colors from the file and that the x and y coordinates are inferred from the cell position inside the matrix.



The tikzpicture looks like this:



enter image description here










share|improve this question





















  • Do you want to read the actual color from your dat file (red!20), or do you prefer it as you have it? Also not all these data needed in dat file.... just the color could be enough.
    – koleygr
    3 hours ago







  • 1




    The data is a matrix of integers and I will map these integers z_i to an intensity of red with red!z_i for each z_i! Thanks.
    – PatrickT
    3 hours ago










  • Ok... but it could be a matrix of integers with just the color value like z_i but without 1/1/z_1 1/2/z_2 etc... Would this be accepted or you prefer your own dat file format?
    – koleygr
    3 hours ago










  • It's a matrix of integers like 2, 8, 2\ 8, 10, 8\ 2, 8, 2.
    – PatrickT
    3 hours ago






  • 1




    Thanks, got it.
    – koleygr
    3 hours ago














up vote
3
down vote

favorite












I'd like to use data stored in an external CSV file to fill nodes inside a tikzpicture matrix. If my data is stored as x/y/z, I can write the following loop (put together from this and this):



documentclassstandalone
usepackagetikz
usepackagexcolor
usepackagecatchfile
newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.dat
1/1/2, 1/2/8, 1/3/2,
2/1/8, 2/2/10, 2/3/8,
3/1/2, 3/2/8, 3/3/2
endfilecontents*

begindocument
begintikzpicture
loaddatajobname.dat
foreach x/y/c in loadeddata
pgfmathsetmacrokc*10
fill[color=red!k] (x,y) rectangle +(1,1);

endtikzpicture
enddocument


My data, instead, is stored as a matrix of z values (color intensities), e.g.



beginfilecontents*jobname.csv
2, 8, 2
8, 10, 8
2, 8, 2
endfilecontents*


Please help me adapt the code so that I can read the colors from the file and that the x and y coordinates are inferred from the cell position inside the matrix.



The tikzpicture looks like this:



enter image description here










share|improve this question





















  • Do you want to read the actual color from your dat file (red!20), or do you prefer it as you have it? Also not all these data needed in dat file.... just the color could be enough.
    – koleygr
    3 hours ago







  • 1




    The data is a matrix of integers and I will map these integers z_i to an intensity of red with red!z_i for each z_i! Thanks.
    – PatrickT
    3 hours ago










  • Ok... but it could be a matrix of integers with just the color value like z_i but without 1/1/z_1 1/2/z_2 etc... Would this be accepted or you prefer your own dat file format?
    – koleygr
    3 hours ago










  • It's a matrix of integers like 2, 8, 2\ 8, 10, 8\ 2, 8, 2.
    – PatrickT
    3 hours ago






  • 1




    Thanks, got it.
    – koleygr
    3 hours ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I'd like to use data stored in an external CSV file to fill nodes inside a tikzpicture matrix. If my data is stored as x/y/z, I can write the following loop (put together from this and this):



documentclassstandalone
usepackagetikz
usepackagexcolor
usepackagecatchfile
newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.dat
1/1/2, 1/2/8, 1/3/2,
2/1/8, 2/2/10, 2/3/8,
3/1/2, 3/2/8, 3/3/2
endfilecontents*

begindocument
begintikzpicture
loaddatajobname.dat
foreach x/y/c in loadeddata
pgfmathsetmacrokc*10
fill[color=red!k] (x,y) rectangle +(1,1);

endtikzpicture
enddocument


My data, instead, is stored as a matrix of z values (color intensities), e.g.



beginfilecontents*jobname.csv
2, 8, 2
8, 10, 8
2, 8, 2
endfilecontents*


Please help me adapt the code so that I can read the colors from the file and that the x and y coordinates are inferred from the cell position inside the matrix.



The tikzpicture looks like this:



enter image description here










share|improve this question













I'd like to use data stored in an external CSV file to fill nodes inside a tikzpicture matrix. If my data is stored as x/y/z, I can write the following loop (put together from this and this):



documentclassstandalone
usepackagetikz
usepackagexcolor
usepackagecatchfile
newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.dat
1/1/2, 1/2/8, 1/3/2,
2/1/8, 2/2/10, 2/3/8,
3/1/2, 3/2/8, 3/3/2
endfilecontents*

begindocument
begintikzpicture
loaddatajobname.dat
foreach x/y/c in loadeddata
pgfmathsetmacrokc*10
fill[color=red!k] (x,y) rectangle +(1,1);

endtikzpicture
enddocument


My data, instead, is stored as a matrix of z values (color intensities), e.g.



beginfilecontents*jobname.csv
2, 8, 2
8, 10, 8
2, 8, 2
endfilecontents*


Please help me adapt the code so that I can read the colors from the file and that the x and y coordinates are inferred from the cell position inside the matrix.



The tikzpicture looks like this:



enter image description here







tikz-pgf csv catchfile






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









PatrickT

90421022




90421022











  • Do you want to read the actual color from your dat file (red!20), or do you prefer it as you have it? Also not all these data needed in dat file.... just the color could be enough.
    – koleygr
    3 hours ago







  • 1




    The data is a matrix of integers and I will map these integers z_i to an intensity of red with red!z_i for each z_i! Thanks.
    – PatrickT
    3 hours ago










  • Ok... but it could be a matrix of integers with just the color value like z_i but without 1/1/z_1 1/2/z_2 etc... Would this be accepted or you prefer your own dat file format?
    – koleygr
    3 hours ago










  • It's a matrix of integers like 2, 8, 2\ 8, 10, 8\ 2, 8, 2.
    – PatrickT
    3 hours ago






  • 1




    Thanks, got it.
    – koleygr
    3 hours ago
















  • Do you want to read the actual color from your dat file (red!20), or do you prefer it as you have it? Also not all these data needed in dat file.... just the color could be enough.
    – koleygr
    3 hours ago







  • 1




    The data is a matrix of integers and I will map these integers z_i to an intensity of red with red!z_i for each z_i! Thanks.
    – PatrickT
    3 hours ago










  • Ok... but it could be a matrix of integers with just the color value like z_i but without 1/1/z_1 1/2/z_2 etc... Would this be accepted or you prefer your own dat file format?
    – koleygr
    3 hours ago










  • It's a matrix of integers like 2, 8, 2\ 8, 10, 8\ 2, 8, 2.
    – PatrickT
    3 hours ago






  • 1




    Thanks, got it.
    – koleygr
    3 hours ago















Do you want to read the actual color from your dat file (red!20), or do you prefer it as you have it? Also not all these data needed in dat file.... just the color could be enough.
– koleygr
3 hours ago





Do you want to read the actual color from your dat file (red!20), or do you prefer it as you have it? Also not all these data needed in dat file.... just the color could be enough.
– koleygr
3 hours ago





1




1




The data is a matrix of integers and I will map these integers z_i to an intensity of red with red!z_i for each z_i! Thanks.
– PatrickT
3 hours ago




The data is a matrix of integers and I will map these integers z_i to an intensity of red with red!z_i for each z_i! Thanks.
– PatrickT
3 hours ago












Ok... but it could be a matrix of integers with just the color value like z_i but without 1/1/z_1 1/2/z_2 etc... Would this be accepted or you prefer your own dat file format?
– koleygr
3 hours ago




Ok... but it could be a matrix of integers with just the color value like z_i but without 1/1/z_1 1/2/z_2 etc... Would this be accepted or you prefer your own dat file format?
– koleygr
3 hours ago












It's a matrix of integers like 2, 8, 2\ 8, 10, 8\ 2, 8, 2.
– PatrickT
3 hours ago




It's a matrix of integers like 2, 8, 2\ 8, 10, 8\ 2, 8, 2.
– PatrickT
3 hours ago




1




1




Thanks, got it.
– koleygr
3 hours ago




Thanks, got it.
– koleygr
3 hours ago










2 Answers
2






active

oldest

votes

















up vote
4
down vote













Here is a way if I finally got it. If this is the case and you need modifications about different size of matrix let me know:



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2,
8, 10, 8,
2, 8, 2
endfilecontents*

begindocument
defMySpaceLength3
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-3*int(i/3)+i
pgfmathsetmacromyyint(i/3)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


enter image description here



Edit (fixed code to accept parameters for matrix dimensions:):



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2, 5,
8, 10, 8, 90,
2, 8, 2,60,
20, 2 , 56, 40,
30, 20, 50, 70
endfilecontents*

begindocument
defMySpaceLength3
defXMatrixDimension4
defYMatrixDimension5
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-XMatrixDimension*int(i/XMatrixDimension)+i
pgfmathsetmacromyyint(i/XMatrixDimension)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


Output:



enter image description here






share|improve this answer






















  • Thanks for your answer koleygr. I think I understand the steps. I'll play around with this a little and get back to you if I get stumped. Perhaps I should have considered making a CSV of my data (obtained with R) in the 1/2/3 format. Might have been easier. That said, it's good to learn how to do this with LaTeX! Thanks.
    – PatrickT
    10 mins ago

















up vote
1
down vote













Here is a solution based on matrix plot that comes with pgfplots. It is taken from this answer, whose second part has some similarity with koleygr's answer. The advantage of the pgfplots solution is that you can use color maps for that. Of course, you can drop the colorbar.



documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
usetikzlibrarypgfplots.colormaps
pgfplotssetcompat=1.16
usepackagepgfplotstable
usepackagefilecontents

beginfilecontents*jobname.csv
2, 8, 2
8, 10, 8
2, 8, 2
endfilecontents*
newcommand*ReadOutElement[4]%
pgfplotstablegetelem#2[index]#3of#1%
let#4pgfplotsretval

begindocument
pgfplotstableread[header=false,col sep=comma]jobname.csvdatatable
pgfplotstablegetrowsofdatatable
pgfmathtruncatemacronumrowspgfplotsretval
pgfplotstablegetcolsofdatatable
pgfmathtruncatemacronumcolspgfplotsretval
xdefLstX
xdefLstY
xdefLstC
foreach Y [evaluate=Y as PrevY using int(Y-1),count=nY] in 1,...,numrows
pgfmathtruncatemacronewYnumrows-Y
foreach X [evaluate=X as PrevX using int(X-1),count=nX] in 1,...,numcols

ReadOutElementdatatablePrevYPrevXCurrent
pgfmathtruncatemacronZnX+nY
ifnumnZ=2
xdefLstXPrevX
xdefLstYPrevY
xdefLstCCurrent
else
xdefLstXLstX,PrevX
xdefLstYLstY,PrevY
xdefLstCLstC,Current
fi


edeftempnoexpandpgfplotstableset
create on use/x/.style=create col/set list=LstX,
create on use/y/.style=create col/set list=LstY,
create on use/color/.style=create col/set list=LstC,
temp
pgfmathtruncatemacrostrangenumnumrows*numcols
pgfplotstablenew[columns=x,y,color]strangenumstrangetable

%pgfplotstabletypeset[empty cells with=---]strangetable
begintikzpicture
pgfplotsset%
colormap=WhiteRedBlack%
rgb255=(255,255,255)
rgb255=(255,0,0)
rgb255=(0,0,0)
,

beginaxis[%
small,
every tick label/.append style=font=tiny,
tick align=outside,
minor tick num=5,
%
xlabel=$beta$,
xticklabel pos=right,
xlabel near ticks,
xmin=-1, xmax=numcols,
xtick=0, 5, ..., 4,
%
ylabel=$alpha$,
ylabel style=rotate=-90,
ymin=-1, ymax=numrows,
ytick=0, 5, ..., 4,
%
point meta min=0,
point meta max=32,
point meta=explicit,
%
%colorbar sampled,
colorbar as palette,
colorbar style=samples=3,
colormap name=WhiteRedBlack,
scale mode=scale uniformly,
]
draw (axis description cs:0,0) -- (axis description cs:1,0);
addplot [
matrix plot,
%mesh/cols=4,
point meta=explicit,
] table [meta=color,col sep=comma] strangetable;
endaxis
endtikzpicture
enddocument


enter image description here






share|improve this answer




















  • Thanks for your answer marmot. I'm going to need to play around with this a little to digest it. I've started reading the links, very useful. Quite frankly, I did not expect that changing the format of the data would induce so much extra work. Thanks!
    – PatrickT
    14 mins ago






  • 1




    @PatrickT It could very well be that I missed a simple possibility... In case you find out, please ping me. ;-)
    – marmot
    11 mins ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f450832%2ftikz-colors-stored-in-matrix-with-coordinates-inferred%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













Here is a way if I finally got it. If this is the case and you need modifications about different size of matrix let me know:



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2,
8, 10, 8,
2, 8, 2
endfilecontents*

begindocument
defMySpaceLength3
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-3*int(i/3)+i
pgfmathsetmacromyyint(i/3)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


enter image description here



Edit (fixed code to accept parameters for matrix dimensions:):



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2, 5,
8, 10, 8, 90,
2, 8, 2,60,
20, 2 , 56, 40,
30, 20, 50, 70
endfilecontents*

begindocument
defMySpaceLength3
defXMatrixDimension4
defYMatrixDimension5
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-XMatrixDimension*int(i/XMatrixDimension)+i
pgfmathsetmacromyyint(i/XMatrixDimension)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


Output:



enter image description here






share|improve this answer






















  • Thanks for your answer koleygr. I think I understand the steps. I'll play around with this a little and get back to you if I get stumped. Perhaps I should have considered making a CSV of my data (obtained with R) in the 1/2/3 format. Might have been easier. That said, it's good to learn how to do this with LaTeX! Thanks.
    – PatrickT
    10 mins ago














up vote
4
down vote













Here is a way if I finally got it. If this is the case and you need modifications about different size of matrix let me know:



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2,
8, 10, 8,
2, 8, 2
endfilecontents*

begindocument
defMySpaceLength3
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-3*int(i/3)+i
pgfmathsetmacromyyint(i/3)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


enter image description here



Edit (fixed code to accept parameters for matrix dimensions:):



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2, 5,
8, 10, 8, 90,
2, 8, 2,60,
20, 2 , 56, 40,
30, 20, 50, 70
endfilecontents*

begindocument
defMySpaceLength3
defXMatrixDimension4
defYMatrixDimension5
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-XMatrixDimension*int(i/XMatrixDimension)+i
pgfmathsetmacromyyint(i/XMatrixDimension)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


Output:



enter image description here






share|improve this answer






















  • Thanks for your answer koleygr. I think I understand the steps. I'll play around with this a little and get back to you if I get stumped. Perhaps I should have considered making a CSV of my data (obtained with R) in the 1/2/3 format. Might have been easier. That said, it's good to learn how to do this with LaTeX! Thanks.
    – PatrickT
    10 mins ago












up vote
4
down vote










up vote
4
down vote









Here is a way if I finally got it. If this is the case and you need modifications about different size of matrix let me know:



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2,
8, 10, 8,
2, 8, 2
endfilecontents*

begindocument
defMySpaceLength3
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-3*int(i/3)+i
pgfmathsetmacromyyint(i/3)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


enter image description here



Edit (fixed code to accept parameters for matrix dimensions:):



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2, 5,
8, 10, 8, 90,
2, 8, 2,60,
20, 2 , 56, 40,
30, 20, 50, 70
endfilecontents*

begindocument
defMySpaceLength3
defXMatrixDimension4
defYMatrixDimension5
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-XMatrixDimension*int(i/XMatrixDimension)+i
pgfmathsetmacromyyint(i/XMatrixDimension)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


Output:



enter image description here






share|improve this answer














Here is a way if I finally got it. If this is the case and you need modifications about different size of matrix let me know:



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2,
8, 10, 8,
2, 8, 2
endfilecontents*

begindocument
defMySpaceLength3
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-3*int(i/3)+i
pgfmathsetmacromyyint(i/3)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


enter image description here



Edit (fixed code to accept parameters for matrix dimensions:):



documentclassstandalone
usepackagetikz
usetikzlibrarycalc
usepackagexcolor
usepackagecatchfile
usepackagefilecontents

newcommandloaddata[1]CatchFileDefloadeddata#1endlinechar=-1

beginfilecontents*jobname.csv
2, 8, 2, 5,
8, 10, 8, 90,
2, 8, 2,60,
20, 2 , 56, 40,
30, 20, 50, 70
endfilecontents*

begindocument
defMySpaceLength3
defXMatrixDimension4
defYMatrixDimension5
begintikzpicture
loaddatajobname.csv
foreach k[count=i from 0] in loadeddata
pgfmathsetmacromyx-XMatrixDimension*int(i/XMatrixDimension)+i
pgfmathsetmacromyyint(i/XMatrixDimension)
fill[color=red!k] (MySpaceLength*myx,-MySpaceLength*myy) rectangle+ ($(MySpaceLength,MySpaceLength)$);

endtikzpicture
enddocument


Output:



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 3 hours ago









koleygr

9,5101634




9,5101634











  • Thanks for your answer koleygr. I think I understand the steps. I'll play around with this a little and get back to you if I get stumped. Perhaps I should have considered making a CSV of my data (obtained with R) in the 1/2/3 format. Might have been easier. That said, it's good to learn how to do this with LaTeX! Thanks.
    – PatrickT
    10 mins ago
















  • Thanks for your answer koleygr. I think I understand the steps. I'll play around with this a little and get back to you if I get stumped. Perhaps I should have considered making a CSV of my data (obtained with R) in the 1/2/3 format. Might have been easier. That said, it's good to learn how to do this with LaTeX! Thanks.
    – PatrickT
    10 mins ago















Thanks for your answer koleygr. I think I understand the steps. I'll play around with this a little and get back to you if I get stumped. Perhaps I should have considered making a CSV of my data (obtained with R) in the 1/2/3 format. Might have been easier. That said, it's good to learn how to do this with LaTeX! Thanks.
– PatrickT
10 mins ago




Thanks for your answer koleygr. I think I understand the steps. I'll play around with this a little and get back to you if I get stumped. Perhaps I should have considered making a CSV of my data (obtained with R) in the 1/2/3 format. Might have been easier. That said, it's good to learn how to do this with LaTeX! Thanks.
– PatrickT
10 mins ago










up vote
1
down vote













Here is a solution based on matrix plot that comes with pgfplots. It is taken from this answer, whose second part has some similarity with koleygr's answer. The advantage of the pgfplots solution is that you can use color maps for that. Of course, you can drop the colorbar.



documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
usetikzlibrarypgfplots.colormaps
pgfplotssetcompat=1.16
usepackagepgfplotstable
usepackagefilecontents

beginfilecontents*jobname.csv
2, 8, 2
8, 10, 8
2, 8, 2
endfilecontents*
newcommand*ReadOutElement[4]%
pgfplotstablegetelem#2[index]#3of#1%
let#4pgfplotsretval

begindocument
pgfplotstableread[header=false,col sep=comma]jobname.csvdatatable
pgfplotstablegetrowsofdatatable
pgfmathtruncatemacronumrowspgfplotsretval
pgfplotstablegetcolsofdatatable
pgfmathtruncatemacronumcolspgfplotsretval
xdefLstX
xdefLstY
xdefLstC
foreach Y [evaluate=Y as PrevY using int(Y-1),count=nY] in 1,...,numrows
pgfmathtruncatemacronewYnumrows-Y
foreach X [evaluate=X as PrevX using int(X-1),count=nX] in 1,...,numcols

ReadOutElementdatatablePrevYPrevXCurrent
pgfmathtruncatemacronZnX+nY
ifnumnZ=2
xdefLstXPrevX
xdefLstYPrevY
xdefLstCCurrent
else
xdefLstXLstX,PrevX
xdefLstYLstY,PrevY
xdefLstCLstC,Current
fi


edeftempnoexpandpgfplotstableset
create on use/x/.style=create col/set list=LstX,
create on use/y/.style=create col/set list=LstY,
create on use/color/.style=create col/set list=LstC,
temp
pgfmathtruncatemacrostrangenumnumrows*numcols
pgfplotstablenew[columns=x,y,color]strangenumstrangetable

%pgfplotstabletypeset[empty cells with=---]strangetable
begintikzpicture
pgfplotsset%
colormap=WhiteRedBlack%
rgb255=(255,255,255)
rgb255=(255,0,0)
rgb255=(0,0,0)
,

beginaxis[%
small,
every tick label/.append style=font=tiny,
tick align=outside,
minor tick num=5,
%
xlabel=$beta$,
xticklabel pos=right,
xlabel near ticks,
xmin=-1, xmax=numcols,
xtick=0, 5, ..., 4,
%
ylabel=$alpha$,
ylabel style=rotate=-90,
ymin=-1, ymax=numrows,
ytick=0, 5, ..., 4,
%
point meta min=0,
point meta max=32,
point meta=explicit,
%
%colorbar sampled,
colorbar as palette,
colorbar style=samples=3,
colormap name=WhiteRedBlack,
scale mode=scale uniformly,
]
draw (axis description cs:0,0) -- (axis description cs:1,0);
addplot [
matrix plot,
%mesh/cols=4,
point meta=explicit,
] table [meta=color,col sep=comma] strangetable;
endaxis
endtikzpicture
enddocument


enter image description here






share|improve this answer




















  • Thanks for your answer marmot. I'm going to need to play around with this a little to digest it. I've started reading the links, very useful. Quite frankly, I did not expect that changing the format of the data would induce so much extra work. Thanks!
    – PatrickT
    14 mins ago






  • 1




    @PatrickT It could very well be that I missed a simple possibility... In case you find out, please ping me. ;-)
    – marmot
    11 mins ago














up vote
1
down vote













Here is a solution based on matrix plot that comes with pgfplots. It is taken from this answer, whose second part has some similarity with koleygr's answer. The advantage of the pgfplots solution is that you can use color maps for that. Of course, you can drop the colorbar.



documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
usetikzlibrarypgfplots.colormaps
pgfplotssetcompat=1.16
usepackagepgfplotstable
usepackagefilecontents

beginfilecontents*jobname.csv
2, 8, 2
8, 10, 8
2, 8, 2
endfilecontents*
newcommand*ReadOutElement[4]%
pgfplotstablegetelem#2[index]#3of#1%
let#4pgfplotsretval

begindocument
pgfplotstableread[header=false,col sep=comma]jobname.csvdatatable
pgfplotstablegetrowsofdatatable
pgfmathtruncatemacronumrowspgfplotsretval
pgfplotstablegetcolsofdatatable
pgfmathtruncatemacronumcolspgfplotsretval
xdefLstX
xdefLstY
xdefLstC
foreach Y [evaluate=Y as PrevY using int(Y-1),count=nY] in 1,...,numrows
pgfmathtruncatemacronewYnumrows-Y
foreach X [evaluate=X as PrevX using int(X-1),count=nX] in 1,...,numcols

ReadOutElementdatatablePrevYPrevXCurrent
pgfmathtruncatemacronZnX+nY
ifnumnZ=2
xdefLstXPrevX
xdefLstYPrevY
xdefLstCCurrent
else
xdefLstXLstX,PrevX
xdefLstYLstY,PrevY
xdefLstCLstC,Current
fi


edeftempnoexpandpgfplotstableset
create on use/x/.style=create col/set list=LstX,
create on use/y/.style=create col/set list=LstY,
create on use/color/.style=create col/set list=LstC,
temp
pgfmathtruncatemacrostrangenumnumrows*numcols
pgfplotstablenew[columns=x,y,color]strangenumstrangetable

%pgfplotstabletypeset[empty cells with=---]strangetable
begintikzpicture
pgfplotsset%
colormap=WhiteRedBlack%
rgb255=(255,255,255)
rgb255=(255,0,0)
rgb255=(0,0,0)
,

beginaxis[%
small,
every tick label/.append style=font=tiny,
tick align=outside,
minor tick num=5,
%
xlabel=$beta$,
xticklabel pos=right,
xlabel near ticks,
xmin=-1, xmax=numcols,
xtick=0, 5, ..., 4,
%
ylabel=$alpha$,
ylabel style=rotate=-90,
ymin=-1, ymax=numrows,
ytick=0, 5, ..., 4,
%
point meta min=0,
point meta max=32,
point meta=explicit,
%
%colorbar sampled,
colorbar as palette,
colorbar style=samples=3,
colormap name=WhiteRedBlack,
scale mode=scale uniformly,
]
draw (axis description cs:0,0) -- (axis description cs:1,0);
addplot [
matrix plot,
%mesh/cols=4,
point meta=explicit,
] table [meta=color,col sep=comma] strangetable;
endaxis
endtikzpicture
enddocument


enter image description here






share|improve this answer




















  • Thanks for your answer marmot. I'm going to need to play around with this a little to digest it. I've started reading the links, very useful. Quite frankly, I did not expect that changing the format of the data would induce so much extra work. Thanks!
    – PatrickT
    14 mins ago






  • 1




    @PatrickT It could very well be that I missed a simple possibility... In case you find out, please ping me. ;-)
    – marmot
    11 mins ago












up vote
1
down vote










up vote
1
down vote









Here is a solution based on matrix plot that comes with pgfplots. It is taken from this answer, whose second part has some similarity with koleygr's answer. The advantage of the pgfplots solution is that you can use color maps for that. Of course, you can drop the colorbar.



documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
usetikzlibrarypgfplots.colormaps
pgfplotssetcompat=1.16
usepackagepgfplotstable
usepackagefilecontents

beginfilecontents*jobname.csv
2, 8, 2
8, 10, 8
2, 8, 2
endfilecontents*
newcommand*ReadOutElement[4]%
pgfplotstablegetelem#2[index]#3of#1%
let#4pgfplotsretval

begindocument
pgfplotstableread[header=false,col sep=comma]jobname.csvdatatable
pgfplotstablegetrowsofdatatable
pgfmathtruncatemacronumrowspgfplotsretval
pgfplotstablegetcolsofdatatable
pgfmathtruncatemacronumcolspgfplotsretval
xdefLstX
xdefLstY
xdefLstC
foreach Y [evaluate=Y as PrevY using int(Y-1),count=nY] in 1,...,numrows
pgfmathtruncatemacronewYnumrows-Y
foreach X [evaluate=X as PrevX using int(X-1),count=nX] in 1,...,numcols

ReadOutElementdatatablePrevYPrevXCurrent
pgfmathtruncatemacronZnX+nY
ifnumnZ=2
xdefLstXPrevX
xdefLstYPrevY
xdefLstCCurrent
else
xdefLstXLstX,PrevX
xdefLstYLstY,PrevY
xdefLstCLstC,Current
fi


edeftempnoexpandpgfplotstableset
create on use/x/.style=create col/set list=LstX,
create on use/y/.style=create col/set list=LstY,
create on use/color/.style=create col/set list=LstC,
temp
pgfmathtruncatemacrostrangenumnumrows*numcols
pgfplotstablenew[columns=x,y,color]strangenumstrangetable

%pgfplotstabletypeset[empty cells with=---]strangetable
begintikzpicture
pgfplotsset%
colormap=WhiteRedBlack%
rgb255=(255,255,255)
rgb255=(255,0,0)
rgb255=(0,0,0)
,

beginaxis[%
small,
every tick label/.append style=font=tiny,
tick align=outside,
minor tick num=5,
%
xlabel=$beta$,
xticklabel pos=right,
xlabel near ticks,
xmin=-1, xmax=numcols,
xtick=0, 5, ..., 4,
%
ylabel=$alpha$,
ylabel style=rotate=-90,
ymin=-1, ymax=numrows,
ytick=0, 5, ..., 4,
%
point meta min=0,
point meta max=32,
point meta=explicit,
%
%colorbar sampled,
colorbar as palette,
colorbar style=samples=3,
colormap name=WhiteRedBlack,
scale mode=scale uniformly,
]
draw (axis description cs:0,0) -- (axis description cs:1,0);
addplot [
matrix plot,
%mesh/cols=4,
point meta=explicit,
] table [meta=color,col sep=comma] strangetable;
endaxis
endtikzpicture
enddocument


enter image description here






share|improve this answer












Here is a solution based on matrix plot that comes with pgfplots. It is taken from this answer, whose second part has some similarity with koleygr's answer. The advantage of the pgfplots solution is that you can use color maps for that. Of course, you can drop the colorbar.



documentclass[border=3.14mm,tikz]standalone
usepackagepgfplots
usetikzlibrarypgfplots.colormaps
pgfplotssetcompat=1.16
usepackagepgfplotstable
usepackagefilecontents

beginfilecontents*jobname.csv
2, 8, 2
8, 10, 8
2, 8, 2
endfilecontents*
newcommand*ReadOutElement[4]%
pgfplotstablegetelem#2[index]#3of#1%
let#4pgfplotsretval

begindocument
pgfplotstableread[header=false,col sep=comma]jobname.csvdatatable
pgfplotstablegetrowsofdatatable
pgfmathtruncatemacronumrowspgfplotsretval
pgfplotstablegetcolsofdatatable
pgfmathtruncatemacronumcolspgfplotsretval
xdefLstX
xdefLstY
xdefLstC
foreach Y [evaluate=Y as PrevY using int(Y-1),count=nY] in 1,...,numrows
pgfmathtruncatemacronewYnumrows-Y
foreach X [evaluate=X as PrevX using int(X-1),count=nX] in 1,...,numcols

ReadOutElementdatatablePrevYPrevXCurrent
pgfmathtruncatemacronZnX+nY
ifnumnZ=2
xdefLstXPrevX
xdefLstYPrevY
xdefLstCCurrent
else
xdefLstXLstX,PrevX
xdefLstYLstY,PrevY
xdefLstCLstC,Current
fi


edeftempnoexpandpgfplotstableset
create on use/x/.style=create col/set list=LstX,
create on use/y/.style=create col/set list=LstY,
create on use/color/.style=create col/set list=LstC,
temp
pgfmathtruncatemacrostrangenumnumrows*numcols
pgfplotstablenew[columns=x,y,color]strangenumstrangetable

%pgfplotstabletypeset[empty cells with=---]strangetable
begintikzpicture
pgfplotsset%
colormap=WhiteRedBlack%
rgb255=(255,255,255)
rgb255=(255,0,0)
rgb255=(0,0,0)
,

beginaxis[%
small,
every tick label/.append style=font=tiny,
tick align=outside,
minor tick num=5,
%
xlabel=$beta$,
xticklabel pos=right,
xlabel near ticks,
xmin=-1, xmax=numcols,
xtick=0, 5, ..., 4,
%
ylabel=$alpha$,
ylabel style=rotate=-90,
ymin=-1, ymax=numrows,
ytick=0, 5, ..., 4,
%
point meta min=0,
point meta max=32,
point meta=explicit,
%
%colorbar sampled,
colorbar as palette,
colorbar style=samples=3,
colormap name=WhiteRedBlack,
scale mode=scale uniformly,
]
draw (axis description cs:0,0) -- (axis description cs:1,0);
addplot [
matrix plot,
%mesh/cols=4,
point meta=explicit,
] table [meta=color,col sep=comma] strangetable;
endaxis
endtikzpicture
enddocument


enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered 34 mins ago









marmot

57.3k462124




57.3k462124











  • Thanks for your answer marmot. I'm going to need to play around with this a little to digest it. I've started reading the links, very useful. Quite frankly, I did not expect that changing the format of the data would induce so much extra work. Thanks!
    – PatrickT
    14 mins ago






  • 1




    @PatrickT It could very well be that I missed a simple possibility... In case you find out, please ping me. ;-)
    – marmot
    11 mins ago
















  • Thanks for your answer marmot. I'm going to need to play around with this a little to digest it. I've started reading the links, very useful. Quite frankly, I did not expect that changing the format of the data would induce so much extra work. Thanks!
    – PatrickT
    14 mins ago






  • 1




    @PatrickT It could very well be that I missed a simple possibility... In case you find out, please ping me. ;-)
    – marmot
    11 mins ago















Thanks for your answer marmot. I'm going to need to play around with this a little to digest it. I've started reading the links, very useful. Quite frankly, I did not expect that changing the format of the data would induce so much extra work. Thanks!
– PatrickT
14 mins ago




Thanks for your answer marmot. I'm going to need to play around with this a little to digest it. I've started reading the links, very useful. Quite frankly, I did not expect that changing the format of the data would induce so much extra work. Thanks!
– PatrickT
14 mins ago




1




1




@PatrickT It could very well be that I missed a simple possibility... In case you find out, please ping me. ;-)
– marmot
11 mins ago




@PatrickT It could very well be that I missed a simple possibility... In case you find out, please ping me. ;-)
– marmot
11 mins ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f450832%2ftikz-colors-stored-in-matrix-with-coordinates-inferred%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery