1

I am creating a tikz graph and the values are very small. For some reason, if I make the width larger than 4cm on each side, the code will not execute. I am trying to fit it to the page. Here is my code for reference, thanks:

\documentclass{standalone}
%graph and diagrams
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{automata,positioning}

\pgfplotsset{compat=1.12} %remember this line or the graph wont work

\begin{document}
    
\begin{tikzpicture}[every text node part/.style={align=center}] %text after tikzpicture allows for multiline nodes
\begin{axis}[
    title={K Constant as Determined by Mass and Period},
    xlabel={$Mass: \frac{m_2}{m_1+m_2}_{(g)}$},
    ylabel={$Acceleration: (m/s^2)$},
    xmin=0, xmax=.022,
    ymin=0, ymax=.15,
    xtick={0,.002,.004,.006,.008,.01,.012,.014,.016,.018,.02,.022},  
    ytick={0,.0125,.03,.0425,.06,.0725,.09,.1025,.12,.1375,.15},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed, %remove for undashed grid
    width=4cm,
    height=4cm,%scale of graph window
    xticklabel style={  %remember this paragraph to prevent scientific notation
        /pgf/number format/fixed,
        /pgf/number format/precision=5
},
]

%legend
    \legend{6.564x}

%linear line
\addplot [
    samples=100, 
    color=blue,
]
{6.564*x};

%data points
 \addplot+[only marks, mark=oplus, color=black] coordinates
      {(.0077,.05)(.0107 ,.07)(.0136,.09)(.0167,.11)(.0196,.13)};
\end{axis}
\end{tikzpicture}

\end{document}
1

Welcome to TeX:SE!

The following MWE (Minimal Working Example):

\documentclass[border=3.141592mm]{standalone}
%graph and diagrams
\usepackage{pgfplots}
\pgfplotsset{compat=1.17} %remember this line or the graph wont work
\usetikzlibrary{automata,positioning}

\usepackage{amsmath}
\usepackage{siunitx}


\begin{document}
    \begin{tikzpicture}
\begin{axis}[width=\linewidth, height=7cm,
    title={K Constant as Determined by Mass and Period},
    xlabel={Mass: $\dfrac{m_2}{m_1+m_2}$ (g)},
    ylabel={Acceleration: (\si{m/s^2})},
    label style={font=\small},
    xmin=0, xmax=.022,
    ymin=0,
    xtick={0,.002,...,0.022},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed, %remove for undashed grid
%
    tick label style={font=\scriptsize,
                      /pgf/number format/.cd, fixed, fixed zerofill},
    scaled ticks=false,
    yticklabel style={/pgf/number format/precision=2}, 
    xticklabel style={/pgf/number format/precision=3},             ]
%legend
    \legend{6.564x}
%linear line
\addplot [domain=0:0.022, samples=2,color=blue]   {6.564*x};
%data points
\addplot +[only marks, mark=oplus, color=black] coordinates
      {(.0077,.05) (.0107 ,.07) (.0136,.09) (.0167,.11) (.0196,.13)};
\end{axis}
    \end{tikzpicture}
\end{document}

produce:

enter image description here

Edit: corrected as you ask in comment.

3
  • Thanks so much! One question, how do I move the line to have 0 in the bottom left corner? – Zerender Feb 9 at 0:18
  • @Zerender, to axis preamble just add option ymin=0. Answer is corrected now. – Zarko Feb 9 at 0:32
  • Thanks again :) – Zerender Feb 9 at 0:38

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.