Python 散点图的轴限制 - Matplotlib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24976239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:32:54  来源:igfitidea点击:

Axis limits for scatter plot - Matplotlib

pythonmatplotlibplotlimits

提问by pceccon

I'm having the same problem presented here, however, the proposed solution didn't work for me.

我在这里遇到了同样的问题,但是,建议的解决方案对我不起作用。

I'm plotting a set of data which the main plot have this pattern:

我正在绘制一组数据,其中主图具有这种模式:

enter image description here

在此处输入图片说明

Which is a plot which axis limits varies from (-1, 1) in both x and y, with a margin set with this piece of code:

这是一个坐标轴限制在 x 和 y 中从 (-1, 1) 变化的图,用这段代码设置了一个边距:

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])

The problem is 'cause I have more "complex" plot in which some changes had to me made. This is the code that produces it:

问题是因为我有更“复杂”的情节,其中必须对我进行一些更改。这是产生它的代码:

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()

This produces the following image:

这会产生以下图像:

enter image description here

在此处输入图片说明

As can be seen, the axis limits do not hold and I didn't found why yet.

可以看出,轴限制不成立,我还没有找到原因。

Any help would be appreciated.

任何帮助,将不胜感激。

Thank you in advance.

先感谢您。

采纳答案by pceccon

I was able to solve the problem putting this piece of code

我能够解决放置这段代码的问题

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

Right after calling scatter().

调用后就可以了scatter()

回答by Lucas Aimaretto

You can also set those to the axobject:

您还可以将它们设置为ax对象:

ax.set_xlim((-1,1))
ax.set_ylim((-1,1))