0

I really like the "clean" scientific axes generated by TikZ' datavisualization library. However, since it provides quite a bit more plotting functionality, I want to give pgfplots a shot. How can i emulate datavisualizations's scientific axes=clean style in pgfplots?


\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{datavisualization.formats.functions}

\usepackage{pgfplots}
\pgfmathsetlengthmacro\axisdefaultwidth{5cm}
\pgfmathsetlengthmacro\axisdefaultheight{\axisdefaultwidth * (sqrt(5) - 1) / 2}
\pgfplotsset{
  compat=1.17,
  scale only axis,
}

\begin{document}

With Ti\emph kZ:
\vskip 1ex
\begin{tikzpicture}
  \datavisualization [
      scientific axes=clean,
      visualize as line,
    ]
  data [format=function] {
      var x : interval [-1.5:1.5] samples 7;
      func y = \value x*\value x;
    };
\end{tikzpicture}

\vskip 4ex

With \textsc{pgfplots}:
\vskip 1ex
\begin{tikzpicture}
  \begin{axis}
    \addplot [
        domain=-1.5:1.5,
        samples=7,
      ] {x^2};
  \end{axis}
\end{tikzpicture}

\end{document}

WME output

3

It just amounts to adding some keys. Using

    axis lines=left,
    axis x line shift=1em,
    axis y line shift=1em,
    axis line style={-,very thin},
    axis background/.style={draw,ultra thin,gray},
    tick align=outside,
    xtick distance=0.5,
    ytick distance=0.5,

comes very close IMHO.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{datavisualization.formats.functions}

\usepackage{pgfplots}
\pgfmathsetlengthmacro\axisdefaultwidth{5cm}
\pgfmathsetlengthmacro\axisdefaultheight{\axisdefaultwidth * (sqrt(5) - 1) / 2}
\pgfplotsset{
  compat=1.17,
  scale only axis,
}

\begin{document}

With Ti\emph kZ:
\vskip 2ex
\begin{tikzpicture}
  \datavisualization [
      scientific axes=clean,
      visualize as line,
    ]
  data [format=function] {
      var x : interval [-1.5:1.5] samples 7;
      func y = \value x*\value x;
    };
\end{tikzpicture}

\vskip 4ex

With \textsc{pgfplots}:
\vskip 2ex
\begin{tikzpicture}
  \begin{axis}[axis lines=left,
        axis x line shift=1em,
        axis y line shift=1em,
        axis line style={-,very thin},
        axis background/.style={draw,ultra thin,gray},
        tick align=outside,
        xtick distance=0.5,
        ytick distance=0.5,
        ]
    \addplot [
        domain=-1.5:1.5,
        samples=7,
      ] {x^2};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Needless to say that you can change the size of the plot, and combined the above keys to a style.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{datavisualization.formats.functions}

\usepackage{pgfplots}
\pgfmathsetlengthmacro\axisdefaultwidth{5cm}
\pgfmathsetlengthmacro\axisdefaultheight{\axisdefaultwidth * (sqrt(5) - 1) / 2}
\pgfplotsset{
  compat=1.17,
  scale only axis,
}

\begin{document}

With Ti\emph kZ:
\vskip 2ex
\begin{tikzpicture}
  \datavisualization [
      scientific axes=clean,
      visualize as line,
    ]
  data [format=function] {
      var x : interval [-1.5:1.5] samples 7;
      func y = \value x*\value x;
    };
\end{tikzpicture}

\vskip 4ex

With \textsc{pgfplots}:
\pgfplotsset{mimick datavisualization/.style={axis lines=left,
        axis x line shift=1em,
        axis y line shift=1em,
        axis line style={-,very thin},
        axis background/.style={draw,ultra thin,gray},
        tick align=outside,
        xtick distance=0.5,
        ytick distance=0.5,
        major tick length=2pt}}
\vskip 2ex
\begin{tikzpicture}
  \begin{axis}[mimick datavisualization,unit vector ratio=1 1 1,
    xmin=-1.5,xmax=1.5,width=5.6cm]
    \addplot [semithick,
        domain=-1.5:1.5,
        samples=7,
      ] {x^2};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.