-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
ENH: speed up wide DataFrame.line plots by using a single LineCollection #61764
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
base: main
Are you sure you want to change the base?
Conversation
7bf84c2
to
0febdd9
Compare
pandas/plotting/_matplotlib/core.py
Outdated
threshold = 200 # switch when DataFrame has more than this many columns | ||
can_use_lc = ( | ||
not self._is_ts_plot() # not a TS plot | ||
and not self.stacked # stacking not requested | ||
and not com.any_not_none(*self.errors.values()) # no error bars | ||
and len(self.data.columns) > threshold | ||
) | ||
if can_use_lc: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer not to have a special casing like this because it's difficult to maintain parity between a "fast path" and the existing path.
Is there a way to refactor our plotting here to generalize the plotting to this form rather than the iterative approach below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion, @mroeschke
Removed the early-return fast path; use_collection now only decides how we draw after the shared loop, so there’s one unified code path.
Let me know if you’d like anything tweaked.
f4f499e
to
0febdd9
Compare
0febdd9
to
308f6a6
Compare
What does this PR change?
DataFrame.plot(kind="line")
when the frame is “wide”.RangeIndex
or integer/float values), is not a time-series plot, has no stacking
and no error bars, we now draw everything with a single
matplotlib.collections.LineCollection
instead of oneLine2D
per column.cases above.
Performance numbers
df.plot(legend=False)
Benchmarked on pandas 3.0.0.dev0+2183.g94ff63adb2, matplotlib 3.10.3, NumPy 2.2.6
Notes
DatetimeIndex
plots—those remain on the original per-column path. A follow-up could combineLineCollection
with thex_compat=True
workaround (see #61398) to similarly speed up time-series plots.> 200
columns) is a heuristic and can be tuned in review.indices still use the original per-column draw, so behaviour there is
unchanged.
DataFrame.plot
usingLineCollection
#61532pytest pandas/tests/plotting -q
)pre-commit run --all-files
)doc/source/whatsnew/v3.0.0.rst
cc @shadnikn @arthurlw – happy to take any feedback 🙂