Skip to content

Commit b0f7973

Browse files
committed
Fixed bug with discrete scale and an empty panel
fixes #647
1 parent 2e1e900 commit b0f7973

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

doc/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ v0.10.2
88
Bug Fixes
99
*********
1010

11+
- Fixed bug where a discrete position scale failed when mapping
12+
an empty variable. (:issue:`647`)
13+
1114
- Fixed bug where :class:`~plotnine.facets.facet_grid` with a datetime
1215
column run into an exception. (:issue:`629`)
1316

plotnine/scales/scale_xy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def map(self, series, limits=None):
7676
if array_kind.discrete(series):
7777
seq = np.arange(1, len(limits)+1)
7878
idx = np.asarray(match(series, limits, nomatch=len(series)))
79+
if not len(idx):
80+
return np.array([])
7981
try:
8082
seq = seq[idx]
8183
except IndexError:

tests/test_scale_internals.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66
import pandas as pd
77
import pytest
88

9-
from plotnine import ggplot, aes, geom_point, expand_limits, theme
10-
from plotnine import geom_col, geom_bar, lims, element_text, annotate
9+
from plotnine import (
10+
aes,
11+
annotate,
12+
element_text,
13+
expand_limits,
14+
facet_wrap,
15+
geom_bar,
16+
geom_col,
17+
geom_point,
18+
ggplot,
19+
lims,
20+
theme,
21+
)
1122
from plotnine.scales import scale_color, scale_color_manual
1223
from plotnine.scales import scale_identity
1324
from plotnine.scales import scale_manual
@@ -760,3 +771,27 @@ def test_discrete_scale_exceeding_maximum_number_of_values():
760771
)
761772
with pytest.warns(PlotnineWarning):
762773
p.draw_test()
774+
775+
776+
def test_discrete_scale_for_empty_layer():
777+
# Ref: https://github.com/has2k1/plotnine/issues/647
778+
df1 = pd.DataFrame({
779+
'x': list("abc"),
780+
'y': [1, 2, 3],
781+
'g': list("AAA")
782+
783+
})
784+
785+
df2 = pd.DataFrame({
786+
'x': list("abc"),
787+
'y': [4, 5, 6],
788+
'g': list("AAB")
789+
})
790+
791+
p = (ggplot(aes("x", "y"))
792+
+ geom_point(df1)
793+
+ geom_point(df2)
794+
+ facet_wrap("g", scales="free_x")
795+
)
796+
797+
p.draw_test()

0 commit comments

Comments
 (0)