Пунктирные сегменты в pgfplots и изменение размера шрифта в легенде

Пунктирные сегменты в pgfplots и изменение размера шрифта в легенде

У меня есть следующий код:

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs,siunitx}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
%\usepgfplotslibrary{external}
%\tikzexternalize
\begin{center}
\begin{tikzpicture}
\begin{axis}[
    title={$x-t$ graph model of LAB 1},
    height=10cm,
    width=10.2cm,
    xlabel={time ($t$)},
    ylabel={position ($x$)},
    xmin=0, xmax=10,
    ymin=0, ymax=12,
    xtick={0,1,2,3,4,5,6,7,8,9,10},
    ytick={0,2,4,6,8,10,12},
    legend pos=south east,
    ymajorgrids=true,
    grid style=dashed,
    legend entries={$x$,$x^2$},
        legend style={
            at={(1.03,0.5)},
            anchor=west}
]
\addplot[
    color=blue,
    mark=square,
    ]
    coordinates {
    (0,0)(1.48,2)(2.76,4)(4.02,6)(5.22,8)(6.43,10)(7.76,12)
    };
\addplot[
    color=red,
    mark=square,
    ]
    coordinates {
    (0,0)(1.66,2)(3.21,4)(4.76,6)(6.32,8)(7.81,10)(9.47,12)
    };
\addplot[
    color=green,
    mark=square,
    ]
    coordinates {
    (0,12)(1.53,10)(2.94,8)(4.53,6)(5.95,4)(7.36,2)(8.83,0)
    };
\addplot[
    color=black,
    mark=square,
    ]
    coordinates {
    (0,12)(1.80,10)(3.46,8)(4.94,6)(6.49,4)(8.20,2)(9.85,0)
    };
\legend{Trail 1 (fast and $+$), Trail 2 (slow and $+$), Trail 3 (fast and $-$), Trail 4 (slow and $-$)}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

что дает следующее изображение:

График

Мне интересно, как сделать вышеуказанные сегменты линий пунктирными (т. е. черными, синими, красными, зелеными). Я хочу добавить линию наилучшего соответствия поверх них, поэтому я хотел бы, чтобы линии были пунктирными. Также, как изменить размер текста легенды? Я хотел бы, чтобы он был немного меньше, поэтому я делаю график больше. Спасибо!

решение1

Я внес следующие изменения:

  1. добавлено every axis plot/.append style={line width=2pt,dotted}, что делает каждую строку шириной 2 пт в пунктирном стиле
  2. добавлено mark options={solid}, что делает линию для каждой отметки сплошной, в противном случае они были бы пунктирными
  3. добавлено font=\scriptsize, к legend style, чтобы сделать текст легенды мельче.

введите описание изображения здесь

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs,siunitx}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,
compat=1.9,
every axis plot/.append style={line width=2pt,dotted} %<- added
}
%\usepgfplotslibrary{external}
%\tikzexternalize
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
    title={$x-t$ graph model of LAB 1},
    height=10cm,
    width=10.2cm,
    xlabel={time ($t$)},
    ylabel={position ($x$)},
    xmin=0, xmax=10,
    ymin=0, ymax=12,
    xtick={0,1,2,3,4,5,6,7,8,9,10},
    ytick={0,2,4,6,8,10,12},
    mark options={solid},   %<- to get solid lines for markers
 %   legend pos=south east, %<- this is overwritten by legend style
    ymajorgrids=true,
    grid style=dashed,
    legend entries={$x$,$x^2$},
        legend style={
            at={(1.03,0.5)},
            font=\scriptsize, %<- added
            anchor=west}
]
\addplot[
    color=blue,
    mark=square,
    ]
    coordinates {
    (0,0)(1.48,2)(2.76,4)(4.02,6)(5.22,8)(6.43,10)(7.76,12)
    };
\addplot[
    color=red,
    mark=square,
    ]
    coordinates {
    (0,0)(1.66,2)(3.21,4)(4.76,6)(6.32,8)(7.81,10)(9.47,12)
    };
\addplot[
    color=green,
    mark=square,
    ]
    coordinates {
    (0,12)(1.53,10)(2.94,8)(4.53,6)(5.95,4)(7.36,2)(8.83,0)
    };
\addplot[
    color=black,
    mark=square,
    ]
    coordinates {
    (0,12)(1.80,10)(3.46,8)(4.94,6)(6.49,4)(8.20,2)(9.85,0)
    };
\legend{Trail 1 (fast and $+$), Trail 2 (slow and $+$), Trail 3 (fast and $-$), Trail 4 (slow and $-$)}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

Эта версия применяет стиль линии локально к каждому графику. Это делается путем добавления line width=2pt,dottedк опциям, применяемым к каждому \addplot.

\documentclass[11pt]{scrartcl}
%\usepackage[utf8]{inputenc} %<- default
\usepackage[T1]{fontenc}
\usepackage{booktabs,siunitx}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,
compat=1.9,
%every axis plot/.append style={line width=2pt,dotted} %<- added
}
%\usepgfplotslibrary{external}
%\tikzexternalize
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
    title={$x-t$ graph model of LAB 1},
    height=10cm,
    width=10.2cm,
    xlabel={time ($t$)},
    ylabel={position ($x$)},
    xmin=0, xmax=10,
    ymin=0, ymax=12,
    xtick={0,1,2,3,4,5,6,7,8,9,10},
    ytick={0,2,4,6,8,10,12},
    mark options={solid},   %<- to get solid lines for markers
 %   legend pos=south east, %<- this is overwritten by legend style
    ymajorgrids=true,
    grid style=dashed,
    legend entries={$x$,$x^2$},
        legend style={
            at={(1.03,0.5)},
            font=\scriptsize, %<- added
            anchor=west}
]
\addplot[
    color=blue,
    mark=square,
    line width=2pt,
    dotted
    ]
    coordinates {
    (0,0)(1.48,2)(2.76,4)(4.02,6)(5.22,8)(6.43,10)(7.76,12)
    };
\addplot[
    color=red,
    mark=square,
    line width=2pt,
    dotted
    ]
    coordinates {
    (0,0)(1.66,2)(3.21,4)(4.76,6)(6.32,8)(7.81,10)(9.47,12)
    };
\addplot[
    color=green,
    mark=square,
    line width=2pt,
    dotted
    ]
    coordinates {
    (0,12)(1.53,10)(2.94,8)(4.53,6)(5.95,4)(7.36,2)(8.83,0)
    };
\addplot[
    color=black,
    mark=square,
    line width=2pt,
    dotted
    ]
    coordinates {
    (0,12)(1.80,10)(3.46,8)(4.94,6)(6.49,4)(8.20,2)(9.85,0)
    };
\legend{Trail 1 (fast and $+$), Trail 2 (slow and $+$), Trail 3 (fast and $-$), Trail 4 (slow and $-$)}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

Связанный контент