帮助在 tikz 中分割矩形

帮助在 tikz 中分割矩形

我想画一些类似下图的东西。我尝试了 split tikz 库,并尝试分割一个矩形,但没有绘制 32 个部分,它只显示一些部分。

关于如何在乳胶中绘制图片,有什么帮助吗?

谢谢。图片

答案1

这是一个简单的例子,使用\pic矩形和编号颜色来使代码简洁。

\documentclass[tikz,border=2mm]{standalone}

% colors
\definecolor{color0}{HTML}{999999} % gray
\definecolor{color1}{HTML}{FF0000} % red
\definecolor{color2}{HTML}{0000FF} % blue
\definecolor{color3}{HTML}{00FF00} % green
\definecolor{color4}{HTML}{FF9999} % light red
 
\tikzset
{
  pics/my rectangle/.style={% #1 -> list of numbers (colors)
    code={%
      \foreach[count=\j]\i in {#1}
        \fill[color\i] (0.2*\j,0) rectangle (0.2*\j+0.18,0.5); 
      \coordinate (-label) at (3.39,-0.3);
      }},
}

\begin{document}
\begin{tikzpicture}
  \pic  (A) at (0,0) {my rectangle={0,0,1,1,1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
  \node at (A-label) {$0-31$};
  \pic  (B) at (8,0) {my rectangle={0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
  \node at (B-label) {$32-63$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

或者你可以尝试元帖子

在此处输入图片描述

这被包裹在内,luamplib因此用 进行编译lualatex

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
input colorbrewer-rgb
color key[];
key0 = 5/8 white;
key1 = Reds 8 6;
key2 = Oranges 8 6;
key3 = Greens 8 6;
key4 = Blues 8 6;

beginfig(1);
numeric x, y, h; x = y = h = 0;
forsuffixes $=0,0,2,2,2,4,4,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
              0,0,0,0,0,0,0,3, 0,0,0,0,0,0,0,1, 2,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
              0,0,1,1,1,1,1,1, 1,2,2,0,0,1,1,1, 1,1,1,1,1,1,1,1, 2,0,0,2,2,4,0,2,
              0,0,0,0,0,0,1,1, 1,1,1,1,2,2,4,4, 0,0,0,3,0,0,0,0, 0,0,0,1,1,1,1,1,
              0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,2,2,2,2,2,
              0,0,0,0,0,0,0,0, 2,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,2,
              1,2,4,0,0,0,0,1, 2,3,0,0,0,0,0,1, 2,0,0,2,4,4,0,0, 0,0,0,1,1,1,1,1,
              2,1,2,4,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,1,2,2,0, 0,0,0,0,0,0,0,1:

    fill unitsquare xscaled 3 yscaled 16 shifted (4x + 32h, -42y) withcolor key$;
    x := x + 1;
    if x = 32:
        h := 1;
    elseif x = 64:
        x := 0; h := 0; y := y + 1;
    fi
    if x = 16:
        label.bot(decimal 64y & "–" & decimal (64y + 31), (4x, -42y));
    elseif x = 48:
        label.bot(decimal (64y + 32) & "–" & decimal (64y + 63), (4x + 32, -42y));
    fi
endfor

interim bboxmargin := 16;
draw bbox currentpicture withpen pencircle scaled 1;

endfig;
\end{mplibcode}
\end{document}

还有一个传奇...

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
input colorbrewer-rgb
color key[];
key0 = 5/8 white;
key1 = Reds 8 6;
key2 = Oranges 8 6;
key3 = Greens 8 6;
key4 = Blues 8 6;

string desc[];
desc0 = "Grey $\pi$"; % thanks to the "textextlabel" option above this is processed through TEX()
desc1 = "Red";
desc2 = "Orange";
desc3 = "Green";
desc4 = "Blue";

beginfig(1);
numeric x, y, h; x = y = h = 0;
forsuffixes $=0,0,2,2,2,4,4,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
              0,0,0,0,0,0,0,3, 0,0,0,0,0,0,0,1, 2,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
              0,0,1,1,1,1,1,1, 1,2,2,0,0,1,1,1, 1,1,1,1,1,1,1,1, 2,0,0,2,2,4,0,2,
              0,0,0,0,0,0,1,1, 1,1,1,1,2,2,4,4, 0,0,0,3,0,0,0,0, 0,0,0,1,1,1,1,1,
              0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,2,2,2,2,2,
              0,0,0,0,0,0,0,0, 2,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,2,
              1,2,4,0,0,0,0,1, 2,3,0,0,0,0,0,1, 2,0,0,2,4,4,0,0, 0,0,0,1,1,1,1,1,
              2,1,2,4,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,1,2,2,0, 0,0,0,0,0,0,0,1:

    fill unitsquare xscaled 3 yscaled 16 shifted (4x + 32h, -42y) withcolor key$;
    x := x + 1;
    if x = 32:
        h := 1;
    elseif x = 64:
        x := 0; h := 0; y := y + 1;
    fi
    if x = 16:
        label.bot(decimal 64y & "–" & decimal (64y + 31), (4x, -42y));
    elseif x = 48:
        label.bot(decimal (64y + 32) & "–" & decimal (64y + 63), (4x + 32, -42y));
    fi
endfor

%
% make a legend
picture legend; legend = image(
    for i = 0 upto 4:
        fill unitsquare xscaled -3 yscaled 12 shifted (50i, -6) withcolor key[i];
        label.rt(desc[i], (50i, 0));
    endfor
);
label.bot(legend, point 1/2 of bbox currentpicture shifted 13 down);

interim bboxmargin := 16;
draw bbox currentpicture withpen pencircle scaled 1;

endfig;
\end{mplibcode}
\end{document}

答案3

matrix一种具有和.list覆盖内腔细胞的替代结构。

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{matrix, positioning, fit}

\colorlet{color0}{gray}
\colorlet{color1}{red}
\colorlet{color2}{blue}
\colorlet{color3}{green}
\colorlet{color4}{pink}


\begin{document}
\begin{tikzpicture}[
    mymatrix/.style={matrix of nodes, nodes in empty cells, inner sep=0mm,
        nodes={fill=color0, anchor=center, minimum width=2mm, 
                minimum height=5mm, inner sep=0pt, outer sep=0pt},
        column sep=.25mm,
        },
    col1/.style={column #1/.style={nodes={fill=color1}}},      
    col2/.style={column #1/.style={nodes={fill=color2}}},        
    col3/.style={column #1/.style={nodes={fill=color3}}},      
    col4/.style={column #1/.style={nodes={fill=color4}}},        
    ]
    
\matrix[mymatrix, col4/.list={3,4,5}, col2/.list={6,7}, label=below:0-31] (A1)
    {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\\};
\matrix[mymatrix, col3/.list={8}, col1/.list={16}, col4/.list={17}, label=below:32-63, right=1cm of A1] (A2)
    {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\\};
\matrix[mymatrix, col1/.list={2,3,4,5,6,10,11,12,13,14}, col2/.list={7,8,25,16,23}, color3/.list={15,29,32}, label=below:64-95, below=1cm of A1] (A3)
    {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\\};
\matrix[mymatrix, col1/.list={1,8,16,24,32}, col2/.list={5,12,16,20,28}, label=below:96-127, below=1cm of A2] (A4)
    {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\\};
\node[fit=(A1) (A4), inner sep=5mm, draw, line width=1mm] {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容