5

I am graphing the restriction of a cubic function on the domain [0, 20]. The function has a maximum value of about 3,079 at about 11.5. To get some view of it, I use the specification unit vector ratio={1 100}. PgfPlots gives me the error message "Dimension too large." How do I get this to plot?

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

\begin{tikzpicture}
\begin{axis}[width=6in,axis equal image,unit vector ratio={1 100},clip=false,
    axis lines=middle,
    xmin=-2,xmax=20,
    domain=-2:0.5,
    xlabel=$x$,ylabel=$y$,
    ymin=-100,ymax=3000,
    restrict y to domain=-100:3000,
    enlargelimits={abs=0.5cm},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xtick={},ytick={},
    extra x ticks={11.547},
    extra x tick labels={$20/\sqrt{3}$},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot[samples=501,domain=-2:20,blue] {400*x - x^3};
\end{axis}
\end{tikzpicture}


\end{document}
5
  • Please reduce to a real MWE. Does this help you: tex.stackexchange.com/q/133923
    – LaRiFaRi
    Commented Nov 4, 2014 at 17:14
  • @LaRiFaRi I do not know why the code is not compiling. The preferred answer to the question that you suggested does not apply to my code.
    – Adelyn
    Commented Nov 4, 2014 at 17:24
  • 2
    Why don't you just remove both axis equal image,unit vector ratio={1 100},?
    – percusse
    Commented Nov 4, 2014 at 18:12
  • @percusse I did remove axis equal image,unit vector ratio={1 100}, as you suggested. The graph is fine, but it is too high. The graph occupies 2/3 of a page. With a command like unit vector ratio={1 250} or unit vector ratio={1 500}, I can get a nice graph that does not occupy so much of the page. Do you know why it is not compiling?
    – Adelyn
    Commented Nov 5, 2014 at 1:10
  • 1
    Just use width and height keys to resize the axes
    – percusse
    Commented Nov 5, 2014 at 6:40

1 Answer 1

2

Using all three of these:

  • width=6in
  • axis equal image
  • unit vector ratio={1 100}

makes no sense. Most of what axis equal image does is to set unit vector ratio={1 1} which you immediately override with the other setting. The only other thing it does is to shrink the axis box to fit the axis min and max values at the specified unit vector ratio, which renders the width key setting totally impossible to reach because of the selected unit vector ratio.

Just let pgfplots do its job (use the width and height keys) and everything works fine. Here's a truly minimal example of that at work1:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    samples=100,
    width=6in,
    height=4in, % or whatever height you want
%    axis equal image,
%    unit vector ratio={1 250},
  ]
    \addplot[domain=-2:20] {400*x - x^3};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1: I've said this before, and I'll say it again: putting all of the excess styling code in all of your questions just makes them harder to read and understand what you're after. Please, make some effort to determine what is actually needed to show the problem you want to solve, before just posting all of it.

As I've also mentioned before, you are fighting pgfplots tooth and nail to get the axes to look the way you want, while at the same time, the functions/other things you are plotting don't really need the advanced capabilities of pgfplots. If you really want this level of control over things pgfplots tries so hard to hide from you (axis scaling, in this instance), you may find it easier to work in tikz directly, without involving pgfplots. Here's some code that gives the output you're after without having to fight pgfplots' default styling/scaling:

\documentclass[tikz]{standalone}
\tikzset{
  axisline/.style={latex-latex},
  tickmark/.style={gray},
  ticklabel/.style={font=\tiny,fill=white,text=black},
}

\begin{document}
\begin{tikzpicture}[x=0.25in,y=0.001in]
  \foreach \y in {500,1000,...,3000} {% y-ticks
    \draw[tickmark] (2pt,\y) -- ++(-4pt,0) % draw the tick mark
      node[left,ticklabel] {$\pgfmathprintnumber{\y}$}; % label it
  }
  \foreach \x in {-2,2,4,...,20} {% x-ticks
    \draw[tickmark] (\x,2pt) -- ++(0,-4pt) % draw the tick mark
      node[below,ticklabel] {$\pgfmathprintnumber{\x}$}; % label it
  }
  \draw[tickmark] (11.547,2pt) -- ++(0,-4pt) % an extra x tick
    node[below,ticklabel] {$20/\sqrt{3}$}; % its label
  \draw[axisline] (0,3000) +(0,0.5cm) node[above right] {$y$} -- (0,-100) -- +(0,-0.5cm); % the y-axis
  \draw[axisline] (-2,0) +(-0.5cm,0) -- (20,0) -- +(0.5cm,0) node[below right] {$x$}; % the x-axis
  \begin{scope}
    \clip (-2.1,-100) rectangle (20.1,3000); % "restrict y to domain"
    \draw[blue,domain=-2:20,samples=100] plot (\x,400*\x - \x^3);
  \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

12
  • This command does give me the axes printed by pgfplots. \tikzset{ axisline/.style={latex-latex}, tickmark/.style={gray}, ticklabel/.style={font=\tiny,fill=white,text=black}, } I am not insisting to use pgfplots to graph functions. This is the first time that I have seen a nice plot of a function with TikZ. I am now inclined to just use `TikZ'.
    – Adelyn
    Commented Feb 19, 2015 at 16:17
  • I will need some explanations to your code. I looked on page 34 of the manual at the following web site. texample.net/media/pgf/builds/pgfmanualCVS2012-11-04.pdf
    – Adelyn
    Commented Feb 19, 2015 at 17:30
  • What is \draw (2pt,\y) -- ++(-4pt,0) instructing TikZ to plot? I think "(2pt,\y)" is the rectangular coordinates for a point 2pt to the right of the y-axis and \y above the x-axis. What are the units for \y? There are no units in the command \foreach \y in {500,1000,...,3000}. I think (-4pt,0) is the rectangular coordinates for a point 4pt to the left of the y-axis and \y above the x-axis. I guess -- ++ instructs TikZ to draw a tick mark between these points. The graph shows the tick marks are symmetric across the y-axis, though.
    – Adelyn
    Commented Feb 19, 2015 at 18:06
  • 1
    The tick marks are centered because they start 2pt to the right of the y-axis and then I use the relative coordinate specifier ++ to move relative to the current point in the path. So we end up with a line from 2pt to the right of the axis to 2pt left of the axis (or 4pt to the left of the starting point). Commented Feb 19, 2015 at 18:19
  • 1
    \pgfmathprintnumber{\x} is used instead of just \x so that the number has , as a thousands separator, which would not happen otherwise. There are many options to configure how this command will print numbers, so I used it so that the style is separate from the content and is easy to change if desired. Commented Feb 19, 2015 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .