-1

I'm facing problems with my plots again.

The first one is that the produced plot is not centered in the pdf. See here: enter image description here

For plots, like the one as example I use

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

to get it centered in my document. But I have to figure out the patterns for every plot because they vary in size (height and width). So is there a simple way to do this? As seen in the code I set the size to:

width=15cm,
height=9cm,

and want to keep it that way.

Second problem (probably way to easy) is the size of my legend. It should be font=\large or font=\Large. Well, this kind code does not work in a legend with \LegendImage \LegendEntry...or does it?

Thanks in advance!

Here comes the code:

\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}
Torbjørn T.
  • 206,688
Björn
  • 327
  • 1
    There is a font=\footnotesize present in the \LegendEntry macro, not sure if you've noticed that. If you want a larger font, just edit that. For the centering, if reducing the width of the plot is not an option, see http://tex.stackexchange.com/questions/39435 or http://tex.stackexchange.com/questions/16582 – Torbjørn T. Mar 21 '17 at 19:09
  • Note also that you will find the following message in the .log file: LaTeX Warning: Unused global option(s): [paper=a4,xcolor=dvipsnames]. For the standard article class, use a4paper to set the papersize, not paper=a4. And use just dvipsnames, not xcolor=dvipsnames. – Torbjørn T. Mar 21 '17 at 19:12

1 Answers1

1
  • I added \usepackage[showframe]{geometry} to show your borders. The title (that looks like a legend) was wider than the textwidth. I think that this caused the not-centering.
  • I added the title option align=center -- then you can add \\ (line breaks) in the title.
  • Regarding the \begin{center} or the alternatice \centering, please read e. g. When should we use \begin{center} instead of \centering?
  • Your code example is far from minimal. Next time please add only the code that is minimal needen in order to help.

\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}

enter image description here