【问题标题】:Axis limits for scatter plot - Matplotlib散点图的轴限制 - Matplotlib
【发布时间】:2014-09-18 12:27:07
【问题描述】:

我遇到了与here 相同的问题,但是建议的解决方案对我不起作用。

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

这是一个坐标轴限制在 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])

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

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

这会产生以下图像:

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

任何帮助将不胜感激。

提前谢谢你。

【问题讨论】:

  • 这里有点不确定,顺序有影响吗?您已将颜色条放在最后一个轴命令之后。想尝试删除它吗?
  • 是的,我尝试过这样做。结果是一样的。当我调用 scatter 函数时会出现问题。
  • 关闭自动缩放或在调用 scatter 后将限制重新设置为您想要的。
  • 嗨,@tcaswell。重新设置限制不是我正在做的事情:plt.xlim(-1, 1), plt.ylim(-1, 1)?谢谢。
  • 我实际上并没有阅读您的代码(因为它与您的问题无关)。不要在函数中使用plt,因为 认为当前轴是什么和pyplot 认为当前轴是什么很容易不同步。尝试使用ax.set_xlimax.set_ylim

标签: python matplotlib plot limits


【解决方案1】:

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

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

在拨打scatter()之后。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-24
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2013-12-31
      • 2021-11-16
      相关资源
      最近更新 更多