Skip to content

Commit cb35dfe

Browse files
committed
FIX: fixed plots with few x labels (closes #1076)
1 parent c7662d5 commit cb35dfe

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

doc/source/changes/version_0_34_2.rst.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ Fixes
5252
but this did not: ::
5353

5454
>>> (X.age != 2) * arr
55+
56+
* fixed plots with fewer than 6 integer labels in the x axis. In that case,
57+
it interpolated the values, which usually looks wrong for integer labels (e.g. year).
58+
Closes :issue:`1076`.

larray/core/plot.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,22 @@ def _plot_array(array, *args, x=None, y=None, series=None, _x_axes_last=False, *
122122
# move label_axis last (it must be a dataframe column)
123123
array = array.transpose(..., label_axis)
124124

125-
lineplot = 'kind' not in kwargs or kwargs['kind'] == 'line'
125+
kind = kwargs.get('kind', 'line')
126+
if kind is None:
127+
kind = 'line'
128+
lineplot = kind == 'line'
129+
# TODO: why don't we handle all line plots this way?
126130
if lineplot and label_axis is not None and series is not None and len(series) > 0:
127131
# the problem with this approach (n calls to pandas.plot) is that the color
128132
# cycling and "stacked" bar/area of pandas break for all kinds of plots except "line"
129133
# when we have more than one dimension involved
130134
for series_key, series_data in array.items(series):
135+
# workaround for issue #1076 (see below for more details)
136+
# matplotlib default behavior when there are few ticks is to interpolate
137+
# them, which looks pretty silly for integer types
138+
x_labels = series_data.axes[0].labels
139+
if len(x_labels) < 6 and np.issubdtype(x_labels.dtype, np.integer) and 'xticks' not in kwargs:
140+
kwargs['xticks'] = x_labels
131141
series_name = ' '.join(str(k) for k in series_key)
132142
# support for list-like y
133143
if isinstance(y, (list, np.ndarray)):
@@ -145,6 +155,15 @@ def _plot_array(array, *args, x=None, y=None, series=None, _x_axes_last=False, *
145155
if not _x_axes_last and label_axis is None:
146156
array = array.transpose(..., array.axes[0])
147157

158+
# workaround for issue #1076 (see above)
159+
# We only do it for line and area charts because specifying xticks
160+
# breaks bar plots (and bar plots do not suffer from the issue anyway)
161+
# (see https://github.com/pandas-dev/pandas/issues/55508)
162+
if kind in {'line', 'area'}:
163+
x_labels = array.axes[0].labels
164+
if len(x_labels) < 6 and np.issubdtype(x_labels.dtype, np.integer) and 'xticks' not in kwargs:
165+
kwargs['xticks'] = x_labels
166+
148167
return PlotObject._to_pd_obj(array).plot(*args, x=x, y=y, **kwargs)
149168

150169
def __call__(self, x=None, y=None, ax=None, subplots=False, layout=None, figsize=None,

0 commit comments

Comments
 (0)