pdf 文档中的中心 pgf-plot

pdf 文档中的中心 pgf-plot

我的情节再次遇到问题。

第一的一个是生成的图不在 pdf 的中心。请参见此处:在此处输入图片描述

对于图,就像我使用的示例一样

\includegraphics[trim= 20mm 140mm 0mm 45mm,width....

使其在文档中居中。但我必须找出每个图的模式,因为它们的大小(高度和宽度)各不相同。那么有没有简单的方法可以做到这一点?如代码所示,我将大小设置为:

width=15cm,
height=9cm,

并希望保持这种状态。

第二问题(可能太简单了)是我的图例的大小。它应该是font=\largefont=\Large。嗯,这种代码在带有 ... 的图例中不起作用\LegendImage \LegendEntry,还是可以?

提前致谢!

代码如下:

\begin{filecontents*}{data.txt}
c b
9 3.5e-07
8 3.5e-05   
7 3.5e-03   
6 60   
5 8000   
4 10000  
3 12000  
2 140000  
1 160000  
0 1800000 
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{positioning}
\usepackage{siunitx}

\newcommand\LegendImage[1]{% from pgfplots.code.tex
\draw[%
                /pgfplots/mesh=false,%
                bar width=3pt,% 
                bar shift=0pt,%
                %
                mark repeat=2,%
                mark phase=2,#1] 
                plot coordinates {
                    (0cm,0cm) 
                    (0.3cm,0cm)
                    (0.6cm,0cm)%
                };
}
\newcommand\LegendEntry[1]{\node[anchor=west,black,font=\footnotesize,inner xsep=2pt]{#1};}


\def\showmark#1{\tikz\node[red!95!black]{\pgfuseplotmark{#1}};}
\newcommand{\RM}[1]{\MakeUppercase{\romannumeral #1{.}}}
\begin{document}
\pagenumbering{gobble}
\begin{center}


  \begin{tikzpicture}[trim axis left, trim axis right]
    \begin{semilogxaxis}[
        grid=both,
        width=15cm, 
        height=9cm,
%        axis equal,
        xtick pos=left, style={font=\Large},
        ytick pos=left, style={font=\Large},
        label style={font=\Large},
        title style={draw,fill=none,name=legend},
        title={%
            \showmark{pentagon*}=$\SI{100}{\kilo\Hz}$,
            \showmark{triangle*}=$\SI{10}{\kilo\Hz}$,
            \showmark{square*}=$\SI{1}{\kilo\Hz}$,
            \showmark{diamond*}=$\SI{100}{\Hz}$,
            \showmark{10-pointed star}=$\SI{10}{\Hz}$,
            \showmark{oplus}=$\SI{1}{\Hz}$,
            \showmark{o}=$\SI{20}{\mHz}$},
        xlabel={F}, ylabel={Y}, 
        scatter,
        point meta=explicit symbolic,
        scatter/classes={
          x={},
          g={mark=pentagon*,red!95!black},
          f={mark=triangle*,red!95!black},
          e={mark=square*,red!95!black},
          d={mark=diamond*,red!95!black},
          c={mark=10-pointed star,red!95!black},
          b={mark=oplus,red!95!black},
          a={mark=o,red!95!black},
          z={mark=*,red!80!black}
          },
        ]
\node[font=\large] at (1800000,6.5) {\RM{1}};
\node[font=\large] at (140000,6.5) {\RM{2}};
\node[font=\large] at (60,6.5) {\RM{3}};
\node[font=\large] at (3.5e-05,6.5) {\RM{4}};
\node[font=\large] at (3.5e-06,6.5) {\RM{5}};

\addplot [blue!99!green,mark=+] table [x=b,y=c] {data.txt};
\addplot[mark=none, black] coordinates {(1800000,0) (1800000,6)};
\addplot[mark=none, black] coordinates {(140000,0) (140000,6)};
\addplot[mark=none, black] coordinates {(60,0) (60,6)};
\addplot[mark=none, black] coordinates {(3.5e-05,0) (3.5e-05,6)};


        % coordinate for upper left corner of legend
        \coordinate (legpos) at (rel axis cs:0.02,0.98);



   \end{semilogxaxis}
        \matrix (m) at (legpos) [
           fill=white,
           draw=black,
           anchor=north west,
           cells={anchor=west}]
         {
          \LegendImage{color=black!70!green,mark=x,mark size=2pt} &  \LegendEntry{$\SI{2}{\milli \ampere}$}; \\
          \LegendImage{color=blue!99!green,mark=x,mark size=2pt} &  \LegendEntry{$\SI{5}{\milli \ampere}$}; \\
         };

\end{tikzpicture}
\end{center}
\end{document}

答案1

  • 我添加了\usepackage[showframe]{geometry}显示边框。标题(看起来像图例)比文本宽度宽。我认为这导致了不居中。
  • 我添加了标题选项align=center——然后您可以\\在标题中添加(换行符)。
  • 关于\begin{center}或替代方案\centering,请阅读例如何时应使用 \begin{center} 而不是 \centering?
  • 您的代码示例远非最简化。下次请仅添加最简化的代码以提供帮助。

\begin{filecontents*}{data.txt}
c b
9 3.5e-07
8 3.5e-05   
7 3.5e-03   
6 60   
5 8000   
4 10000  
3 12000  
2 140000  
1 160000  
0 1800000 
\end{filecontents*}

\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}

\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{positioning}
\usepackage{siunitx}

% I added this
\usepackage[showframe]{geometry}

\newcommand\LegendImage[1]{% from pgfplots.code.tex
\draw[%
                /pgfplots/mesh=false,%
                bar width=3pt,% 
                bar shift=0pt,%
                %
                mark repeat=2,%
                mark phase=2,#1] 
                plot coordinates {
                    (0cm,0cm) 
                    (0.3cm,0cm)
                    (0.6cm,0cm)%
                };
}
\newcommand\LegendEntry[1]{\node[anchor=west,black,font=\footnotesize,inner xsep=2pt]{#1};}


\def\showmark#1{\tikz\node[red!95!black]{\pgfuseplotmark{#1}};}
\newcommand{\RM}[1]{\MakeUppercase{\romannumeral #1{.}}}
\begin{document}
\pagenumbering{gobble}

\begin{center}
  \begin{tikzpicture}[trim axis left, trim axis right]
    \begin{semilogxaxis}[
        grid=both,
        width=15cm, 
        height=9cm,
%        axis equal,
        xtick pos=left, style={font=\Large},
        ytick pos=left, style={font=\Large},
        label style={font=\Large},
        title style={draw,fill=none,name=legend,align=center},
        title={%
            \showmark{pentagon*}=$\SI{100}{\kilo\Hz}$,
            \showmark{triangle*}=$\SI{10}{\kilo\Hz}$,
            \showmark{square*}=$\SI{1}{\kilo\Hz}$,\\
            \showmark{diamond*}=$\SI{100}{\Hz}$,
            \showmark{10-pointed star}=$\SI{10}{\Hz}$,
            \showmark{oplus}=$\SI{1}{\Hz}$,
            \showmark{o}=$\SI{20}{\mHz}$},
        xlabel={F}, ylabel={Y}, 
        scatter,
        point meta=explicit symbolic,
        scatter/classes={
          x={},
          g={mark=pentagon*,red!95!black},
          f={mark=triangle*,red!95!black},
          e={mark=square*,red!95!black},
          d={mark=diamond*,red!95!black},
          c={mark=10-pointed star,red!95!black},
          b={mark=oplus,red!95!black},
          a={mark=o,red!95!black},
          z={mark=*,red!80!black}
          },
        ]
\node[font=\large] at (1800000,6.5) {\RM{1}};
\node[font=\large] at (140000,6.5) {\RM{2}};
\node[font=\large] at (60,6.5) {\RM{3}};
\node[font=\large] at (3.5e-05,6.5) {\RM{4}};
\node[font=\large] at (3.5e-06,6.5) {\RM{5}};

\addplot [blue!99!green,mark=+] table [x=b,y=c] {data.txt};
\addplot[mark=none, black] coordinates {(1800000,0) (1800000,6)};
\addplot[mark=none, black] coordinates {(140000,0) (140000,6)};
\addplot[mark=none, black] coordinates {(60,0) (60,6)};
\addplot[mark=none, black] coordinates {(3.5e-05,0) (3.5e-05,6)};


        % coordinate for upper left corner of legend
        \coordinate (legpos) at (rel axis cs:0.02,0.98);



   \end{semilogxaxis}
        \matrix (m) at (legpos) [
           fill=white,
           draw=black,
           anchor=north west,
           cells={anchor=west}]
         {
          \LegendImage{color=black!70!green,mark=x,mark size=2pt} &  \LegendEntry{$\SI{2}{\milli \ampere}$}; \\
          \LegendImage{color=blue!99!green,mark=x,mark size=2pt} &  \LegendEntry{$\SI{5}{\milli \ampere}$}; \\
         };

\end{tikzpicture}
\end{center}

\end{document}

在此处输入图片描述

相关内容