2014-07-26 57 views
6

我遇到同样的问题here,但是,建议的解决方案对我无效。散点图的轴限制 - Matplotlib

我绘制的一组数据的其主要情节有以下模式:

enter image description here

哪个是哪个轴限制从(-1,1)在x和y而变化的曲线图,与这段代码设置保证金:

plt.figure() 
plt.show(data) 
## Add some margin 
l, r, b, t = plt.axis() 
dx, dy = r-l, t-b 
plt.axis([l-0.1*dx, r+0.1*dx, b-0.1*dy, t+0.1*dy]) 

的问题是因为我有更多的‘复杂’的情节,其中的一些变化不得不向我提出。这是产生它的代码:

def plot_quiver_singularities(min_points, max_points, vector_field_x, vector_field_y, file_path): 
    """ 
    Plot the singularities of vector field 
    :param file_path : the path to save the data 
    :param vector_field_x : the vector field x component to be plot 
    :param vector_field_y : the vector field y component to be plot 
    :param min_points : a set (x, y) of min points field 
    :param max_points : a set (x, y) of max points field 
    """ 
    fig = plt.figure(figsize=(8, 8)) 
    ax = fig.add_axes([.13, .3, .6, .6]) 

    ## Plot quiver 
    x, y = numpy.mgrid[-1:1:100*1j, -1:1:100*1j] 
    m = numpy.sqrt(numpy.power(vector_field_x, 2) + numpy.power(vector_field_y, 2)) 
    quiver = ax.quiver(x, y, vector_field_x, vector_field_y, m, zorder=1) 

    ## Plot critical points 
    x = numpy.linspace(-1, 1, x_steps) 
    y = numpy.linspace(-1, 1, y_steps) 

    # Draw the min points 
    x_indices = numpy.nonzero(min_points)[0] 
    y_indices = numpy.nonzero(min_points)[1] 
    ax.scatter(x[x_indices], y[y_indices], marker='$\\circlearrowright$', s=100, zorder=2) 

    # Draw the max points 
    x_indices = numpy.nonzero(max_points)[0] 
    y_indices = numpy.nonzero(max_points)[1] 
    ax.scatter(x[x_indices], y[y_indices], marker='$\\circlearrowleft$', s=100, zorder=2) 

    ## Put legends 
    marker_min = plt.Line2D((0, 0), (0, 0), markeredgecolor=(1.0, 0.4, 0.0), linestyle='', 
          marker='$\\circlearrowright$', markeredgewidth=1, markersize=10) 
    marker_max = plt.Line2D((0, 0), (0, 0), markeredgecolor=(0.2, 0.2, 1.0), linestyle='', 
          marker='$\\circlearrowleft$', markeredgewidth=1, markersize=10) 
    plt.legend([marker_min, marker_max], ['CW rot. center', 'CCW rot. center'], numpoints=1, 
       loc='center left', bbox_to_anchor=(1, 0.5)) 

    quiver_cax = fig.add_axes([.13, .2, .6, .03]) 
    fig.colorbar(quiver, orientation='horizontal', cax=quiver_cax) 

    ## Set axis limits 
    plt.xlim(-1, 1) 
    plt.ylim(-1, 1) 

    ## Add some margin 
    # l, r, b, t = plt.axis() 
    # dx, dy = r-l, t-b 
    # plt.axis([l-0.1*dx, r+0.1*dx, b-0.1*dy, t+0.1*dy]) 

    plt.savefig(file_path + '.png', dpi=dpi) 
    plt.close() 

这将产生以下图片:

enter image description here

可以看出,轴界限不成立,我并没有发现,为什么呢。

任何帮助,将不胜感激。

预先感谢您。

+0

有一点不确定,订单是否起作用?您已将颜色栏放在最后一个轴命令之后。小心尝试删除它? – Dan

+0

是的,我试图做到这一点。结果是一样的。当我调用分散函数时会发生问题。 – pceccon

+0

关闭自动缩放或在调用scatter后重新设置限制。 – tacaswell

回答

14

我能够调用scatter()后立即解决把这段代码

plt.xlim(-1, 1) 
plt.ylim(-1, 1) 

问题。