0

i have always use PGFPlot for basic 2D function graphique. However, I'm unable to plot 1/x^2 with the following code (i included all my documents packages even if only tikz and pgfplot are used here).

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{pgfplots}
\usepackage{wrapfig}
\usepackage{siunitx}
\usepackage{nameref} % For reference
\usepackage{hyperref} % url reference
\usepackage{tikz} % State diagram
\usetikzlibrary{automata,positioning} % State diagram
\usetikzlibrary{arrows} % State diagram
\usetikzlibrary{automata} 
\usetikzlibrary{patterns}
\usetikzlibrary{shapes}
\usetikzlibrary{babel}
\usetikzlibrary{plotmarks}
\usetikzlibrary{quotes,arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{calc}
\usetikzlibrary{positioning} % state diagram x and y 
\usepackage{amsmath} % For matrix in state machine
\usepackage{xcolor}
\usepackage{beramono} % monospaced font with bold variant
\usepackage{lastpage} % To add lastpage référence.
\usepackage{siunitx} % For microsecond
\usepackage{listings} % Required for insertion of code
\usepackage{eso-pic} % Title background

\begin{document}

\begin{center}
\begin{tikzpicture}%[scale=0.6, every node/.style={transform shape}]
    \begin{axis}[
        axis lines = center,
        xlabel = $d$, ylabel=$P$
        %ymin=-1, xmin=0
        %ymax=0, xmax=0
        ]
        \addplot[samples=200, color=blue, domain=0:1]{1/(x*x)};
        \legend{$\frac{1}{x^2}$}
      \end{axis}
    \end{tikzpicture}
  \end{center}

  \end{document}

This produces the following output:Output

How to get a proper graph between 0 and 2-3, without a high value in y axis ? Thank you for your help.

2
  • Thats right for x=0...1. If you want also 0...-1. Add domain=-1:1.
    – Bobyandbob
    Mar 15 '17 at 13:30
  • 2
    This plot is correct. Did you not notice that the y axis is labeled in multiples of 10^4 ? Maybe you should stay away from 0 in your domain= !
    – marsupilam
    Mar 15 '17 at 13:31
3

The following function is technically plotted correctly, however your sketchy way of setting domain causes a pile of problems. I used \pgfplotsset to set the xmin, xmax and ymin, ymax that couldn't be included in \axis square brackets (the reason is unknown).

Here's the code:

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{pgfplots}
\usepackage{wrapfig}
\usepackage{siunitx}
\usepackage{nameref} % For reference
\usepackage{hyperref} % url reference
\usepackage{tikz} % State diagram
\usetikzlibrary{automata,positioning} % State diagram
\usetikzlibrary{arrows} % State diagram
\usetikzlibrary{automata} 
\usetikzlibrary{patterns}
\usetikzlibrary{shapes}
\usetikzlibrary{babel}
\usetikzlibrary{plotmarks}
\usetikzlibrary{quotes,arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{calc}
\usetikzlibrary{positioning} % state diagram x and y 
\usepackage{amsmath} % For matrix in state machine
\usepackage{xcolor}
\usepackage{beramono} % monospaced font with bold variant
\usepackage{lastpage} % To add lastpage référence.
\usepackage{siunitx} % For microsecond
\usepackage{listings} % Required for insertion of code
\usepackage{eso-pic} % Title background

\pgfplotsset{xmin=0, xmax=2, ymin=0, ymax=3}

\begin{document}

    \begin{center}
        \begin{tikzpicture}
        %[scale=0.6, every node/.style={transform shape}]
        \begin{axis}[
        axis lines = center,
        xlabel = $d$, ylabel=$P$, restrict y to domain=0:5]
        \addplot[samples=200, color=blue]{1/(x*x)};
        \legend{$\frac{1}{x^2}$}
        \end{axis}
        \end{tikzpicture}
    \end{center}

\end{document}

And here is the output: x=2, y=3 result you desire.

enter image description here

However, the aspect ratio in this plot isn't 1:1 but rather 2:1. If you want to have a 1:1 aspect ratio plot, you have to include axis equal in axis square brackets. However, this produces a strange result I haven't been able to fix myself. I'll post it anyway as you might know how a workaround. Again, the code + axis equal:

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{pgfplots}
\usepackage{wrapfig}
\usepackage{siunitx}
\usepackage{nameref} % For reference
\usepackage{hyperref} % url reference
\usepackage{tikz} % State diagram
\usetikzlibrary{automata,positioning} % State diagram
\usetikzlibrary{arrows} % State diagram
\usetikzlibrary{automata} 
\usetikzlibrary{patterns}
\usetikzlibrary{shapes}
\usetikzlibrary{babel}
\usetikzlibrary{plotmarks}
\usetikzlibrary{quotes,arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{calc}
\usetikzlibrary{positioning} % state diagram x and y 
\usepackage{amsmath} % For matrix in state machine
\usepackage{xcolor}
\usepackage{beramono} % monospaced font with bold variant
\usepackage{lastpage} % To add lastpage référence.
\usepackage{siunitx} % For microsecond
\usepackage{listings} % Required for insertion of code
\usepackage{eso-pic} % Title background

\pgfplotsset{xmin=0, xmax=2, ymin=0, ymax=3}

\begin{document}

    \begin{center}
        \begin{tikzpicture}
        %[scale=0.6, every node/.style={transform shape}]
        \begin{axis}[axis equal,
        axis lines = center,
        xlabel = $d$, ylabel=$P$, restrict y to domain=0:5]
        \addplot[samples=200, color=blue]{1/(x*x)};
        \legend{$\frac{1}{x^2}$}
        \end{axis}
        \end{tikzpicture}
    \end{center}

\end{document}

And the result.

enter image description here

0

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.