Skip to content

Commit 28047c5

Browse files
authored
enh: Small improvements to the Spread (#6883)
1 parent 7023beb commit 28047c5

4 files changed

Lines changed: 79 additions & 9 deletions

File tree

examples/reference/elements/matplotlib/Spread.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
" <dt>Title</dt> <dd> Spread Element</dd>\n",
1010
" <dt>Dependencies</dt> <dd>Matplotlib</dd>\n",
1111
" <dt>Backends</dt>\n",
12-
" <dd><a href='./Spread.ipynb'>Bokeh</a></dd>\n",
12+
" <dd><a href='../bokeh/Spread.ipynb'>Bokeh</a></dd>\n",
1313
" <dd><a href='../matplotlib/Spread.ipynb'>Matplotlib</a></dd>\n",
1414
" <dd><a href='../plotly/Spread.ipynb'>Plotly</a></dd>\n",
1515
"</dl>\n",

examples/reference/elements/plotly/Spread.ipynb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
"<dl class=\"dl-horizontal\">\n",
99
" <dt>Title</dt> <dd> Spread Element</dd>\n",
1010
" <dt>Dependencies</dt> <dd>Plotly</dd>\n",
11-
" <dt>Backends</dt> <dd><a href='./Spread.ipynb'>Bokeh</a></dd> <dd><a href='../matplotlib/Spread.ipynb'>Matplotlib</a></dd> <dd><a href='../plotly/Spread.ipynb'>Plotly</a></dd>\n",
11+
" <dt>Backends</dt>\n",
12+
" <dd><a href='../bokeh/Spread.ipynb'>Bokeh</a></dd>\n",
13+
" <dd><a href='../matplotlib/Spread.ipynb'>Matplotlib</a></dd>\n",
14+
" <dd><a href='../plotly/Spread.ipynb'>Plotly</a></dd>\n",
1215
"</dl>\n",
1316
"</div>"
1417
]

holoviews/plotting/bokeh/chart.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,13 +811,19 @@ class SpreadPlot(ElementPlot):
811811
_plot_methods = dict(single="patch")
812812
_stream_data = False # Plot does not support streaming data
813813

814+
@staticmethod
815+
def _create_nan(data):
816+
if dtype_kind(data) in "mM":
817+
return np.array(["nat"], dtype=data.dtype)
818+
else:
819+
return np.array([np.nan])
820+
814821
def _split_area(self, xs, lower, upper):
815822
"""Splits area plots at nans and returns x- and y-coordinates for
816823
each area separated by nans.
817824
818825
"""
819-
xnan = np.array([np.datetime64("nat") if dtype_kind(xs) == "M" else np.nan])
820-
ynan = np.array([np.datetime64("nat") if dtype_kind(lower) == "M" else np.nan])
826+
xnan, ynan = self._create_nan(xs), self._create_nan(lower)
821827
split = np.where(~isfinite(xs) | ~isfinite(lower) | ~isfinite(upper))[0]
822828
xvals = np.split(xs, split)
823829
lower = np.split(lower, split)

holoviews/tests/plotting/bokeh/test_spreadplot.py

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import holoviews as hv
66
from holoviews.streams import Buffer
7-
from holoviews.testing import assert_data_equal
87

98
from .test_plot import TestBokehPlot, bokeh_renderer
109

@@ -16,8 +15,12 @@ def test_spread_stream_data(self):
1615
plot = bokeh_renderer.get_plot(dmap)
1716
buffer.send({"y": [1, 2, 1, 4], "yerror": [0.5, 0.2, 0.1, 0.5], "x": [0, 1, 2, 3]})
1817
cds = plot.handles["cds"]
19-
assert_data_equal(cds.data["x"], np.array([0.0, 1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 0.0]))
20-
assert_data_equal(cds.data["y"], np.array([0.5, 1.8, 0.9, 3.5, 4.5, 1.1, 2.2, 1.5]))
18+
np.testing.assert_array_equal(
19+
cds.data["x"], np.array([0.0, 1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 0.0])
20+
)
21+
np.testing.assert_array_equal(
22+
cds.data["y"], np.array([0.5, 1.8, 0.9, 3.5, 4.5, 1.1, 2.2, 1.5])
23+
)
2124

