我使用 Python 制作了下面显示的图(保存为 png)。
它应该有一个通用的颜色条,并且所有数据都是从h5
文件中读取的,但我认为将它保存到 txt 文件或使用 Python 脚本读取 TikZ 图形。
共有 5 个图和 1 个色条:
第一个图只有一个变量(值为 0、25、33、50、66、75%),每个彩色方块代表相应的输出,可以用长度为 6 的数组表示。
另外四个图有两个变量(值为 [0,25,33,50,66,75 %] 和 [0,1,2,3,4,5,6,7,8]),因此可以用 6x9 矩阵表示。
我想制作一个像 PNG 一样的 TikZ 图形,但当然带有不重叠的文本,也许每个子图都有一个标题以及更好的颜色条轮廓(png 很快且很脏,能够快速查看输出)。
有人可以帮我从这里出去吗?
(如果这些值有用,请告诉我最简单的格式,我会txt
尽快上传文件。)
编辑:添加数据data.txt
编辑2:我忘了说我希望能够使用文件中定义的 \figureheight 和 \figurewidthtex
来调整尺寸(就像我在图中所做的那样)另一个问题)。
答案1
因为我对自己能取得的成绩不满意pgf图,我尝试自己写一些东西。基本上它是一个 foreach 循环,它计算特定值的颜色,然后绘制一个正方形。有一些障碍,因为我了解到你需要\xglobal
将颜色定义全局化。结果仍然远非自动化,但我正在考虑通过添加一个键值接口鍵盤。无论如何,以下是我目前所拥有的:
代码
\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{xifthen}
\begin{document}
\newcommand{\mycolorbar}[6]% height,width,colors,label min,label max,label step
{ \foreach \x [count=\c] in {#3}{ \xdef\numcolo{\c}}
\pgfmathsetmacro{\pieceheight}{#1/(\numcolo-1)}
\xdef\lowcolo{}
\foreach \x [count=\c] in {#3}
{ \ifthenelse{\c = 1}
{}
{ \fill[bottom color=\lowcolo,top color=\x] (0,{(\c-2)*\pieceheight}) rectangle (#2,{(\c-1)*\pieceheight});
}
\xdef\lowcolo{\x}
}
\draw[thick] (0,0) rectangle (#2,#1);
\pgfmathsetmacro{\secondlabel}{#4+#6}
\pgfmathsetmacro{\lastlabel}{#5+0.01}
\pgfkeys{/pgf/number format/.cd,fixed,precision=2}
\foreach \x in {#4,\secondlabel,...,\lastlabel}
{ \draw[thick] (#2,{(\x-#4)/(#5-#4)*#1}) -- ++ (0.15,0) node[right] {\pgfmathprintnumber{\x}};
}
}
\newcommand{\mycolor}[4]% z, min, max, colors
{ \foreach \x [count=\c] in {#4}
{ \xdef\numcolors{\c}
}
\foreach \x [count=\c] in {#4}
{ \ifthenelse{\c > 1}
{ \pgfmathsetmacro{\lowbound}{(\c-2)/(\numcolors-1)*(#3-#2)+#2}
\pgfmathsetmacro{\upbound}{(\c-1)/(\numcolors-1)*(#3-#2)+#2}
\pgfmathtruncatemacro{\thisinterval}{and(#1>=\lowbound,#1<\upbound)?1:0}
\ifthenelse{\thisinterval = 1}
{ \pgfmathtruncatemacro{\myperc}{(#1-\lowbound)/(\upbound-\lowbound)*100}
\pgfmathtruncatemacro{\myinvperc}{100-\myperc}
\xglobal\colorlet{myfillcolor}{rgb:\lowcolor,\myinvperc;\x,\myperc}
}
{}
\pgfmathtruncatemacro{\isbigger}{#1>#3?1:0}
\ifthenelse{\isbigger=1}
{ \xglobal\colorlet{myfillcolor}{\x}
}
{}
}
{ \pgfmathtruncatemacro{\issmaller}{#1<#2?1:0}
\ifthenelse{\issmaller=1}
{ \xglobal\colorlet{myfillcolor}{\x}
}
{}
}
\xdef\lowcolor{\x}
}
}
\begin{tikzpicture}[scale=0.4]
\foreach \z [count=\cz] in {4.21, 4.21, 4.21, 4.21, 4.21, 4.38, 4.38, 4.38, 4.38, 14.74, 14.74, 14.74, 14.74, 14.74, 15.11, 15.11, 16.51, 18.54, 23.02, 23.02, 23.02, 23.02, 23.02, 22.84, 22.84, 24.77, 26.93, 25.18, 25.18, 25.18, 25.18, 25.18, 25.03, 25.03, 25.27, 28.47, 37.71, 37.71, 37.71, 37.71, 37.71, 37.71, 38.51, 40.13, 39.68, 33.62, 33.62, 33.62, 33.62, 33.62, 33.62, 34.02, 34.02, 31.52}
{ \definecolor{myfillcolor}{rgb}{128,128,128}
\mycolor{\z}{0}{45}{blue!50!black, blue!50!gray, blue!50!cyan, green!50!cyan, lime, yellow!50!orange, orange, red, red!50!black}
\fill[myfillcolor] ({mod(\cz-1,9)},{div(\cz-1,9)}) rectangle ++(1,1) node[pos=0.5,black] {};
}
\draw[thick] (0,0) rectangle (9,6);
\foreach \x in {0,...,8}
{ \draw[thick] (\x+0.5,6) -- (\x+0.5,5.7);
\draw[thick] (\x+0.5,0.3) -- (\x+0.5,0) node[below] {\tiny\x};
}
\foreach \y [count=\c] in {0,25,33,50,66,75}
{ \draw[thick] (9,\c-0.5) -- (8.7,\c-0.5);
\draw[thick] (0.3,\c-0.5) -- (0,\c-0.5) node[left] {\tiny\y};
}
\node[below] at (4.5,-0.5) {first raster};
\begin{scope}[shift={(11,0)}]
\foreach \z [count=\cz] in {1,...,54}
{ \definecolor{myfillcolor}{rgb}{128,128,128}
\pgfmathsetmacro{\zr}{rnd*45}
\mycolor{\zr}{0}{45}{blue!50!black, blue!50!gray, blue!50!cyan, green!50!cyan, lime, yellow!50!orange, orange, red, red!50!black}
\fill[myfillcolor] ({mod(\cz-1,9)},{div(\cz-1,9)}) rectangle ++(1,1) node[pos=0.5,black] {};
}
\draw[thick] (0,0) rectangle (9,6);
\foreach \x in {0,...,8}
{ \draw[thick] (\x+0.5,6) -- (\x+0.5,5.7);
\draw[thick] (\x+0.5,0.3) -- (\x+0.5,0) node[below] {\tiny\x};
}
\foreach \y [count=\c] in {0,25,33,50,66,75}
{ \draw[thick] (9,\c-0.5) -- (8.7,\c-0.5);
\draw[thick] (0.3,\c-0.5) -- (0,\c-0.5) node[left] {\tiny\y};
}
\node[below] at (4.5,-0.5) {chaos};
\end{scope}
\begin{scope}[shift={(0,-8)}]
\foreach \z [count=\cz] in {1,...,54}
{ \definecolor{myfillcolor}{rgb}{128,128,128}
\pgfmathsetmacro{\zr}{(mod(\cz-1,9)+div(\cz-1,9))*3}
\mycolor{\zr}{0}{45}{blue!50!black, blue!50!gray, blue!50!cyan, green!50!cyan, lime, yellow!50!orange, orange, red, red!50!black}
\fill[myfillcolor] ({mod(\cz-1,9)},{div(\cz-1,9)}) rectangle ++(1,1) node[pos=0.5,black] {};
}
\draw[thick] (0,0) rectangle (9,6);
\foreach \x in {0,...,8}
{ \draw[thick] (\x+0.5,6) -- (\x+0.5,5.7);
\draw[thick] (\x+0.5,0.3) -- (\x+0.5,0) node[below] {\tiny\x};
}
\foreach \y [count=\c] in {0,25,33,50,66,75}
{ \draw[thick] (9,\c-0.5) -- (8.7,\c-0.5);
\draw[thick] (0.3,\c-0.5) -- (0,\c-0.5) node[left] {\tiny\y};
}
\node[below] at (4.5,-0.5) {gradient};
\end{scope}
\begin{scope}[shift={(11,-8)}]
\foreach \z [count=\cz] in {1,...,54}
{ \definecolor{myfillcolor}{rgb}{128,128,128}
\pgfmathsetmacro{\zr}{pow(mod(\cz-1,9),2)+pow(div(\cz-1,9),2)-11}
\mycolor{\zr}{0}{45}{blue!50!black, blue!50!gray, blue!50!cyan, green!50!cyan, lime, yellow!50!orange, orange, red, red!50!black}
\fill[myfillcolor] ({mod(\cz-1,9)},{div(\cz-1,9)}) rectangle ++(1,1) node[pos=0.5,black] {};
}
\draw[thick] (0,0) rectangle (9,6);
\foreach \x in {0,...,8}
{ \draw[thick] (\x+0.5,6) -- (\x+0.5,5.7);
\draw[thick] (\x+0.5,0.3) -- (\x+0.5,0) node[below] {\tiny\x};
}
\foreach \y [count=\c] in {0,25,33,50,66,75}
{ \draw[thick] (9,\c-0.5) -- (8.7,\c-0.5);
\draw[thick] (0.3,\c-0.5) -- (0,\c-0.5) node[left] {\tiny\y};
}
\node[below] at (4.5,-0.5) {floor \& roof};
\end{scope}
\begin{scope}[shift={(21,-8)}]
\mycolorbar{14}{1}{blue!50!black, blue!50!gray, blue!50!cyan, green!50!cyan, lime, yellow!50!orange, orange, red, red!50!black}{0}{45}{5}
\end{scope}
\end{tikzpicture}
\end{document}