pgfplot 中断开的时间序列

pgfplot 中断开的时间序列

假设有 66 个实例,其中每个实例应该有一个关联的值(称为 CPU 时间)。但是,其中一些可能缺失。如果有缺失值,我想使下面的 pgfplot 断开连接,这样就会很明显地表明该实例没有可用的数据(而不是连接的线)。下面的代码是一个有很多缺失值的示例(例如,下面的代码中缺少实例 27、28 和 29 的 CPU 时间值。)

\documentclass{article}

\usepackage{pgfplots} \usepackage{graphicx}

\begin{document}


\begin{figure}[h!]
    \centering
    \subfloat[Realistic]{
        \begin{tikzpicture}[scale=0.75, transform shape]
            \begin{axis}[
                title={ },
                xlabel={Instances},
                ylabel={CPU time (min)},
                xmin=0, xmax=52,
                ymin=0, ymax=130,
                xtick={0,1,6,11,16,21,26,31,36,41,46,50},
                xticklabels={,1,6,11,16,21,26,31,36,41,46,50},
                ytick={0,20,40,60,80,100,120,130},
                yticklabels={0,20,40,60,80,100,120, },
                legend pos=outer north east,
                ymajorgrids=true,
                grid style=dashed,
            ]
            
            \addplot[
                color=blue,
                mark=square,
                ]
                coordinates {
                (1,0.1)(2,16.99)(3,17.24)(4,18.14)(5,51.9)(6,4.6)(7,17.97)(8,79.04)(9,107.45)(10,120)(11,120)(12,120)(13,120)(14,120)(15,120)(16,120)(20,120)(21,120)(22,120)(23,120)(24,120)(25,120)(26,120)(30,120)(31,120)(32,120)(33,120)(34,120)(35,120)(36,120)(37,120)(38,120)(39,120)(42,120)(43,120)(44,120)(45,120)(46,120)(47,120)(48,120)(49,120)(50,120)
                };
                \addlegendentry{MP}
                
            \end{axis}
        \end{tikzpicture}
    } \end{figure}


\end{document}

答案1

我只需将数据分成连续的部分,然后将图合并在一个轴环境中。

在此处输入图片描述

\documentclass[varwidth=\maxdimen]{standalone}

\usepackage{pgfplots}
\usepackage{graphicx}

\begin{document}
    
    
    \begin{figure}[h!]
        \centering
            \begin{tikzpicture}[scale=0.75, transform shape]
                \begin{axis}[
                    title={ },
                    xlabel={Instances},
                    ylabel={CPU time (min)},
                    xmin=0, xmax=52,
                    ymin=0, ymax=130,
                    xtick={0,1,6,11,16,21,26,31,36,41,46,50},
                    xticklabels={,1,6,11,16,21,26,31,36,41,46,50},
                    ytick={0,20,40,60,80,100,120,130},
                    yticklabels={0,20,40,60,80,100,120, },
                    legend pos=outer north east,
                    ymajorgrids=true,
                    grid style=dashed,
                    ]
                    
                    \addplot[
                    color=blue,
                    mark=square,
                    ]
                    coordinates {
                        (1,0.1)
                        (2,16.99)
                        (3,17.24)
                        (4,18.14)
                        (5,51.9)
                        (6,4.6)
                        (7,17.97)
                        (8,79.04)
                        (9,107.45)
                        (10,120)
                        (11,120)
                        (12,120)
                        (13,120)
                        (14,120)
                        (15,120)
                        (16,120)
                    };
                    \addlegendentry{MP}
                    
                    \addplot[
                    color=blue,
                    mark=square,
                    ]
                    coordinates {
                        (20,120)
                        (21,120)
                        (22,120)
                        (23,120)
                        (24,120)
                        (25,120)
                        (26,120)
                    };
                    
                    \addplot[
                    color=blue,
                    mark=square,
                    ]
                    coordinates {
                        (30,120)
                        (31,120)
                        (32,120)
                        (33,120)
                        (34,120)
                        (35,120)
                        (36,120)
                        (37,120)
                        (38,120)
                        (39,120)
                    };
                    
                    \addplot[
                    color=blue,
                    mark=square,
                    ]
                    coordinates {
                        (42,120)
                        (43,120)
                        (44,120)
                        (45,120)
                        (46,120)
                        (47,120)
                        (48,120)
                        (49,120)
                        (50,120)
                    };
                \end{axis}
            \end{tikzpicture}
     \end{figure}
    
    
\end{document}

相关内容