1

I saw a plotting style that I really like and I am trying to recreate it. I am having troubles getting that crisp style and am having problems with the semi-transparent gray borders.

I tried to recreate it but I turned on transparency for the whole thing using \pgfsetfillopacity{0.5} which doesn't look great.

Additionally, the grey boxes have manual coordinate. How do I set the box coordinates relative to the frame instead of the data coordinates?

The plot that I'm trying to copy

This is the plot I want to copy

The code I made and how it looks

    \begin{tikzpicture}
        \begin{axis}[
            height=7cm,
            width=11cm,
            grid=major,
            xmin=-0.5,
            xmax=20.0,
            ymin=-0.6,
            ymax=0.6,
            xlabel={Time [$s$]},
            ylabel={Position [$m$]},
            thick,
            legend columns=-1,
            legend style={/tikz/every even column/.append style={column sep=0.5cm}},
            y tick label style={
                /pgf/number format/.cd,
                    fixed,
                    fixed zerofill,
                    precision=1,
                /tikz/.cd
            },
            x tick label style={
                /pgf/number format/.cd,
                    fixed,
                    fixed zerofill,
                    precision=1,
                /tikz/.cd
            },
            every axis/.append style={font=\small}
        ]
    % Cool looking borders ??
    \pgfsetfillopacity{0.5} % ??
    \draw[line width=0.4pt, fill=gray] (-0.5,-0.6) rectangle (20,-0.45);
    \draw[line width=0.4pt, fill=gray] (-0.5, 0.6) rectangle (20, 0.45);

    % Add sine and cos plots
    \addplot[color=blue, samples=65, domain=0:20]{0.1 * sin(2 * pi * 10 * x)};
    \addplot[color=orange, mark=*, domain=0:20]{0.1 * cos(2 * pi * 10 * x)};

    % Add legends
    \addlegendentry{Element 1};
    \addlegendentry{Element 2};

    \end{axis}
\end{tikzpicture}

What I end up with is a bit different. I didn't find anything for the opacity thing. What this code creates

1 Answers1

0

I'm actually a bit surprised that you managed to find \pgfsetfillopacity before the fill opacity key :)

Try e.g.

\draw[line width=0.4pt, fill opacity=0.5, fill=gray] (-0.5,-0.6) rectangle (20,-0.45);

and remove \pgfsetfillopacity{0.5}.

Regarding the last part of the question, you can access the axis limits with e.g. \pgfkeysvalueof{/pgfplots/xmin}, as mentioned in How to access \xmin, \xmax, \ymin, \ymax from within PGFplots axis environment

So for example

\draw[plotborder] (\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin}) rectangle (\pgfkeysvalueof{/pgfplots/xmax},0.75*\pgfkeysvalueof{/pgfplots/ymin});

where plotborder is a newly defined style, containing the settings from the top example with fill opacity.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\tikzset{
  plotborder/.style={
    line width=0.4pt, fill=gray, fill opacity=0.5
    }
}
\begin{document}
\begin{tikzpicture}
        \begin{axis}[
            height=7cm,
            width=11cm,
            grid=major,
            xmin=-0.5,
            xmax=20.0,
            ymin=-0.6,
            ymax=0.6,
            xlabel={Time [s]},
            ylabel={Position [m]},
            thick,
            legend columns=-1,
            legend style={/tikz/every even column/.append style={column sep=0.5cm}},
            y tick label style={
                /pgf/number format/.cd,
                    fixed,
                    fixed zerofill,
                    precision=1,
                /tikz/.cd
            },
            x tick label style={
                /pgf/number format/.cd,
                    fixed,
                    fixed zerofill,
                    precision=1,
                /tikz/.cd
            },
            every axis/.append style={font=\small}
        ]
    % Cool looking borders ??
    \draw[plotborder] (\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin}) rectangle (\pgfkeysvalueof{/pgfplots/xmax},0.75*\pgfkeysvalueof{/pgfplots/ymin});
    \draw[plotborder] (\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymax}) rectangle (\pgfkeysvalueof{/pgfplots/xmax},0.75*\pgfkeysvalueof{/pgfplots/ymax});

    % Add sine and cos plots
    \addplot[color=blue, samples=65, domain=0:20]{0.1 * sin(2 * pi * 10 * x)};
    \addplot[color=orange, mark=*, domain=0:20]{0.1 * cos(2 * pi * 10 * x)};

    % Add legends
    \addlegendentry{Element 1};
    \addlegendentry{Element 2};

    \end{axis}
\end{tikzpicture}

\end{document}

Torbjørn T.
  • 206,688
  • Awesome thanks! That solves the opacity thing. Any tips on how to parameterize the rectangle coordinates? So I can say: \draw (xmin, ymin) rectangle (xmax, 0.8*ymin); – Speterius Jan 31 '22 at 06:55
  • @Speterius Oops, missed that part, sorry. Try using e.g. \pgfkeysvalueof{/pgfplots/xmin} in a \draw command like that. Equivalently for the other limits. – Torbjørn T. Jan 31 '22 at 06:58