具有可变参数的 Tikz 数据图

具有可变参数的 Tikz 数据图

我想要不同的绘图样式。例如,我想要一个方形绘图和一个宽度较大的绘图。

所以实际上我想定义全局参数

宽度正方形=8cm;

宽度较大 = 10cm;

高度=8厘米;

以便使用这些参数创建 tikzpicture。我的想法是,我可以随时更改这些变量,而无需更改每个图的 tikz-Code,并且仍然具有图的一致外观。

这是一个使用 widthSquare 作为变量参数和高度的示例图。

\documentclass[a4paper, 11 pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}

\pgfplotsset{compat=1.11}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
        % Dimensions of plot
        width = widthSquare,
        height = height,
        % Domain for values 
        xmin = 1,
        xmax = 20,
        ymin = 0,
        ymax = 250,
        % Axis labeling
        xlabel = {Frequency [Hz]},
        ylabel = {Amplitude},
        title = {Test plot},
        %legend style = {at={(1.05,0.95)}, anchor = north east, cells = {anchor = west}}
        ]
        \addplot [red, mark = none, thick, smooth] coordinates{(1,20)(5,60)(7,90)(17,150)};
        % Plot aus Datei data.txt mit Tabulator getrennten x und y Koordinaten in gleichen Ordner wie main
        %\addplot table {data.txt} oder auch {data.csv} möglich
        \legend{Graph1}
        \end{axis}
    \end{tikzpicture}
\end{document}

答案1

  • pgfplotsset在序言中设置默认值
  • 定义新的长度\widthLarger并设置其大小
  • 例外,当你不使用设置的默认值时,添加到axis选项中width=\widthLarger

\documentclass[a4paper, 11 pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
    \newlength{\widthLarger}
    \setlength{\widthLarger}{10cm}
\pgfplotsset{compat=1.11,
    height=8cm,
    width=8cm}% most (standard) used width

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
        % Dimensions of plot
        % used default size determined in \pgfplotsset
        % Domain for values
        xmin = 1,
        xmax = 20,
        ymin = 0,
        ymax = 250,
        % Axis labeling
        xlabel = {Frequency [Hz]},
        ylabel = {Amplitude},
        title = {Test plot},
        %legend style = {at={(1.05,0.95)}, anchor = north east, cells = {anchor = west}}
        ]
        \addplot [red, mark = none, thick, smooth] coordinates{(1,20)(5,60)(7,90)(17,150)};
        % Plot aus Datei data.txt mit Tabulator getrennten x und y Koordinaten in gleichen Ordner wie main
        %\addplot table {data.txt} oder auch {data.csv} möglich
        \legend{Graph1}
        \end{axis}
    \end{tikzpicture}

    \begin{tikzpicture}
        \begin{axis}[
        % Dimensions of plot
        width = \widthLarger,
        % Domain for values
        xmin = 1,
        xmax = 20,
        ymin = 0,
        ymax = 250,
        % Axis labeling
        xlabel = {Frequency [Hz]},
        ylabel = {Amplitude},
        title = {Test plot},
        %legend style = {at={(1.05,0.95)}, anchor = north east, cells = {anchor = west}}
        ]
        \addplot [red, mark = none, thick, smooth] coordinates{(1,20)(5,60)(7,90)(17,150)};
        % Plot aus Datei data.txt mit Tabulator getrennten x und y Koordinaten in gleichen Ordner wie main
        %\addplot table {data.txt} oder auch {data.csv} möglich
        \legend{Graph1}
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

相关内容