2014-07-26 22 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) 

問題。