如何绘制区间图

如何绘制区间图

我尝试使用这篇文章中给出的代码:如何使用 tikz 或其他方式在乳胶中绘制间隔?

但是当我尝试修改代码来适应我的情况时,图表变得完全混乱(间隔数字周围的圆圈、间隔上方的线段等。

我正在尝试制作类似这样的内容: 在此处输入图片描述

我将非常感激任何关于如何执行此操作或我可能如何错误地更改其他帖子中的代码的帮助。

编辑:

使用下面建议的代码作为模板,我已经非常接近了:

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
    % \draw[help lines,dashed] (-1,0) grid (10,7); % helper grid OPTIONAL 
    
    \draw (-1,0)-- (10,0); %draw the horizontal axis
    \foreach \x in {0,...,9} {
        \draw (\x,0.2) -- (\x,-0.2) node[below] {\x}; % draw the ticks
    }
    
    % first line
    \draw (0,1) -- (5,1); %straight line
    \draw[fill=white] (0,1) circle (0.15); %circle filled white over the end of the line <<<<
    \draw[fill] (5,1) circle (0.15);%circle filled black
        
    % second line + two black circles
    \draw (2,2) -- (3,2);
    \draw[fill] (2,2) circle (0.15);
    \draw[fill](3,2) circle (0.15);
    
    % third line + two black circles
    \draw (4,4) -- (6,4);
    \draw[fill] (4,4) circle (0.15);
    \draw[fill](6,4) circle (0.15);
    
    % fourth line + two black circles
    \draw (5,5) -- (6,5);
    \draw[fill] (5,5) circle (0.15);
    \draw[fill](6,5) circle (0.15);
    
    % upper line+  two white circles
    \draw (7,5) -- (9,5);
    \draw[fill=white] (7,5) circle (0.15);
    \draw[fill=white](9,5) circle (0.15);
    
    \end{tikzpicture}

\end{document} 

得出的结果为: 在此处输入图片描述

如您所见,它几乎完美。我只需要将 4-6 的线和 5-6 的线向下移动,这样它们就位于 2-3 的线上方和 7-9 的线下方。

关于如何做到这一点有什么建议吗?我在代码中看不到任何明显控制行高的地方。

答案1

该模板将帮助您入门。

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
    \draw[help lines,dashed] (-1,0) grid (10,7); % helper grid OPTIONAL 
    
    \draw (-1,0)-- (10,0); %draw the horizontal axis
    \foreach \x in {0,...,9} {
        \draw (\x,0.2) -- (\x,-0.2) node[below] {\x}; % draw the ticks
    }
    
    % first line
    \draw (0,1) -- (5,1); %straight line
    \draw[fill=white] (0,1) circle (0.15); %circle filled white over the end of the line <<<<
    \draw[fill] (5,1) circle (0.15);%circle filled black
        
    % second line + two black circles
    \draw (2,2) -- (3,2);
    \draw[fill] (2,2) circle (0.15);
    \draw[fill](3,2) circle (0.15);
    
    % upper line+  two white circles
    \draw (7,5) -- (9,5);
    \draw[fill=white] (7,5) circle (0.15);
    \draw[fill=white](9,5) circle (0.15);
    
    \end{tikzpicture}

\end{document}

相关内容