2225
def test_spread_with_nans(self):
2326
spread = hv.Spread(
@@ -34,11 +37,11 @@ def test_spread_with_nans(self):
3437
)
3538
plot = bokeh_renderer.get_plot(spread)
3639
cds = plot.handles["cds"]
37-
assert_data_equal(
40+
np.testing.assert_array_equal(
3841
cds.data["x"],
3942
np.array([0.0, 1.0, 2.0, 2.0, 1.0, 0.0, np.nan, 4.0, 5.0, 6.0, 6.0, 5.0, 4.0]),
4043
)
41-
assert_data_equal(
44+
np.testing.assert_array_equal(
4245
cds.data["y"],
4346
np.array([0.0, 0.0, 0.0, 3.0, 2.0, 1.0, np.nan, 0.0, 0.0, 0.0, 7.0, 6.0, 5.0]),
4447
)
@@ -111,3 +114,61 @@ def test_spread_padding_logy(self):
111114
assert x_range.end == 3.2
112115
assert y_range.start == 0.41158562699652224
113116
assert y_range.end == 4.2518491541367327
117+
118+
def test_spread_datetime_x(self):
119+
dates = np.array(["2020-01-01", "2020-01-02", "2020-01-03"], dtype="datetime64[ns]")
120+
spread = hv.Spread((dates, [1.0, 2.0, 3.0], [0.5, 0.5, 0.5]))
121+
plot = bokeh_renderer.get_plot(spread)
122+
cds = plot.handles["cds"]
123+
expected_x = np.array(
124+
["2020-01-01", "2020-01-02", "2020-01-03", "2020-01-03", "2020-01-02", "2020-01-01"],
125+
dtype="datetime64[ns]",
126+
)
127+
expected_y = np.array([0.5, 1.5, 2.5, 3.5, 2.5, 1.5])
128+
np.testing.assert_array_equal(cds.data["x"], expected_x)
129+
np.testing.assert_array_equal(cds.data["y"], expected_y)
130+
131+
def test_spread_datetime_x_with_nat(self):
132+
dates = np.array(
133+
["2020-01-01", "2020-01-02", "NaT", "2020-01-04", "2020-01-05"], dtype="datetime64[ns]"
134+
)
135+
spread = hv.Spread((dates, [1.0, 2.0, np.nan, 4.0, 5.0], [0.5, 0.5, np.nan, 0.5, 0.5]))
136+
plot = bokeh_renderer.get_plot(spread)
137+
cds = plot.handles["cds"]
138+
expected_x = np.array(
139+
[
140+
"2020-01-01",
141+
"2020-01-02",
142+
"2020-01-02",
143+
"2020-01-01",
144+
"NaT",
145+
"2020-01-04",
146+
"2020-01-05",
147+
"2020-01-05",
148+
"2020-01-04",
149+
],
150+
dtype="datetime64[ns]",
151+
)
152+
expected_y = np.array([0.5, 1.5, 2.5, 1.5, np.nan, 3.5, 4.5, 5.5, 4.5])
153+
np.testing.assert_array_equal(cds.data["x"], expected_x)
154+
np.testing.assert_array_equal(cds.data["y"], expected_y)
155+
156+
def test_spread_timedelta_x(self):
157+
tds = np.array([0, 1, 2], dtype="timedelta64[D]")
158+
spread = hv.Spread((tds, [1.0, 2.0, 3.0], [0.5, 0.5, 0.5]))
159+
plot = bokeh_renderer.get_plot(spread)
160+
cds = plot.handles["cds"]
161+
expected_x = np.array([0, 1, 2, 2, 1, 0], dtype="timedelta64[D]")
162+
expected_y = np.array([0.5, 1.5, 2.5, 3.5, 2.5, 1.5])
163+
np.testing.assert_array_equal(cds.data["x"], expected_x)
164+
np.testing.assert_array_equal(cds.data["y"], expected_y)
165+
166+
def test_spread_timedelta_x_with_nat(self):
167+
tds = np.array([0, 1, "NaT", 3, 4], dtype="timedelta64[D]")
168+
spread = hv.Spread((tds, [1.0, 2.0, np.nan, 4.0, 5.0], [0.5, 0.5, np.nan, 0.5, 0.5]))
169+
plot = bokeh_renderer.get_plot(spread)
170+
cds = plot.handles["cds"]
171+
expected_x = np.array([0, 1, 1, 0, "NaT", 3, 4, 4, 3], dtype="timedelta64[D]")
172+
expected_y = np.array([0.5, 1.5, 2.5, 1.5, np.nan, 3.5, 4.5, 5.5, 4.5])
173+
np.testing.assert_array_equal(cds.data["x"], expected_x)
174+
np.testing.assert_array_equal(cds.data["y"], expected_y)

0 commit comments

Comments
 (0)