Skip to content

Commit 50a3c8f

Browse files
committed
Dash styles for line collections (hv)lines etc
This goes together with mpld3/mplexporter#71 it's the mpld3 side of the change. Again, done together with gpt-5.1-codex but with full review/polish from me, thoug I think this one's trivial given the other one. See the other PR for example screenshot of it working. I would re-generate the compiled js files once you approve. Also, how shall we do the submodule update commit? Should I include it in this PR after the mplexplorer got in? (I find the submodule to be an annoying maintenance burden btw)
1 parent 3aad00b commit 50a3c8f

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

mpld3/mpld3renderer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ def draw_path_collection(self, paths, path_coordinates, path_transforms,
211211
offsets, offset_coordinates, offset_order,
212212
styles, mplobj=None):
213213
if len(paths) != 0:
214+
dasharrays = styles.get('dasharray', None)
215+
if dasharrays is None:
216+
dasharrays = ["none"]
214217
styles = dict(alphas=[styles['alpha']],
218+
dasharrays=list(dasharrays),
215219
edgecolors=[export_color(ec)
216220
for ec in styles['edgecolor']],
217221
facecolors=[export_color(fc)

mpld3/tests/test_elements.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ def test_scatter():
6060
points = axrep['collections'][0]
6161

6262
assert_equal(list(sorted(points.keys())),
63-
['alphas', 'edgecolors', 'edgewidths', 'facecolors', 'id',
64-
'offsetcoordinates', 'offsets', 'pathcoordinates', 'paths',
65-
'pathtransforms', 'xindex', 'yindex', 'zorder'])
63+
['alphas', 'dasharrays', 'edgecolors', 'edgewidths',
64+
'facecolors', 'id', 'offsetcoordinates', 'offsets',
65+
'pathcoordinates', 'paths', 'pathtransforms', 'xindex',
66+
'yindex', 'zorder'])
6667
assert_equal(points['alphas'], [0.3])
68+
assert_equal(points['dasharrays'], ['none'])
6769
assert_equal(points['zorder'], 10)
6870
assert_equal(points['edgecolors'], ['rgba(0, 0, 255, 0.3)'])
6971
assert_equal(points['facecolors'], ['rgba(255, 0, 0, 0.3)'])
@@ -120,6 +122,17 @@ def test_text():
120122
assert_equal(text['coordinates'], "data")
121123

122124

125+
def test_hlines_linestyle():
126+
fig, ax = plt.subplots()
127+
ax.hlines([0, 1], [0, 0], [1, 2], linestyles='--')
128+
rep = fig_to_dict(fig)
129+
collection = rep['axes'][0]['collections'][0]
130+
131+
assert 'dasharrays' in collection
132+
assert len(collection['dasharrays']) > 0
133+
assert collection['dasharrays'][0] not in (None, 'none')
134+
135+
123136
def test_image():
124137
fig, ax = plt.subplots()
125138
ax.imshow(np.random.random((20, 20)), cmap=plt.cm.binary,

src/elements/path_collection.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mpld3_PathCollection.prototype.defaultProps = {
1717
edgecolors: ["#000000"],
1818
drawstyle: "none",
1919
edgewidths: [1.0],
20+
dasharrays: ["none"],
2021
facecolors: ["#0000FF"],
2122
alphas: [1.0],
2223
zorder: 2
@@ -86,6 +87,7 @@ mpld3_PathCollection.prototype.styleFunc = function(d, i) {
8687
var styles = {
8788
"stroke": stroke,
8889
"stroke-width": getMod(this.props.edgewidths, i),
90+
"stroke-dasharray": getMod(this.props.dasharrays, i),
8991
"fill": fill,
9092
};
9193

0 commit comments

Comments
 (0)