Skip to content

BUG: 7023 allow style when using error bars #57967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ Period

Plotting
^^^^^^^^
-
- Bug in :meth:`MPLPlot._plot` ignoring parameter ``style`` when using error bar parameters ``yerr`` or ``xerr`` (:issue:`7023`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as in original PR, "Can you describe this in a user-facing way"?
I.E. the method that the user uses is not MPLPlot._plot but rather DataFrame.plot or Series.plot

-

Groupby/resample/rolling
Expand Down
3 changes: 3 additions & 0 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,9 @@ def _plot(
kwds["xerr"] = np.array(kwds.get("xerr"))
if "yerr" in kwds:
kwds["yerr"] = np.array(kwds.get("yerr"))
# GH 7023 allow setting plot style when using errorbars
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# GH 7023 allow setting plot style when using errorbars
# GH 7023

if style is not None:
kwds["fmt"] = style
return ax.errorbar(x, y, **kwds)
else:
# prevent style kwarg from going to errorbar, where it is unsupported
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/plotting/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,47 @@ def _check_errorbar_color(containers, expected, has_err="has_xerr"):
_check_has_errorbars(ax, xerr=0, yerr=1)
_check_errorbar_color(ax.containers, "green", has_err="has_yerr")

def test_errorbar_plot_line_style(self):
def _check_line_style(ax, expected):
for line_num, line_data in enumerate(ax.get_lines()):
received = [
line_data.get_linestyle(),
line_data.get_color(),
line_data.get_marker(),
line_data.get_markeredgecolor(),
line_data.get_markerfacecolor(),
]
assert received == expected[line_num]

# GH 7023
data1 = np.array([9, 3, 5, 1, 7])
data2 = np.array([1, 2, 2, 8, 4])
err1 = data1 * 0.1
err2 = data2 * 0.1
df = DataFrame({"data1": data1, "data2": data2})
err_x = DataFrame({"data1": err1, "data2": err2})
err_y = DataFrame({"data1": err2, "data2": err1})
expected = [[":", "r", "o", "r", "r"], ["--", "g", "v", "g", "g"]]

# check for single line
ax = df["data1"].plot(xerr=err_x["data1"], yerr=err_y["data1"], style="or:")
num_lines = len(ax.get_lines())
_check_has_errorbars(ax, xerr=num_lines, yerr=num_lines)
_check_line_style(ax, expected)

# check for two lines on a single plot
ax = df.plot(xerr=err_x, yerr=err_y, style=["or:", "vg--"], subplots=False)
num_lines = len(ax.get_lines())
_check_has_errorbars(ax, xerr=num_lines, yerr=num_lines)
_check_line_style(ax, expected)

# check for two lines, each on separate subplots
axes = df.plot(xerr=err_x, yerr=err_y, style=["or:", "vg--"], subplots=True)
for ax_num, ax in enumerate(axes):
num_lines = len(ax.get_lines())
_check_has_errorbars(ax, xerr=num_lines, yerr=num_lines)
_check_line_style(ax, [expected[ax_num]])

def test_scatter_unknown_colormap(self):
# GH#48726
df = DataFrame({"a": [1, 2, 3], "b": 4})
Expand Down