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

Code before:
    ax_test.set_position([0.25, 0.25, 0.5, 0.5])
    assert (bbox.x0, bbox.y0, bbox.width, bbox.height) == (0.0, 0.0, 1.0, 1.0)
    ax_ref = fig_ref.add_axes([0.25, 0.25, 0.5, 0.5])


def test_bar_shape_mismatch():
    x = ["foo", "bar"]
    height = [1, 2, 3]
    error_message = (
        r"Mismatch is between 'x' with shape \(2,\) and 'height' with shape \(3,\)"
    )
    with pytest.raises(ValueError, match=error_message):
        plt.bar(x, height)


def test_caps_color():

    # Creates a simple plot with error bars and a specified ecolor
    x = np.linspace(0, 10, 10)
    y = np.sin(x)
    yerr = 0.1
    mpl.rcParams['lines.markeredgecolor'] = 'green'
    ecolor = 'red'

    fig, ax = plt.subplots()
    errorbars = ax.errorbar(x, y, yerr=yerr, ecolor=ecolor, fmt='o', capsize=5)

    # Tests if the caps have the specified color
    for cap in errorbars[2]:
        assert mcolors.same_color(cap.get_edgecolor(), ecolor)


def test_caps_no_ecolor():

    # Creates a simple plot with error bars without specifying ecolor
    x = np.linspace(0, 10, 10)
    y = np.sin(x)
    yerr = 0.1
    mpl.rcParams['lines.markeredgecolor'] = 'green'

    fig, ax = plt.subplots()
    errorbars = ax.errorbar(x, y, yerr=yerr, fmt='o', capsize=5)

    # Tesrts if the caps have the default color (blue)
    for cap in errorbars[2]:
        assert mcolors.same_color(cap.get_edgecolor(), "blue")


def test_pie_non_finite_values():
    fig, ax = plt.subplots()
    df = [5, float('nan'), float('inf')]

Review comment (suggestion):
```suggestion
    mpl.rcParams['lines.markeredgecolor'] = 'green'
    ecolor = 'red'

    fig, ax = plt.subplots()
    errorbars = ax.errorbar(x, sin(x), yerr=0.1, ecolor=ecolor)
```
- Inlining the only-once used parameters make the code more compact.
- The `fmt` and `capsize` parameters don't have an influence. Leaving them out keeps the code more readable.

Code after:
    ax_test.set_position([0.25, 0.25, 0.5, 0.5])
    assert (bbox.x0, bbox.y0, bbox.width, bbox.height) == (0.0, 0.0, 1.0, 1.0)
    ax_ref = fig_ref.add_axes([0.25, 0.25, 0.5, 0.5])


def test_bar_shape_mismatch():
    x = ["foo", "bar"]
    height = [1, 2, 3]
    error_message = (
        r"Mismatch is between 'x' with shape \(2,\) and 'height' with shape \(3,\)"
    )
    with pytest.raises(ValueError, match=error_message):
        plt.bar(x, height)


def test_caps_color():

    # Creates a simple plot with error bars and a specified ecolor
    x = np.linspace(0, 10, 10)
    mpl.rcParams['lines.markeredgecolor'] = 'green'
    ecolor = 'red'

    fig, ax = plt.subplots()
    errorbars = ax.errorbar(x, np.sin(x), yerr=0.1, ecolor=ecolor)

    # Tests if the caps have the specified color
    for cap in errorbars[2]:
        assert mcolors.same_color(cap.get_edgecolor(), ecolor)


def test_caps_no_ecolor():

    # Creates a simple plot with error bars without specifying ecolor
    x = np.linspace(0, 10, 10)
    mpl.rcParams['lines.markeredgecolor'] = 'green'
    fig, ax = plt.subplots()
    errorbars = ax.errorbar(x, np.sin(x), yerr=0.1)

    # Tests if the caps have the default color (blue)
    for cap in errorbars[2]:
        assert mcolors.same_color(cap.get_edgecolor(), "blue")


def test_pie_non_finite_values():
    fig, ax = plt.subplots()
    df = [5, float('nan'), float('inf')]

    with pytest.raises(ValueError, match='Wedge sizes must be finite numbers'):
        ax.pie(df, labels=['A', 'B', 'C'])