5

I'm trying to use pgfplots to plot a Maxwell-Boltzmann curve. After scouring around the internet for the density function, I've come up with this:

\begin{tikzpicture}

    \def\kB{1.3806488e-23}% boltzmann constant
    \def\temperature{298}% room temperature
    \def\Beta{1/(\kB*\temperature)}
    \def\amu{1.660538921e-27}% atomar mass unit in kg
    \def\mass{1*\amu}

    \begin{axis}[
        axis lines  = left,
        domain      = 0:6000,
        xlabel      = $v$,
        ylabel      = $P(v)$,
        xtick       = \empty,
        ytick       = \empty
    ]

    \addplot[color=black]{
        sqrt(2/pi)*(\mass*\Beta)^(3/2)*x^2*exp(-.5*\mass*\Beta*x^2)
    };

    \end{axis}
\end{tikzpicture}

The curve and everything comes out alright, problem is that, as the title suggests, the top bit of the curve is cut off. I think this has something to do with some sort of automatic scaling, since after stretching (mathematically) the graph by doing 0.5 * f(x), everything seems to remain the same.

Here's an image of what I mean: enter image description here

As can be seen, the peak of the curve is truncated.

Global settings are as such:

\pgfplotsset{
    compat          = 1.13,
    samples         = 100,
    scaled ticks    = false,
    width           = 100mm,
    height          = 60mm
}

Thanks!

3
  • Please provide complete, compilable code rather than fragments: we should be able to copy, paste & compile to see what you see.
    – cfr
    May 23, 2016 at 2:46
  • 2
    As cfr noted, an MWE is really the way to go. But adding clip=false will likely solve your problem. May 23, 2016 at 4:29
  • @PaulGessler, could you write that as an answer. It is the simplest and best.
    – corporal
    May 23, 2016 at 4:45

2 Answers 2

7

This artifact is visible whenever a non-boxed axis style is used. If a boxed plot is used, the axis box line covers this area, so the clipping is not visible.

In addition to the proposed changes by corporal, you can avoid the clipping on a per-axis basis by adding clip=false to the axis options. Beware that this may be unsuitable for plotting certain functions, in particular those with discontinuities tending towards ±∞. But in this case, it works well to prevent the clipping without adjusting the axis limits.

\documentclass[border=10pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{
  compat  = 1.13,
  samples = 100,
}

\def\kB{1.3806488e-23}% boltzmann constant
\def\temperature{298}% room temperature
\def\Beta{1/(\kB*\temperature)}
\def\amu{1.660538921e-27}% atomar mass unit in kg
\def\mass{1*\amu}
\def\mB{\mass*\Beta}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  axis lines = left,
  domain     = 0:6000,
  xlabel     = $v$,
  ylabel     = $P(v)$,
  xtick      = \empty,
  ytick      = \empty,
  clip       = false,
]
  \addplot[black]{sqrt(2/pi)*(\mB)^(3/2)*x^2*exp(-.5*\mB*x^2)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

4

Here are a couple of possible ways to fix the problem. Add enlarge y limits={abs=0.4pt}, after the axis option ytick = \empty, Don't forget the extra comma after \empty. Use abs=0.4pt because this is the line width of the plot, so it removes the truncation or clipping at the top, but also adds a small amount of padding at the horizontal axis.

A separate thing you can try is to add ymax={0.0004}, or even ymax={0.00038}, to the axis options. These were obtained by testing with some different ymax values.

You must log in to answer this question.

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