1

I would like to be able to change the line style within a certain section of the graph given a single addplot command. Is there any way possible to change the properties of the line style for specified sections of the x axis? I have only seen similar Q&A addressing multiple addplots to fulfill the different line style and also a nice example in which it seems the solution is by adding a draw with a different style to meet the expectations of the question.. Though, it seems that it also just adds a different plot at the end plot visualization..

My MWE is shown below with an "ugly" solution of just splitting the sections as multiple addplot commands.

enter image description here

\documentclass[10pt,border=3mm,tikz]{standalone}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.18}
    \usetikzlibrary{pgfplots.groupplots}
\begin{document}
\begin{tikzpicture}   
        \begin{groupplot}[
            group style={
                group size = 1 by 1,
                },
            clip=true,
            enlargelimits=true,
            height = 0.75\columnwidth,
            width = 1\columnwidth,
            axis x line = center,
            axis y line = center,
            xlabel={$x$},
            xmin=0,
            xmax=3,
            xtick={0,1,2,3}
            ]
            \nextgroupplot[ymin=0,
                            ymax=10,
                            ytick={0,2,...,10},
                            ylabel = {$y$}
                            ]

                \addplot[color=blue, domain = 0:1, samples=50                   
                        ] {x^2};
                \addplot[color=blue, dashed, domain = 1:2, samples=50                   
                        ] {x^2};
                \addplot[color=blue, domain = 2:3, samples=50                   
                        ] {x^2};

        \end{groupplot}    
    \end{tikzpicture}
\end{document}

Thanks in advance.

MS-SPO
  • 11,519
  • 2
    Doesn't look like conditional styling or similar is implemented. But looks like you can try softclipping, see Ch. "5.5 Decoration: Soft Clipping" in the PGFPLOTSmanual. However, code won't become shorter ... or easier to understand ... – MS-SPO May 15 '23 at 15:00
  • You could maybe use a custom dash pattern, but it would require a lot of calculation. I would actually stick to your version. It is at least clean and easy to read. – Jasper Habicht May 15 '23 at 16:16

1 Answers1

3

You can make use of soft clip provided by the fillbetween library (as suggested by MS-SPO in the comments) and create your custom style inspired by the example on page 104 of the current PGFPlots manual (version 1.18.1):

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{pgfplots.groupplots, fillbetween}

\pgfplotsset{ dashed between/.style args={#1:#2}{ draw=none, postaction={ draw, decorate, decoration={ soft clip, soft clip path={ (\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin}) rectangle (#1,\pgfkeysvalueof{/pgfplots/ymax}) }, } }, postaction={ draw, dashed, decorate, decoration={ soft clip, soft clip path={ (#1,\pgfkeysvalueof{/pgfplots/ymin}) rectangle (#2,\pgfkeysvalueof{/pgfplots/ymax}) }, } }, postaction={ draw, decorate, decoration={ soft clip, soft clip path={ (#2,\pgfkeysvalueof{/pgfplots/ymin}) rectangle (\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax}) }, } } } }

\begin{document} \begin{tikzpicture}
\begin{groupplot}[ group style={ group size=1 by 1, }, clip=true, enlargelimits=true, height=0.75\columnwidth, width=1\columnwidth, axis x line=center, axis y line=center, xlabel={$x$}, xmin=0, xmax=3, xtick={0,1,2,3} ] \nextgroupplot[ ymin=0, ymax=10, ytick={0,2,...,10}, ylabel={$y$} ]

    \addplot[color=blue, domain={0:3}, dashed between={1:2}] {x^2};

    \addplot[color=red, domain={0:3}, dashed between={0.5:2.5}] {4};

\end{groupplot}    

\end{tikzpicture} \end{document}

enter image description here