使用 tikz 合并两个图表并进行合理缩放

使用 tikz 合并两个图表并进行合理缩放

我有以下代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,calc}

\begin{document}
\usetikzlibrary{decorations.pathmorphing,calc}
\begin{tikzpicture}
% Define some reference points 
% The figure is drawn a bit bigger, and then clipped to the following dimensions:
\coordinate (clipping area) at (10, 7);
\clip (0,0) rectangle (clipping area);

% Next reference points are relative to the lower left corner of the clipping area
\coordinate (water level) at (0, 6);
\coordinate (bottom)      at (5, 1.3);     % (bottom of the pit)
\coordinate (ground1)     at (0, 5);       % (left shore)
\coordinate (ground2)     at (10, 5);      % (right shore)

% Coordinates of the bigger area really drawn
\coordinate (lower left)  at ([xshift=-5mm, yshift=-5mm] 0,0);
\coordinate (upper right) at ([xshift=5mm,  yshift=5mm] clipping area);

% Draw the water and ripples
\draw [draw=blue!80!black, decoration={bumps, mirror, segment length=6mm}, decorate,
     bottom color=cyan!60!black, top color=blue!20!white] 
  (lower left) rectangle (water level-|upper right);

% draw horizontal dashed line
\draw[dashed](0,4) -- (10,4);
\draw[dashed](0,3.6) -- (10,3.6);

% Draw the ground
\draw [draw=brown!30!black, fill=brown] 
  (lower left) -- (lower left|-ground1)  --
  (ground1) .. controls ($(ground1)!.3!(bottom)$) and (bottom-|ground1) ..
  (bottom) .. controls (bottom-|ground2) and ($(ground2)!.3!(bottom)$) .. 
  (ground2) -- (ground2-|upper right) -- (lower left-|upper right) -- cycle;

\end{tikzpicture}
\end{document}

生成下图左侧的图表:

在此处输入图片描述

我的下一个任务是生成右侧图表并缩放它,以便两个图上的水平线匹配(在第二张图中显示为矩形区域)。生成此图时我遇到的主要问题是第一个图表被裁剪,我无法找到缩放第二个图表的一致方法。所示的第二个图表是一个简单的示例。我希望插入的实际图表由以下代码给出:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,calc}

\begin{document}
\begin{tikzpicture}
[x = 2mm, y = 7mm,transition/.style = {rectangle, draw = black!50}] % define rectangular region
\draw [>=stealth, ->,thick](0,0) -- (0,-10); % draw xaxis for the diagram
\draw [>=stealth, ->,thick](0,0) -- (42,0); % draw yaxis
\foreach \x in {0,4,10,20} { \draw(\x,-2mm)--(\x,2mm) node[above]{\x};} % temperatures for graph
\draw[transition] (0,-6) rectangle (40,-2); % draw the rectangular region
\draw [thick] (0,-4) -- (41,-4); % define the thermocline
\draw[dashed] (4,0) -- (4,-10); % draw line at 4 degC
\draw (4,-10) .. controls (4,-4)  and (18,-4) .. (18,0); % draw temperature profile i.e. curved line
\end{tikzpicture}
\end{document}

答案1

最简单的解决方案是在两个图中使用相同的 y 比例,并在两个图中使用相同的坐标系。我的意思是,由于第一个图中的水位为 y=6,所以第二个图中应该使用长度为 6 个单位的 y 轴,并且所有其他 y 坐标都应进行相应调整。事实上,第一个图使用正 y,而第二个图使用负 y,这让事情变得有点复杂,但可以做到:

\documentclass{article}
\usepackage{tikz}
%\usepackage{tikzscale}
\usetikzlibrary{decorations.pathmorphing,calc}

\begin{document}
\usetikzlibrary{decorations.pathmorphing,calc}
\begin{tikzpicture}[y=15mm, x=8mm]
% Define some reference points 
% The figure is drawn a bit bigger, and then clipped to the following dimensions:
\coordinate (clipping area) at (10, 7);
\clip (0,0) rectangle (clipping area);

% Next reference points are relative to the lower left corner of the clipping area
\coordinate (water level) at (0, 6);
\coordinate (bottom)      at (5, 1.3);     % (bottom of the pit)
\coordinate (ground1)     at (0, 5);       % (left shore)
\coordinate (ground2)     at (10, 5);      % (right shore)

% Coordinates of the bigger area really drawn
\coordinate (lower left)  at ([xshift=-5mm, yshift=-5mm] 0,0);
\coordinate (upper right) at ([xshift=5mm,  yshift=5mm] clipping area);

% Draw the water and ripples
\draw [draw=blue!80!black, decoration={bumps, mirror, segment length=6mm}, decorate,
     bottom color=cyan!60!black, top color=blue!20!white] 
  (lower left) rectangle (water level-|upper right);

% draw horizontal dashed line
\draw[dashed](0,4) -- (10,4);
\draw[dashed](0,3.6) -- (10,3.6);

% Draw the ground
\draw [draw=brown!30!black, fill=brown] 
  (lower left) -- (lower left|-ground1)  --
  (ground1) .. controls ($(ground1)!.3!(bottom)$) and (bottom-|ground1) ..
  (bottom) .. controls (bottom-|ground2) and ($(ground2)!.3!(bottom)$) .. 
  (ground2) -- (ground2-|upper right) -- (lower left-|upper right) -- cycle;

\end{tikzpicture}
% Important: no blank lines here
\begin{tikzpicture}
[x = 1.5mm, y = 15mm,transition/.style = {rectangle, draw = black!50}] % define rectangular region
\draw [>=stealth, ->,thick](0,0) -- (0,-6); % draw xaxis for the diagram
\draw [>=stealth, ->,thick](0,0) -- (42,0); % draw yaxis
\foreach \x in {0,4,10,20} { \draw(\x,-2mm)--(\x,2mm) node[above]{\x};} % temperatures for graph
\draw[transition] (0,-2) rectangle +(40,-0.4); % draw the rectangular region
\draw [thick] (0,-2.2) -- (41,-2.2); % define the thermocline
\draw[dashed] (4,0) -- (4,-6); % draw line at 4 degC
\draw (4,-6) .. controls (4,-2)  and (18,-2) .. (18,0); % draw temperature profile i.e. curved line
\end{tikzpicture}
\end{document}

您可以分别使用 x 比例,以便两者都能按照您最喜欢的纵横比适合同一条线。

结果

相关内容