def test_contour_1x1_array():
    # github issue 8197
    with pytest.raises(TypeError) as excinfo:
        contour(plt.gca(), [[0]])
    excinfo.match(r'Input z must be at least a 2x2 array.')

    with pytest.raises(TypeError) as excinfo:
        contour(plt.gca(), [0], [0], [[0]])
    excinfo.match(r'Input z must be at least a 2x2 array.')
def test_contour_shape_1d_valid():

    x = np.arange(10)
    y = np.arange(9)
    z = np.random.random((9, 10))

    fig = plt.figure()
    ax = fig.add_subplot(111)
    contour(ax, x, y, z)
def test_contour_empty_levels():

    x = np.arange(9)
    z = np.random.random((9, 9))

    fig, ax = plt.subplots()
    with pytest.warns(UserWarning) as record:
        contour(ax, x, x, z, levels=[])
    assert len(record) == 1
def test_contour_uniform_z():

    x = np.arange(9)
    z = np.ones((9, 9))

    fig, ax = plt.subplots()
    with pytest.warns(UserWarning) as record:
        contour(ax, x, x, z)
    assert len(record) == 1
def test_contour_shape_2d_valid():

    x = np.arange(10)
    y = np.arange(9)
    xg, yg = np.meshgrid(x, y)
    z = np.random.random((9, 10))

    fig = plt.figure()
    ax = fig.add_subplot(111)
    contour(ax, xg, yg, z)
def test_contour_shape_mismatch_2():

    x = np.arange(10)
    y = np.arange(10)
    z = np.random.random((9, 10))

    fig = plt.figure()
    ax = fig.add_subplot(111)

    with pytest.raises(TypeError) as excinfo:
        contour(ax, x, y, z)
    excinfo.match(r'Length of y must be number of rows in z.')
def test_contour_shape_invalid_2():

    x = np.random.random((3, 3, 3))
    y = np.random.random((3, 3, 3))
    z = np.random.random((3, 3, 3))

    fig = plt.figure()
    ax = fig.add_subplot(111)

    with pytest.raises(TypeError) as excinfo:
        contour(ax, x, y, z)
    excinfo.match(r'Input z must be a 2D array.')
def test_given_colors_levels_and_extends():
    _, axes = plt.subplots(2, 4)

    data = np.arange(12).reshape(3, 4)

    colors = ['red', 'yellow', 'pink', 'blue', 'black']
    levels = [2, 4, 8, 10]

    for i, ax in enumerate(axes.flatten()):
        filled = i % 2 == 0.
        extend = ['neither', 'min', 'max', 'both'][i // 2]

        if filled:
            # If filled, we have 3 colors with no extension,
            # 4 colors with one extension, and 5 colors with both extensions
            first_color = 1 if extend in ['max', 'neither'] else None
            last_color = -1 if extend in ['min', 'neither'] else None
            c = contourf(ax, data, colors=colors[first_color:last_color],
                            levels=levels, extend=extend)
        else:
            # If not filled, we have 4 levels and 4 colors
            c = contour(ax, data, colors=colors[:-1],
                           levels=levels, extend=extend)

        plt.colorbar(c, ax=ax)
def test_contour_shape_mismatch_3():

    x = np.arange(10)
    y = np.arange(10)
    xg, yg = np.meshgrid(x, y)
    z = np.random.random((9, 10))

    fig = plt.figure()
    ax = fig.add_subplot(111)

    with pytest.raises(TypeError) as excinfo:
        contour(ax, xg, y, z)
    excinfo.match(r'Number of dimensions of x and y should match.')

    with pytest.raises(TypeError) as excinfo:
        contour(ax, x, yg, z)
    excinfo.match(r'Number of dimensions of x and y should match.')
Beispiel #10
0
def test_contour_labels_size_color():

    x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 10))
    z = np.max(np.dstack([abs(x), abs(y)]), 2)

    plt.figure(figsize=(6, 2))
    cs = contour(plt.gca(), x, y, z)
    pts = np.array([(1.5, 3.0), (1.5, 4.4), (1.5, 6.0)])
    plt.clabel(cs, manual=pts, fontsize='small', colors=('r', 'g'))
Beispiel #11
0
def test_contour_manual_labels():

    x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 10))
    z = np.max(np.dstack([abs(x), abs(y)]), 2)

    plt.figure(figsize=(6, 2))
    cs = contour(plt.gca(), x, y, z)
    pts = np.array([(1.5, 3.0), (1.5, 4.4), (1.5, 6.0)])
    plt.clabel(cs, manual=pts)
