更改 tikz 绘图样式参数(轴上的数字、点的大小)以匹配以前的参数

更改 tikz 绘图样式参数(轴上的数字、点的大小)以匹配以前的参数

我想修改图表以使其与论文中的先前图表一致。在我的整个文档中,我都使用以下样式进行绘图:

在此处输入图片描述 在此处输入图片描述

代码如下:

\begin{figure}[H] %Parabola
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
    \begin{axis}[
        axis lines = left,
        xlabel = \(t\),
        ylabel = {\(f(t)\)},
        ]
        \addplot [
        domain=0:1, 
        samples=100, 
        color=black,
        line width=1pt,
        ]
        {4*(x- 1/2)^2};
    \end{axis}
\end{tikzpicture}}
\end{figure}


\begin{figure}[H] %Discrete
\centering
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
    \begin{axis}
        [
        axis x line=middle,
        axis y line=middle,
        every axis x label={at={(current axis.right of origin)},anchor=north west},
        every axis y label={at={(current axis.above origin)},anchor= north west},
        every axis plot post/.style={mark options={fill=black}},
        xlabel={$n$},
        ylabel={},
        xtick={0, 1, 2, 3, 4, 5, 6, 7 , 8},
        ymin=0,
        ymax=7.0,
        ]
        \addplot+[ycomb,black,thick] table [x={n}, y={xn}] {original.dat};
    \end{axis}
\end{tikzpicture}}\end{figure}

我在用着:

\usepackage{pgfplots}\pgfplotsset{width=8cm,compat=1.3}

特别注意线条的粗细、离散图上点的大小以及两个轴上的编号。现在,我最近发现了一种绘制分段函数的有用方法,并得到了以下结果:

在此处输入图片描述

使用以下代码:

\usepackage{tikz}
\newcommand{\piecewise}[1]{
    \foreach \f/\a/\b/\open/\closed in {#1}{%
        \draw[domain=\a:\b, smooth, variable=\x] plot ({\x}, \f);
        \foreach \x[evaluate={\y=\f;}] in \open{%
            \draw[fill=white] (\x,\y) circle (.8mm);
        }
        \foreach \x[evaluate={\y=\f;}] in \closed{%
            \draw[fill] (\x,\y) circle (.8mm);
        }
    }
}

\begin{figure}[H]
        \centering
        \begin{tikzpicture}
            \draw[->] (-2, 0) -- (7, 0) node[right] {$x$};
            \draw[->] (0, -0.3) -- (0, 3) node[above] {$f(x)$};
            \begin{scope}[line width=0.5pt]
                \piecewise{{-\x+1}/-2/-1/{}/{-2,-1}, 
                    {\x/3 + 7/3}/-1/2/{}/{2},
                    {-\x + 5}/2/4/{}/{4},
                    {-\x + 6}/4/6/{4}/{6}}
            \end{scope}
        \end{tikzpicture}
    \end{figure}

我想获得离散图端点的精确大小;还有轴上的线条粗细和编号,这是为了与我的文档保持一致;但我不知道该怎么做。非常感谢您的帮助。

这是完整的代码:

    \documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=8cm,compat=1.3}
\usepackage{float}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}

\begin{filecontents}{original.dat}
    n   xn 
    1   6.0  
    2   1.0  
    3   3.0  
    4   1.0 
    5   7.0
    6   4.0
\end{filecontents}

\newcommand{\piecewise}[1]{
    \foreach \f/\a/\b/\open/\closed in {#1}{%
        \draw[domain=\a:\b, smooth, variable=\x] plot ({\x}, \f);
        \foreach \x[evaluate={\y=\f;}] in \open{%
            \draw[fill=white] (\x,\y) circle (.8mm);
        }
        \foreach \x[evaluate={\y=\f;}] in \closed{%
            \draw[fill] (\x,\y) circle (.8mm);
        }
    }
}

\title{Test}

\begin{document}

\maketitle

\section{Introduction}

\begin{figure}[H]
    \centering
\begin{minipage}[t]{0.55\textwidth}
    \resizebox{\linewidth}{!}{
    \begin{tikzpicture}
        \begin{axis}[
            axis lines = left,
            xlabel = \(t\),
            ylabel = {\(f(t)\)},
            ]
            \addplot [
            domain=0:1, 
            samples=100, 
            color=black,
            line width=1pt,
            ]
            {4*(x- 1/2)^2};
        \end{axis}
    \end{tikzpicture}}
\end{minipage}
\end{figure}
    

    
\begin{figure}[H]
    \centering
        \centering
\begin{minipage}[t]{0.55\textwidth}
    \resizebox{\linewidth}{!}{
    \begin{tikzpicture}
        \begin{axis}
            [
            axis x line=middle,
            axis y line=middle,
            every axis x label={at={(current axis.right of origin)},anchor=north west},
            every axis y label={at={(current axis.above origin)},anchor= north west},
            every axis plot post/.style={mark options={fill=black}},
            xlabel={$n$},
            ylabel={},
            xtick={0, 1, 2, 3, 4, 5, 6, 7 , 8},
            ymin=0,
            ymax=7.0,
            ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \addplot+[ycomb,black,thick] table [x={n}, y={xn}] {original.dat};
        \end{axis}
    \end{tikzpicture}
    }
    \end{minipage}
\end{figure}
    
    
\begin{figure}[H]
        \centering
        \begin{tikzpicture}
            \draw[->] (-2, 0) -- (7, 0) node[right] {$x$};
            \draw[->] (0, -0.3) -- (0, 3) node[above] {$f(x)$};
            \begin{scope}[line width=0.5pt]
                \piecewise{{-\x+1}/-2/-1/{}/{-2,-1}, 
                    {\x/3 + 7/3}/-1/2/{}/{2},
                    {-\x + 5}/2/4/{}/{4},
                    {-\x + 6}/4/6/{4}/{6}}
            \end{scope}
        \end{tikzpicture}
    \end{figure}    

\end{document}

答案1

据您了解,您希望有tikz图片(通过命令绘制)\piecewise具有相同的图像大小、相同的标记大小和相同的线条粗细。如果您将所有图像绘制为pgfplots图像,这可以相对简单地实现。如果这对您来说不是可以接受的选项,那么您需要执行以下操作:

  • 缩放图像,以便获得相同宽度的图像

    在您的案例中,pgfplots图表宽度为(以下面的 MWE 表示)6 厘米,x 轴的tikzpicure长度为 9 个单位。为了使两个图表具有相同的宽度,˙其单位应为6cm/9

  • pgfplots线条粗细应与(very thick下面的 MWE 中)中的相同

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18,
             width=6cm, height=4.5cm,
             scale only axis,
             }
