style_utils.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:smhr 作者: andycasey 项目源码 文件源码
def relim_axes(axes, percent=20):
    """
    Generate new axes for a matplotlib axes based on the collections present.

    :param axes:
        The matplotlib axes.

    :param percent: [optional]
        The percent of the data to extend past the minimum and maximum data
        points.

    :returns:
        A two-length tuple containing the lower and upper limits in the x- and
        y-axis, respectively.
    """

    data = np.vstack([item.get_offsets() for item in axes.collections \
        if isinstance(item, PathCollection)])

    if data.size == 0:
        return (None, None)

    data = data.reshape(-1, 2)
    x, y = data[:,0], data[:, 1]

    # Only use finite values.
    finite = np.isfinite(x*y)
    x, y = x[finite], y[finite]

    if x.size > 1:
        xlim = [
            np.min(x) - np.ptp(x) * percent/100.,
            np.max(x) + np.ptp(x) * percent/100.,
        ]

    elif x.size == 0:
        xlim = None

    else:
        xlim = (x[0] - 1, x[0] + 1)


    if y.size > 1:
        ylim = [
            np.min(y) - np.ptp(y) * percent/100.,
            np.max(y) + np.ptp(y) * percent/100.
        ]

    elif y.size == 0: 
        ylim = None

    else:
        ylim = (y[0] - 1, y[0] + 1)

    axes.set_xlim(xlim)
    axes.set_ylim(ylim)

    return (xlim, ylim)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号