Beispiel #12
0
def test_contour_shape_mismatch_4():

    g = np.random.random((9, 10))
    b = np.random.random((9, 9))
    z = np.random.random((9, 10))

    fig = plt.figure()
    ax = fig.add_subplot(111)

    with pytest.raises(TypeError) as excinfo:
        contour(ax, b, g, z)
    excinfo.match(r'Shape of x does not match that of z: found \(9L?, 9L?\) ' +
                  r'instead of \(9L?, 10L?\)')

    with pytest.raises(TypeError) as excinfo:
        contour(ax, g, b, z)
    excinfo.match(r'Shape of y does not match that of z: found \(9L?, 9L?\) ' +
                  r'instead of \(9L?, 10L?\)')
Beispiel #13
0
def test_circular_contour_warning():
    # Check that almost circular contours don't throw a warning
    with pytest.warns(None) as record:
        x, y = np.meshgrid(np.linspace(-2, 2, 4), np.linspace(-2, 2, 4))
        r = np.sqrt(x ** 2 + y ** 2)

        plt.figure()
        cs = contour(plt.gca(), x, y, r)
        plt.clabel(cs)
    assert len(record) == 0
Beispiel #14
0
def test_contour_datetime_axis():
    fig = plt.figure()
    fig.subplots_adjust(hspace=0.4, top=0.98, bottom=.15)
    base = datetime.datetime(2013, 1, 1)
    x = np.array([base + datetime.timedelta(days=d) for d in range(20)])
    y = np.arange(20)
    z1, z2 = np.meshgrid(np.arange(20), np.arange(20))
    z = z1 * z2
    plt.subplot(221)
    contour(plt.gca(), x, y, z)
    plt.subplot(222)
    contourf(plt.gca(), x, y, z)
    x = np.repeat(x[np.newaxis], 20, axis=0)
    y = np.repeat(y[:, np.newaxis], 20, axis=1)
    plt.subplot(223)
    contour(plt.gca(), x, y, z)
    plt.subplot(224)
    contourf(plt.gca(), x, y, z)
    for ax in fig.get_axes():
        for label in ax.get_xticklabels():
            label.set_ha('right')
            label.set_rotation(30)
def get_contour_vertices(x, y, f, lev):
    import legacycontour as cntr
    c = cntr.contour(x, y, f)
    nlist = c.trace(lev, lev, 0)
    segs = nlist[:len(nlist) // 2]
    N = len(segs[0][:, 0])
    xr = [segs[0][ix, 0] for ix in range(N)]
    yr = [segs[0][ix, 1] for ix in range(N)]

    #Set contour to None if it's found to reach the physical domain
    if x.min() >= min(segs[0][:, 0]) or max(segs[0][:, 0]) >= x.max() or \
       y.min() >= min(segs[0][:, 1]) or max(segs[0][:, 1]) >= y.max():
        return [None, None]

    return [xr, yr]  # x,y coords of contour points.
Beispiel #16
0
def test_contour_badlevel_fmt():
    # test funny edge case from
    # https://github.com/matplotlib/matplotlib/issues/9742
    # User supplied fmt for each level as a dictionary, but
    # MPL changed the level to the minimum data value because
    # no contours possible.
    # This would error out pre
    # https://github.com/matplotlib/matplotlib/pull/9743
    x = np.arange(9)
    z = np.zeros((9, 9))

    fig, ax = plt.subplots()
    fmt = {1.: '%1.2f'}
    with pytest.warns(UserWarning) as record:
        cs = contour(ax, x, x, z, levels=[1.])
        ax.clabel(cs, fmt=fmt)
    assert len(record) == 1
Beispiel #17
0
def test_labels():
    # Adapted from pylab_examples example code: contour_demo.py
    # see issues #2475, #2843, and #2818 for explanation
    delta = 0.025
    x = np.arange(-3.0, 3.0, delta)
    y = np.arange(-2.0, 2.0, delta)
    X, Y = np.meshgrid(x, y)
    Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
    Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
    # difference of Gaussians
    Z = 10.0 * (Z2 - Z1)

    fig, ax = plt.subplots(1, 1)
    CS = contour(ax, X, Y, Z)
    disp_units = [(216, 177), (359, 290), (521, 406)]
    data_units = [(-2, .5), (0, -1.5), (2.8, 1)]

    CS.clabel()

    for x, y in data_units:
        CS.add_label_near(x, y, inline=True, transform=None)

    for x, y in disp_units:
        CS.add_label_near(x, y, inline=True, transform=False)