Tikz 矩形接触处无边框

Tikz 矩形接触处无边框

我怎样才能实现,使矩形接触处没有黑色边框。

\begin{figure}[h]
    \begin{tikzpicture}[xscale=6,yscale=0.5]
\draw[thick] (0,0) -- (1,0);
\draw[thick] (0,.1) -- (0,.-.1);
\draw (.2,.05) -- (.2,-.05);
\draw (.4,.05) -- (.4,-.05);
\draw (.6,.05) -- (.6,-.05);
\draw (.8,.05) -- (.8,-.05);
\draw[thick] (1,.1) -- (1,.-.1);
% Knoten unten
\node at (0,-.4) {$0$};
\node at (1,-.4) {$1$};

\newcommand\AONE{.5*0}

\newcommand\BLOCKHEIGHT{0.4}
% U
\draw[fill=red!30] (0,0.2+\AONE) rectangle ++(0.2,3*\BLOCKHEIGHT);
\draw[fill=red!30] (0.2,0.2+\AONE) rectangle ++(0.1,2*\BLOCKHEIGHT);
\draw[fill=red!30] (0.3,0.2+\AONE) rectangle ++(0.1,1*\BLOCKHEIGHT);

\draw[fill=red!30] (0.6,0.2+\AONE) rectangle ++(0.1,3*\BLOCKHEIGHT);
\draw[fill=red!30] (0.7,0.2+\AONE) rectangle ++(0.1,2*\BLOCKHEIGHT);

\node at (-.1,.4+\AONE) {$S$};
    \end{tikzpicture}
\end{figure}

答案1

当然,您可以手动绘制“轮廓”,然后填充颜色。但是,我一直很喜欢 LaTeX 的一点是它能够自动化,让 LaTeX 自己计算要做什么,您只需输入数据即可。

为了回答你的问题我创建了这个命令

\meltedbars[ <options> ]{ <bar width / bar height>, ... }

它适用于您的 Tikz 图片,因此1条形宽度实际上0.1在轴上。如果您更改 Tikz 图片的 x 和 y,则需要适当调整命令。

至于选项,您可以编辑线条和填充颜色的样式。我提供了几个示例来展示这一点以及不同的高度

输出

在此处输入图片描述

代码

\documentclass[margin=15pt]{standalone}
\usepackage{tikz}

\usetikzlibrary{calc}

\tikzset{%
    bar/.style n args={2}{fill=red!30, draw, inner sep=0, anchor=south west, minimum width=#1*6mm, minimum height=#2cm*\BLOCKHEIGHT/2}
}

\newcommand\AONE{.5*0} % I've included it because it's your code, but the result of this is currently 0
\newcommand\BLOCKHEIGHT{0.4}

\newcommand\meltedbars[3][]{%
    \foreach \BarWidth/\BarHeight [%
        count=\n,
        remember=\n as \lastn,
        remember=\BarHeight as \lastheight
        ] in {#3}{%
        \ifnum\n=1\relax%
            \node[bar={\BarWidth}{\BarHeight},#1] (bar\n) at (#2,0.2) {};
        \else
            \node[bar={\BarWidth}{\BarHeight},#1] (bar\n) at (bar\lastn.south east) {};
        \ifnum\BarHeight>\lastheight\relax% if nodes on the right are taller...
            \fill[red!30,#1,draw=none] ($(bar\lastn.south east)+(-.5\pgflinewidth,2*\pgflinewidth)$) rectangle ($(bar\lastn.north east)+(.5\pgflinewidth,-2*\pgflinewidth)$);
        \else % ...or if they aren't
            \fill[red!30,#1,draw=none] ($(bar\n.south west)+(-.5\pgflinewidth,2*\pgflinewidth)$) rectangle ($(bar\n.north west)+(.5\pgflinewidth,-2*\pgflinewidth)$);
        \fi\fi
    }
}

\newcommand\Xaxis{% so we don't have to type it every time
\node at (-.1,.4+\AONE) {$S$};
\draw[thick] (0,0) -- (1,0);
\foreach \tick in {.2,...,.8}{%  you can do the ticks with a \foreach 
\draw (\tick,.05) -- (\tick,-.05);
}
\draw[thick] (0,.06) -- (0,-.06);
\draw[thick] (1,.06) -- (1,-.06);
% Knoten unten
\node at (0,-.4) {$0$};
\node at (1,-.4) {$1$};
}

\begin{document}
    \begin{tikzpicture}[xscale=6,yscale=0.5]
    
    \Xaxis
    \meltedbars{0}{2/3,1/2,1/1}      
    \meltedbars{.6}{1/3,1/2}

    % some extra tests
    \begin{scope}[yshift=-5cm]
        \Xaxis
        \meltedbars[dashed, fill=green!30]{0}{1/5,1/1,1/6,1/4}      
        \meltedbars[densely dotted, fill=orange!30]{.6}{1/1,1/3,1/5}
    \end{scope}   
    \end{tikzpicture}
\end{document}

答案2

不要使用 s,而是rectangle使用具有相对长度的线条作为原始矩形。也可用于\foreach任何重复的线段。

\begin{figure}[h]
\begin{tikzpicture}[xscale=6,yscale=0.5]
\draw[thick] (0,0) -- (1,0);
\draw[thick] (0,.1) -- (0,-.1);
\foreach \x in {.2,.4,.6,.8}
  \draw (\x,.05) -- (\x,-.05);
\draw[thick] (1,.1) -- (1,.-.1);
% Knoten unten
\node at (0,-.4) {$0$};
\node at (1,-.4) {$1$};

\newcommand\AONE{.5*0}

\newcommand\BLOCKHEIGHT{0.4}
% U
\draw[fill=red!30] (0,0.2+\AONE) --++(0,3*\BLOCKHEIGHT) --++(0.2,0)
                   --++(0,-1*\BLOCKHEIGHT) --++(0.1,0) --++(0,-1*\BLOCKHEIGHT)
                   --++(0.1,0) --++(0,-1*\BLOCKHEIGHT) --cycle;

\draw[fill=red!30] (0.6,0.2+\AONE) --++(0,3*\BLOCKHEIGHT) --++(0.1,0)
                   --++(0,-1*\BLOCKHEIGHT) --++(0.1,0) --++(0,-2*\BLOCKHEIGHT)--cycle;
                                     
\node at (-.1,.4+\AONE) {$S$};
    \end{tikzpicture}
\end{figure}

在此处输入图片描述

答案3

示例显示手动调整以删除两个节点之间的中心线,并使用与填充颜色相同的线

在此处输入图片描述

\begin{tikzpicture}[]
  \node [style={draw,fill=red!30,  inner sep=0pt, minimum size=0.5cm}] (1) at (0,  0)  { $1$ };
\node [style={draw,fill=red!30,  inner sep=0pt, minimum size=0.5cm}] (2) at (0.5,  0)  { $2$ };
\draw[red!30,line width=1pt,shorten <=0.4pt, shorten >=0.4pt](1.north east)--(1.south east);
    \end{tikzpicture}

或者

在此处输入图片描述

 \begin{tikzpicture}[]
  \node [style={draw,fill=red!30, rectangle,}] (1) at (0,  0)  { $1$ };
\node [style={draw,fill=red!30, rectangle,}, right=0.0cm of 1, yshift=-4pt] (2)  { $2$ };
\draw[red!30,line width=1pt,shorten <=0.4pt, shorten >=0.4pt](2.north west)--(1.south east);
    \end{tikzpicture}

相关内容