Skip to content

Commit 018ab9a

Browse files
jorisvandenbosscheTomAugspurger
authored andcommitted
PERF: improve plotting performance by not stringifying all x data (#18373)
* add benchmark with basic default plotting (cherry picked from commit 8d04daf)
1 parent 7d7306b commit 018ab9a

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

asv_bench/benchmarks/plotting.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,47 @@ def date_range(start=None, end=None, periods=None, freq=None):
1010
from pandas.tools.plotting import andrews_curves
1111

1212

13+
class Plotting(object):
14+
goal_time = 0.2
15+
16+
def setup(self):
17+
import matplotlib
18+
matplotlib.use('Agg')
19+
self.s = Series(np.random.randn(1000000))
20+
self.df = DataFrame({'col': self.s})
21+
22+
def time_series_plot(self):
23+
self.s.plot()
24+
25+
def time_frame_plot(self):
26+
self.df.plot()
27+
28+
1329
class TimeseriesPlotting(object):
1430
goal_time = 0.2
1531

1632
def setup(self):
1733
import matplotlib
1834
matplotlib.use('Agg')
19-
self.N = 2000
20-
self.M = 5
21-
self.df = DataFrame(np.random.randn(self.N, self.M), index=date_range('1/1/1975', periods=self.N))
35+
N = 2000
36+
M = 5
37+
idx = date_range('1/1/1975', periods=N)
38+
self.df = DataFrame(np.random.randn(N, M), index=idx)
39+
40+
idx_irregular = pd.DatetimeIndex(np.concatenate((idx.values[0:10],
41+
idx.values[12:])))
42+
self.df2 = DataFrame(np.random.randn(len(idx_irregular), M),
43+
index=idx_irregular)
2244

2345
def time_plot_regular(self):
2446
self.df.plot()
2547

2648
def time_plot_regular_compat(self):
2749
self.df.plot(x_compat=True)
2850

51+
def time_plot_irregular(self):
52+
self.df2.plot()
53+
2954

3055
class Misc(object):
3156
goal_time = 0.6

doc/source/whatsnew/v0.21.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Deprecations
3939
Performance Improvements
4040
~~~~~~~~~~~~~~~~~~~~~~~~
4141

42-
-
42+
- Improved performance of plotting large series/dataframes (:issue:`18236`).
4343
-
4444
-
4545

pandas/plotting/_core.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,16 @@ def _add_table(self):
383383

384384
def _post_plot_logic_common(self, ax, data):
385385
"""Common post process for each axes"""
386-
labels = [pprint_thing(key) for key in data.index]
387-
labels = dict(zip(range(len(data.index)), labels))
386+
387+
def get_label(i):
388+
try:
389+
return pprint_thing(data.index[i])
390+
except Exception:
391+
return ''
388392

389393
if self.orientation == 'vertical' or self.orientation is None:
390394
if self._need_to_set_index:
391-
xticklabels = [labels.get(x, '') for x in ax.get_xticks()]
395+
xticklabels = [get_label(x) for x in ax.get_xticks()]
392396
ax.set_xticklabels(xticklabels)
393397
self._apply_axis_properties(ax.xaxis, rot=self.rot,
394398
fontsize=self.fontsize)
@@ -400,7 +404,7 @@ def _post_plot_logic_common(self, ax, data):
400404

401405
elif self.orientation == 'horizontal':
402406
if self._need_to_set_index:
403-
yticklabels = [labels.get(y, '') for y in ax.get_yticks()]
407+
yticklabels = [get_label(y) for y in ax.get_yticks()]
404408
ax.set_yticklabels(yticklabels)
405409
self._apply_axis_properties(ax.yaxis, rot=self.rot,
406410
fontsize=self.fontsize)

0 commit comments

Comments
 (0)