Python Matplotlib散点图轴自动缩放对于小数据值失败

Python Matplotlib散点图轴自动缩放对于小数据值失败,python,matplotlib,Python,Matplotlib,使用Matplotlib的散点图时,有时自动缩放有效,有时无效 我怎么修理它 与bug报告中提供的示例一样,此代码起作用: plt.figure() x = np.array([0,1,2,3]) x = np.array([2,4,5,9]) plt.scatter(x,y) 但当使用较小的值时,缩放无法工作: plt.figure() x = np.array([0,1,2,3]) x = np.array([2,4,5,9]) plt.scatter(x/10000,y/10000)

使用Matplotlib的散点图时,有时自动缩放有效,有时无效

我怎么修理它

与bug报告中提供的示例一样,此代码起作用:

plt.figure()
x = np.array([0,1,2,3])
x = np.array([2,4,5,9])
plt.scatter(x,y)
但当使用较小的值时,缩放无法工作:

plt.figure()
x = np.array([0,1,2,3])
x = np.array([2,4,5,9])
plt.scatter(x/10000,y/10000)

编辑:可以找到一个示例。我没有在问题中详细说明具体原因,因为当遇到错误时,不清楚是什么原因导致了错误。此外,我已在自己的回答中指定了解决方案和原因。

至少在Matplotlib 1.5.1中,有一个错误,如报告所述,小数据值的自动缩放失败

解决方法是使用
.set_ylim(底部,顶部)
()手动设置数据限制(在本例中,对于y轴,要设置x轴,请使用
.set_xlim(左,右)

为了自动找到令人满意的数据限制,可以使用以下伪代码:

def set_axlims(series, marginfactor):
    """
    Fix for a scaling issue with matplotlibs scatterplot and small values.
    Takes in a pandas series, and a marginfactor (float).
    A marginfactor of 0.2 would for example set a 20% border distance on both sides.
    Output:[bottom,top]
    To be used with .set_ylim(bottom,top)
    """
    minv = series.min()
    maxv = series.max()
    datarange = maxv-minv
    border = abs(datarange*marginfactor)
    maxlim = maxv+border
    minlim = minv-border

    return minlim,maxlim

有时你所说的
是什么意思?
?用一些例子演示一下。我已经编辑了这个问题,并链接到了一个例子。