\usetikzlibrary{arrows}
\tikzset{shorten <>/.style = {shorten <=#1, shorten >=#1},
         every mark/.append style={scale=1.5},
         }

\begin{filecontents}{original.dat}
    n   xn
    1   6.0
    2   1.0
    3   3.0
    4   1.0
    5   7.0
    6   4.0
\end{filecontents}

\newcommand{\piecewise}[1]{
    \foreach \f/\a/\b/\open/\closed in {#1}{%
        \draw[domain=\a:\b, smooth, variable=\x] plot ({\x}, \f);
        \foreach \x[evaluate={\y=\f;}] in \open{%
            \draw[fill=white] (\x,\y) circle (.8mm);
        }
        \foreach \x[evaluate={\y=\f;}] in \closed{%
            \draw[fill] (\x,\y) circle (.8mm);
        }
    }
}

\title{Test}
\author{Me}

\begin{document}

\maketitle

\section{Introduction}

\begin{figure}[ht]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            axis lines = left,
            xlabel = \(t\),
            ylabel = \(f(t)\),
            ]
    \addplot [domain=0:1,
              samples=100,
              very thick,
             ]  {4*(x- 1/2)^2};
        \end{axis}
    \end{tikzpicture}
\end{figure}

\begin{figure}[ht]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            axis lines=middle,
            xlabel={$n$},
            xtick=data,
            ymin=0, ]
            \addplot+[ycomb,black,thick] table [x={n}, y={xn}] {original.dat};
        \end{axis}
    \end{tikzpicture}
\end{figure}

\begin{figure}[ht]
    \centering
    \begin{tikzpicture}[x=6cm/9]
        \draw[->] (-2, 0) -- (7, 0) node[right] {$x$};
        \draw[->] (0, -0.3) -- (0, 3) node[above] {$f(x)$};
        \begin{scope}[very thick]
            \piecewise{{-\x+1}/-2/-1/{}/{-2,-1},
                {\x/3 + 7/3}/-1/2/{}/{2},
                {-\x + 5}/2/4/{}/{4},
                {-\x + 6}/4/6/{4}/{6}}
        \end{scope}
    \end{tikzpicture}
\end{figure}

drawn
\begin{figure}[ht]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            axis lines=middle,
            xlabel=$x$,     xlabel style={anchor=west},
            ylabel=$f(x)$,  ylabel style={anchor=south},
            xtick=\empty,
            ytick=\empty,
            xmax=7,
            ymin=-0.3
                    ]
       \addplot [very thick,red,mark=*]
            coordinates { (-2,3) (-1,2) (2,3) (4,1) };
       \addplot [very thick,red,o-*, shorten <>=-2\pgflinewidth]
            coordinates {(4,2) (6,0) };
       \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

无关:

  • 在序言中每个包只加载一个
  • 我稍微修改了轴前言(使其更加一致和简短)
  • 我定义了图表的大小(没有标签)。这样可以更简单地缩放tikz图像中使用的单位(否则您需要猜测比例因子)
  • 最后一张图(带有红色曲线)是为了比较而绘制的图表。在其中我手动计算坐标,但与您的函数pgfplots相比,这似乎不是一个很大的缺点。\piecewise

附录: 按照 OP 在评论中提出的要求,修改最后一张图片:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18,
             width=6cm, height=4.5cm,
             scale only axis,
             }
\usetikzlibrary{arrows}
\tikzset{shorten <>/.style = {shorten <=#1, shorten >=#1},
         every mark/.append style={scale=1.5}}
\begin{document}

\begin{figure}[ht]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            axis lines=middle,
            grid,                  % <--- added 
            grid style = {dashed}, % <--- added
            xlabel=$x$,     xlabel style={anchor=west},
            ylabel=$f(x)$,  ylabel style={anchor=south},
            xmin=-3,    xmax=7,  % <--- changed
            ymin=-0.5,  ymax=3.5 % <--- changed
                    ]
       \addplot [very thick,mark=*]
            coordinates { (-2,3) (-1,2) (2,3) (4,1) };
       \addplot [very thick,o-*, shorten <>=-2.5\pgflinewidth]
            coordinates {(4,2) (6,0) };
       \end{axis}
    \end{tikzpicture}
\end{figure}

or 

\begin{figure}[ht]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            grid,                    % <--- added
            grid style = {dashed},   % <--- added
            xlabel=$x$,
            ylabel=$f(x)$,
                    ]
       \addplot [very thick,mark=*]
            coordinates { (-2,3) (-1,2) (2,3) (4,1) };
       \addplot [very thick,o-*, shorten <>=-2.5\pgflinewidth]
            coordinates {(4,2) (6,0) };
       \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

相关内容