Repository: matplotlib/matplotlib
File: lib/matplotlib/tests/test_collections.py (Python)

Code before:
        c.set_array("wrong_input")

    # Test if array kwarg is copied
    vals[5] = 45
    assert np.not_equal(vals, c.get_array()).any()


def test_blended_collection_autolim():
    f, ax = plt.subplots()

    # sample data to give initial data limits
    ax.plot([2, 3, 4], [0.4, 0.6, 0.5])
    np.testing.assert_allclose((ax.dataLim.xmin, ax.dataLim.xmax), (2, 4))
    data_ymin, data_ymax = ax.dataLim.ymin, ax.dataLim.ymax

    # LineCollection with vertical lines spanning the Axes vertical, using transAxes
    x = [1, 2, 3, 4, 5]
    vertical_lines = [np.array([[xi, 0], [xi, 1]]) for xi in x]
    trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)
    ax.add_collection(LineCollection(vertical_lines, transform=trans))

    # check that the x data limits are updated to include the LineCollection
    np.testing.assert_allclose((ax.dataLim.xmin, ax.dataLim.xmax), (1, 5))
    # check that the y data limits are not updated (because they are not transData)
    np.testing.assert_allclose((ax.dataLim.ymin, ax.dataLim.ymax), (0.4, 0.6))


def test_singleton_autolim():
    fig, ax = plt.subplots()
    ax.scatter(0, 0)
    np.testing.assert_allclose(ax.get_ylim(), [-0.06, 0.06])
    np.testing.assert_allclose(ax.get_xlim(), [-0.06, 0.06])


@pytest.mark.parametrize("transform, expected", [
    ("transData", (-0.5, 3.5)),
    ("transAxes", (2.8, 3.2)),
])
def test_autolim_with_zeros(transform, expected):
    # 1) Test that a scatter at (0, 0) data coordinates contributes to
    # autoscaling even though any(offsets) would be False in that situation.
    # 2) Test that specifying transAxes for the transform does not contribute
    # to the autoscaling.
    fig, ax = plt.subplots()
    ax.scatter(0, 0, transform=getattr(ax, transform))
    ax.scatter(3, 3)
    np.testing.assert_allclose(ax.get_ylim(), expected)
    np.testing.assert_allclose(ax.get_xlim(), expected)


def test_quadmesh_set_array_validation(pcfunc):

Review comment (suggestion):
Note 1: Before, the test did only check that the x data limits were expanded. It did not investigate the y data limits. Now it also ensures, that y data limits are not changed.

Note 2: I've switched from testing view limits to testing data limits, because that's the relevant quantity autolim influences. We save the `autoscale_view()` by not looking at the view limits.

Code after:
        c.set_array("wrong_input")

    # Test if array kwarg is copied
    vals[5] = 45
    assert np.not_equal(vals, c.get_array()).any()


def test_blended_collection_autolim():
    f, ax = plt.subplots()

    # sample data to give initial data limits
    ax.plot([2, 3, 4], [0.4, 0.6, 0.5])
    np.testing.assert_allclose((ax.dataLim.xmin, ax.dataLim.xmax), (2, 4))
    data_ymin, data_ymax = ax.dataLim.ymin, ax.dataLim.ymax

    # LineCollection with vertical lines spanning the Axes vertical, using transAxes
    x = [1, 2, 3, 4, 5]
    vertical_lines = [np.array([[xi, 0], [xi, 1]]) for xi in x]
    trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)
    ax.add_collection(LineCollection(vertical_lines, transform=trans))

    # check that the x data limits are updated to include the LineCollection
    np.testing.assert_allclose((ax.dataLim.xmin, ax.dataLim.xmax), (1, 5))
    # check that the y data limits are not updated (because they are not transData)
    np.testing.assert_allclose((ax.dataLim.ymin, ax.dataLim.ymax),
                               (data_ymin, data_ymax))


def test_singleton_autolim():
    fig, ax = plt.subplots()
    ax.scatter(0, 0)
    np.testing.assert_allclose(ax.get_ylim(), [-0.06, 0.06])
    np.testing.assert_allclose(ax.get_xlim(), [-0.06, 0.06])


@pytest.mark.parametrize("transform, expected", [
    ("transData", (-0.5, 3.5)),
    ("transAxes", (2.8, 3.2)),
])
def test_autolim_with_zeros(transform, expected):
    # 1) Test that a scatter at (0, 0) data coordinates contributes to
    # autoscaling even though any(offsets) would be False in that situation.
    # 2) Test that specifying transAxes for the transform does not contribute
    # to the autoscaling.
    fig, ax = plt.subplots()
    ax.scatter(0, 0, transform=getattr(ax, transform))
    ax.scatter(3, 3)
    np.testing.assert_allclose(ax.get_ylim(), expected)
    np.testing.assert_allclose(ax.get_xlim(), expected)