TikZ: colors stored in matrix with coordinates inferred
Clash 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:
tikz-pgf csv catchfile
add a comment |Â
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:
tikz-pgf csv catchfile
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 integersz_i
to an intensity of red withred!z_i
for eachz_i
! Thanks.
â PatrickT
3 hours ago
Ok... but it could be a matrix of integers with just the color value likez_i
but without1/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 like2, 8, 2\ 8, 10, 8\ 2, 8, 2
.
â PatrickT
3 hours ago
1
Thanks, got it.
â koleygr
3 hours ago
add a comment |Â
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:
tikz-pgf csv catchfile
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:
tikz-pgf csv catchfile
tikz-pgf csv catchfile
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 integersz_i
to an intensity of red withred!z_i
for eachz_i
! Thanks.
â PatrickT
3 hours ago
Ok... but it could be a matrix of integers with just the color value likez_i
but without1/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 like2, 8, 2\ 8, 10, 8\ 2, 8, 2
.
â PatrickT
3 hours ago
1
Thanks, got it.
â koleygr
3 hours ago
add a comment |Â
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 integersz_i
to an intensity of red withred!z_i
for eachz_i
! Thanks.
â PatrickT
3 hours ago
Ok... but it could be a matrix of integers with just the color value likez_i
but without1/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 like2, 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
add a comment |Â
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
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:
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 withR
) in the1/2/3
format. Might have been easier. That said, it's good to learn how to do this withLaTeX
! Thanks.
â PatrickT
10 mins ago
add a comment |Â
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
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
add a comment |Â
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
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:
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 withR
) in the1/2/3
format. Might have been easier. That said, it's good to learn how to do this withLaTeX
! Thanks.
â PatrickT
10 mins ago
add a comment |Â
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
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:
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 withR
) in the1/2/3
format. Might have been easier. That said, it's good to learn how to do this withLaTeX
! Thanks.
â PatrickT
10 mins ago
add a comment |Â
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
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:
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
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:
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 withR
) in the1/2/3
format. Might have been easier. That said, it's good to learn how to do this withLaTeX
! Thanks.
â PatrickT
10 mins ago
add a comment |Â
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 withR
) in the1/2/3
format. Might have been easier. That said, it's good to learn how to do this withLaTeX
! 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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 withred!z_i
for eachz_i
! Thanks.â PatrickT
3 hours ago
Ok... but it could be a matrix of integers with just the color value like
z_i
but without1/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