TransWikia.com

pgfplots: Best way to draw a "patchwork" diagram

TeX - LaTeX Asked on December 20, 2020

Update / Remark

After I got a hint in the chat I also asked the question here (outside of https://tex.stackexchange.com/) and got an alternative approach.


  • This is a follow-up question of this question.
  • I want to draw a diagram as shown in the hand drawing.
  • The idea is to provide a table with x, y, color and a label for each rectangle.

Input

The input could look similar to the following. These are just generic proposals.

Version 1 (Absolute Values)

x    y    color    label
% First Column
10   5    green    Labelx1y1
10   15   green    Labelx1y2
10   20   green    Labelx1y3
% Second Column
18   5    green    Labelx2y1
18   15   green    Labelx2y2
18   20   green    Labelx2y3
% Third Column
25   5    green    Labelx3y1
25   15   green    Labelx3y2
25   20   red      Labelx3y3 % RED rectangle

Version 1 (Relative Values)

In this example, the x-values and y-values are added.

x    y    color    label
% First Column
10   5    green    Labelx1y1
10   10   green    Labelx1y2
10   5    green    Labelx1y3
% Second Column
8   5    green    Labelx2y1
8   10   green    Labelx2y2
8   5    green    Labelx2y3
% Third Column
7   5    green    Labelx3y1
7   10   green    Labelx3y2
7   5    red      Labelx3y3 % RED rectangle

Output

enter image description here

Main Question and Remarks

  • Question: Does this diagram type has a name?
  • Main Question: What would be a clever way to achieve this?
  • Remark: I would prefer to use pgfplots.
  • Remark: I am not married to a specific input format – I generate the data myself (Matlab, Python, Excel) and can therefore influence it.

One Answer

Here is a very basic option. Use pgfplotstable and plain TikZ to convert the data in a plot. Since it is very basic, it may be easy to customize. The prize that you have to pay is that you need to fix some things by hand in

x=3mm,y=3mm,declare function={nrows=3;}

which sets the units and the number of rows. (In all fairness, also in some pgfplots scenarios you have to set the number of rows or columns explicitly.)

documentclass[tikz,border=3mm]{standalone}
usepackage{pgfplotstable}
begin{document}
begin{tikzpicture}[x=3mm,y=3mm,declare function={nrows=3;}]
pgfplotstableread{%
x    y    color    label
% First Column
10   5    green    Labelx1y1
10   15   green    Labelx1y2
10   20   green    Labelx1y3
% Second Column
18   5    green    Labelx2y1
18   15   green    Labelx2y2
18   20   green    Labelx2y3
% Third Column
25   5    green    Labelx3y1
25   15   green    Labelx3y2
25   20   red      Labelx3y3
}dataA
pgfplotstablegetrowsof{dataA}%
pgfmathtruncatemacro{numrows}{pgfplotsretval-1}%
edeflastx{0}%
edeflasty{0}%
pgfplotsforeachungroupedX in{0,...,numrows} {%
pgfplotstablegetelem{X}{x}ofdataA
letxpgfplotsretval
pgfplotstablegetelem{X}{y}ofdataA
letypgfplotsretval
pgfplotstablegetelem{X}{color}ofdataA
letmycolorpgfplotsretval
pgfplotstablegetelem{X}{label}ofdataA
letmylabelpgfplotsretval
draw[fill=mycolor] (lastx,lasty) rectangle (x,y) node[midway]{mylabel};
letlastyy
pgfmathtruncatemacro{itest}{Mod(X+nrows+1,nrows)}%
ifnumitest=0relax
edeflasty{0}%
letlastxx
fi
}
end{tikzpicture}
end{document}

enter image description here

One can actually embed this into pgfplots. There is a small issue regarding the expansion, hence the edef and noexpand trickery (see the discussion on p. 545 of the pgfplots manual v1.17).

documentclass[tikz,border=3mm]{standalone}
usepackage{pgfplotstable}
usepackage{pgfplots}
pgfplotsset{compat=1.17}
begin{document}
begin{tikzpicture}[declare function={nrows=3;}]
begin{axis}[width=12cm,title=Random title]
pgfplotstableread{%
x    y    color    label
% First Column
10   5    green    Labelx1y1
10   15   green    Labelx1y2
10   20   green    Labelx1y3
% Second Column
18   5    green    Labelx2y1
18   15   green    Labelx2y2
18   20   green    Labelx2y3
% Third Column
25   5    green    Labelx3y1
25   15   green    Labelx3y2
25   20   red      Labelx3y3
}dataA
pgfplotstablegetrowsof{dataA}%
pgfmathtruncatemacro{numrows}{pgfplotsretval-1}%
edeflastx{0}%
edeflasty{0}%
pgfplotsforeachungroupedX in{0,...,numrows} {%
pgfplotstablegetelem{X}{x}ofdataA
letxpgfplotsretval
pgfplotstablegetelem{X}{y}ofdataA
letypgfplotsretval
pgfplotstablegetelem{X}{color}ofdataA
letmycolorpgfplotsretval
pgfplotstablegetelem{X}{label}ofdataA
letmylabelpgfplotsretval
edeftemp{noexpandaddplot[fill=mycolor] coordinates {(lastx,lasty) (lastx,y)
(x,y) (x,lasty) (lastx,lasty)};
noexpandpath (lastx,lasty) rectangle (x,y) node[midway]{mylabel};}
temp
letlastyy
pgfmathtruncatemacro{itest}{Mod(X+nrows+1,nrows)}%
ifnumitest=0relax
edeflasty{0}%
letlastxx
fi
}
end{axis}
end{tikzpicture}
end{document}

enter image description here

Correct answer by user228539 on December 20, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP