8
$\begingroup$

Are there any mature Python packages that can plot functions, and possibly use adaptive sampling?

I am looking to pass only a function (can be a numerical black box) and a range, and expect a plot as the output.

What I am not looking for: manually generating the sampling points, manually computing the function values, and finally plotting these pre-computed values. I know matplotlib can do this.

$\endgroup$
1
  • $\begingroup$ Looking for something like MATLAB ezplot, Mathematica Plot, etc. I'm new to using Python like this. I'm interested in ready made libraries, not writing an adaptive sampler myself. $\endgroup$
    – Szabolcs
    Jun 30, 2014 at 23:09

2 Answers 2

4
$\begingroup$

It sounds like you want the multi-precision plot function from SymPy, which is capable of plotting arbitrary black-box functions over a given range.

import math
from sympy.mpmath import plot
plot(lambda x: math.exp(x), [1, 4])

exponential plot

$\endgroup$
2
  • $\begingroup$ Looks like exactly what I was looking for. Are you aware of other packages that have this functionality? $\endgroup$
    – Szabolcs
    Jul 6, 2014 at 2:05
  • $\begingroup$ Geoff mentioned Sage. Nothing else is on my radar. $\endgroup$ Jul 6, 2014 at 3:09
5
$\begingroup$

Yes.

These implementations probably span the extremes of heavyweight (Sage is a big library) and lightweight (a single sampling function is not intrusive, and presumably, you could use it with any graphics backend you like).

$\endgroup$
5
  • $\begingroup$ Is it possible to just use the plotting part of Sage without having to get the whole huge system? I know how to implement a 1D adaptive plotter, I once spent a considerable amount of time thinking about this. But this time I was just looking for a readymade solution. $\endgroup$
    – Szabolcs
    Jul 1, 2014 at 0:03
  • $\begingroup$ You could probably selectively import just the plotting functions you need using the from ... import ... syntax (e.g., from sage.plot import plot3d), but you'd still have to install the whole library. If GPL'd code isn't an issue, you could also look at the source and pull out ready-made parts you need. I figured you know how to do it based on answers to other questions, and I'm a little surprised this feature isn't already part of matplotlib. $\endgroup$ Jul 1, 2014 at 0:21
  • $\begingroup$ Do you often use Python yourself for this type of work? Do you think that your answer is mostly exhaustive? If yes, I'll accept now instead of waiting for more. $\endgroup$
    – Szabolcs
    Jul 1, 2014 at 18:32
  • $\begingroup$ I do. I use Python ~40% of the time, but I'm not always up-to-date on cutting edge Python, because I use it on a piecemeal basis for work projects, and the occasional side project. Most of the Python updates I get, I hear through the grapevine from serious Pythonistas like Aron Ahmadia (one of the lead PyClaw devs), Ondrej Certik (started SymPy, big Cython contributor), William Stein (founded Sage, SageMathCloud), and Andy Terrel (worked on FEniCS, now works at Continuum). People of that caliber would know more than I about this topic, so this answer probably covers 80% of what's out there. $\endgroup$ Jul 1, 2014 at 19:16
  • $\begingroup$ Oooh, now I'm a serious Pythonista? :) $\endgroup$ Jul 5, 2014 at 22:22

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.