python中函数的自适应绘图

时间:2012-12-29 18:23:52

标签: python matplotlib plot

一个简单的问题:我有一个函数f(t),它应该在[0,1]的某个点上有一些尖峰。一个自然的想法是使用此功能的自适应采样来获得一个很好的“自适应”图。我怎么能在Python + matplotlib + numpy +中以快速的方式做到这一点?我可以为[0,1]上的任何t计算f(t)。

似乎Mathematica有这个选项,Python有一个吗?

3 个答案:

答案 0 :(得分:7)

查看我找到的内容:Adaptive sampling of 1D functions,来自scipy-central.org的链接。

代码是:

# License: Creative Commons Zero (almost public domain) http://scpyce.org/cc0

import numpy as np

def sample_function(func, points, tol=0.05, min_points=16, max_level=16,
                    sample_transform=None):
    """
    Sample a 1D function to given tolerance by adaptive subdivision.

    The result of sampling is a set of points that, if plotted,
    produces a smooth curve with also sharp features of the function
    resolved.

    Parameters
    ----------
    func : callable
        Function func(x) of a single argument. It is assumed to be vectorized.
    points : array-like, 1D
        Initial points to sample, sorted in ascending order.
        These will determine also the bounds of sampling.
    tol : float, optional
        Tolerance to sample to. The condition is roughly that the total
        length of the curve on the (x, y) plane is computed up to this
        tolerance.
    min_point : int, optional
        Minimum number of points to sample.
    max_level : int, optional
        Maximum subdivision depth.
    sample_transform : callable, optional
        Function w = g(x, y). The x-samples are generated so that w
        is sampled.

    Returns
    -------
    x : ndarray
        X-coordinates
    y : ndarray
        Corresponding values of func(x)

    Notes
    -----
    This routine is useful in computing functions that are expensive
    to compute, and have sharp features --- it makes more sense to
    adaptively dedicate more sampling points for the sharp features
    than the smooth parts.

    Examples
    --------
    >>> def func(x):
    ...     '''Function with a sharp peak on a smooth background'''
    ...     a = 0.001
    ...     return x + a**2/(a**2 + x**2)
    ...
    >>> x, y = sample_function(func, [-1, 1], tol=1e-3)

    >>> import matplotlib.pyplot as plt
    >>> xx = np.linspace(-1, 1, 12000)
    >>> plt.plot(xx, func(xx), '-', x, y[0], '.')
    >>> plt.show()

    """
    return _sample_function(func, points, values=None, mask=None, depth=0,
                            tol=tol, min_points=min_points, max_level=max_level,
                            sample_transform=sample_transform)

def _sample_function(func, points, values=None, mask=None, tol=0.05,
                     depth=0, min_points=16, max_level=16,
                     sample_transform=None):
    points = np.asarray(points)

    if values is None:
        values = np.atleast_2d(func(points))

    if mask is None:
        mask = Ellipsis

    if depth > max_level:
        # recursion limit
        return points, values

    x_a = points[...,:-1][...,mask]
    x_b = points[...,1:][...,mask]

    x_c = .5*(x_a + x_b)
    y_c = np.atleast_2d(func(x_c))

    x_2 = np.r_[points, x_c]
    y_2 = np.r_['-1', values, y_c]
    j = np.argsort(x_2)

    x_2 = x_2[...,j]
    y_2 = y_2[...,j]

    # -- Determine the intervals at which refinement is necessary

    if len(x_2) < min_points:
        mask = np.ones([len(x_2)-1], dtype=bool)
    else:
        # represent the data as a path in N dimensions (scaled to unit box)
        if sample_transform is not None:
            y_2_val = sample_transform(x_2, y_2)
        else:
            y_2_val = y_2

        p = np.r_['0',
                  x_2[None,:],
                  y_2_val.real.reshape(-1, y_2_val.shape[-1]),
                  y_2_val.imag.reshape(-1, y_2_val.shape[-1])
                  ]

        sz = (p.shape[0]-1)//2

        xscale = x_2.ptp(axis=-1)
        yscale = abs(y_2_val.ptp(axis=-1)).ravel()

        p[0] /= xscale
        p[1:sz+1] /= yscale[:,None]
        p[sz+1:]  /= yscale[:,None]

        # compute the length of each line segment in the path
        dp = np.diff(p, axis=-1)
        s = np.sqrt((dp**2).sum(axis=0))
        s_tot = s.sum()

        # compute the angle between consecutive line segments
        dp /= s
        dcos = np.arccos(np.clip((dp[:,1:] * dp[:,:-1]).sum(axis=0), -1, 1))

        # determine where to subdivide: the condition is roughly that
        # the total length of the path (in the scaled data) is computed
        # to accuracy `tol`
        dp_piece = dcos * .5*(s[1:] + s[:-1])
        mask = (dp_piece > tol * s_tot)

        mask = np.r_[mask, False]
        mask[1:] |= mask[:-1].copy()


    # -- Refine, if necessary

    if mask.any():
        return _sample_function(func, x_2, y_2, mask, tol=tol, depth=depth+1,
                                min_points=min_points, max_level=max_level,
                                sample_transform=sample_transform)
    else:
        return x_2, y_2

答案 1 :(得分:2)

https://github.com/python-adaptive/adaptive似乎是尝试执行此操作以及更多操作:

  

自适应

     

用于数学函数的自适应并行采样的工具。

     

adaptive是一个开放源代码的Python库,旨在使自适应   并行函数评估简单。使用adaptive,您只需提供一个   函数及其边界,并且将在“最佳”状态下进行评估   参数空间中的点。只需几行代码,您就可以   评估计算集群上的功能,实时绘制数据   返回,并微调自适应采样算法。

此项目还受到该问题的另一个答案(或至少一个相关项目)中代码的启发:

  

积分

     

...

     
      
  • Pauli Virtanen的AdaptiveTriSampling脚本(不再可用)   自SciPy Central出现故障以来一直处于在线状态),这为   〜adaptive.Learner2D。
  •   

答案 2 :(得分:-3)

出于绘图目的,不需要自适应采样。为什么不在屏幕分辨率或高于屏幕分辨率进行采样?

POINTS=1920

from pylab import *
x = arange(0,1,1.0/POINTS)
y = sin(3.14*x)
plot(x,y)
axes().set_aspect('equal') ## optional aspect-ratio control
show()

如果您想要任意采样密度,或者该函数与矢量化方法不兼容,您可以逐点构建x,y数组。中间值将由plot()函数进行线性插值。

POINTS=1980
from pylab import *
ax,ay = [],[]
for x in linspace(0.0,POINTS,POINTS):
   if randint(0,50)==0 or x==0 or abs(x-POINTS)<1e-9:
      y = math.sin(4*2*math.pi*x/POINTS)
      ax.append(x), ay.append(y)
# print(ax,ay)
plot(ax,ay)