Skip to content

Commit 523f5c2

Browse files
Working with latest matplotlib, scale_lines also now scales markers.
1 parent 6e3bca8 commit 523f5c2

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

matplotview/_transform_renderer.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,76 @@ def _draw_text_as_path(
251251
# checked above... (Above case causes error)
252252
super()._draw_text_as_path(gc, x, y, s, prop, angle, ismath)
253253

254+
def draw_markers(
255+
self,
256+
gc,
257+
marker_path,
258+
marker_trans,
259+
path,
260+
trans,
261+
rgbFace = None,
262+
):
263+
# If the markers need to be scaled accurately (such as in log scale), just use the fallback as each will need
264+
# to be scaled separately.
265+
if(self.__scale_widths):
266+
super().draw_markers(gc, marker_path, marker_trans, path, trans, rgbFace)
267+
return
268+
269+
# Otherwise we transform just the marker offsets (not the marker patch), so they stay the same size.
270+
path = path.deepcopy()
271+
path.vertices = self._get_transfer_transform(trans).transform(path.vertices)
272+
bbox = self._get_axes_display_box()
273+
274+
# Change the clip to the sub-axes box
275+
gc.set_clip_rectangle(bbox)
276+
if (not isinstance(self.__bounding_axes.patch, Rectangle)):
277+
gc.set_clip_path(TransformedPatchPath(self.__bounding_axes.patch))
278+
279+
rgbFace = tuple(rgbFace) if (rgbFace is not None) else None
280+
self.__renderer.draw_markers(gc, marker_path, marker_trans, path, IdentityTransform(), rgbFace)
281+
282+
def draw_path_collection(
283+
self,
284+
gc,
285+
master_transform,
286+
paths,
287+
all_transforms,
288+
offsets,
289+
offset_trans,
290+
facecolors,
291+
edgecolors,
292+
linewidths,
293+
linestyles,
294+
antialiaseds,
295+
urls,
296+
offset_position,
297+
):
298+
# If we want accurate scaling for each marker (such as in log scale), just use superclass implementation...
299+
if(self.__scale_widths):
300+
super().draw_path_collection(
301+
gc, master_transform, paths, all_transforms, offsets, offset_trans, facecolors,
302+
edgecolors, linewidths, linestyles, antialiaseds, urls, offset_position
303+
)
304+
return
305+
306+
# Otherwise we transform just the offsets, and pass them to the backend.
307+
print(offsets)
308+
if(np.any(np.isnan(offsets))):
309+
raise ValueError("???")
310+
offsets = self._get_transfer_transform(offset_trans).transform(offsets)
311+
print(offsets)
312+
bbox = self._get_axes_display_box()
313+
314+
# Change the clip to the sub-axes box
315+
gc.set_clip_rectangle(bbox)
316+
if (not isinstance(self.__bounding_axes.patch, Rectangle)):
317+
gc.set_clip_path(TransformedPatchPath(self.__bounding_axes.patch))
318+
319+
self.__renderer.draw_path_collection(
320+
gc, master_transform, paths, all_transforms, offsets, IdentityTransform(), facecolors,
321+
edgecolors, linewidths, linestyles, antialiaseds, urls, None
322+
)
323+
254324
def draw_gouraud_triangle(
255325
self,
256326
gc: GraphicsContextBase,

matplotview/tests/test_view_rendering.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,76 @@ def test_stop_viewing(fig_test, fig_ref):
240240

241241
ax1_ref.plot(data)
242242
ax1_ref.text(0.5, 0.5, "Hello")
243+
244+
245+
@check_figures_equal()
246+
def test_log_line(fig_test, fig_ref):
247+
data = [i for i in range(10)]
248+
249+
# Test case... Create a view and stop it...
250+
ax1_test, ax2_test = fig_test.subplots(1, 2)
251+
252+
ax1_test.set(xscale="log", yscale="log")
253+
ax1_test.plot(data, "-o")
254+
255+
view(ax2_test, ax1_test, scale_lines=False)
256+
ax2_test.set_xlim(-1, 10)
257+
ax2_test.set_ylim(-1, 10)
258+
259+
# Reference, just don't plot anything at all in the second axes...
260+
ax1_ref, ax2_ref = fig_ref.subplots(1, 2)
261+
262+
ax1_ref.set(xscale="log", yscale="log")
263+
ax1_ref.plot(data, "-o")
264+
ax2_ref.plot(data, "-o")
265+
ax2_ref.set_xlim(-1, 10)
266+
ax2_ref.set_ylim(-1, 10)
267+
268+
269+
@check_figures_equal()
270+
def test_log_scatter(fig_test, fig_ref):
271+
data = [i for i in range(1, 11)]
272+
273+
# Test case... Create a view and stop it...
274+
ax1_test, ax2_test = fig_test.subplots(1, 2)
275+
276+
ax1_test.set(xscale="log", yscale="log")
277+
ax1_test.scatter(data, data)
278+
279+
view(ax2_test, ax1_test, scale_lines=False)
280+
ax2_test.set_xlim(-5, 15)
281+
ax2_test.set_ylim(-5, 15)
282+
283+
# Reference, just don't plot anything at all in the second axes...
284+
ax1_ref, ax2_ref = fig_ref.subplots(1, 2)
285+
286+
ax1_ref.set(xscale="log", yscale="log")
287+
ax1_ref.scatter(data, data)
288+
ax2_ref.scatter(data, data)
289+
ax2_ref.set_xlim(-5, 15)
290+
ax2_ref.set_ylim(-5, 15)
291+
292+
293+
@check_figures_equal()
294+
def test_log_scatter_with_colors(fig_test, fig_ref):
295+
data = [i for i in range(1, 11)]
296+
colors = list("rgbrgbrgbr")
297+
298+
# Test case... Create a view and stop it...
299+
ax1_test, ax2_test = fig_test.subplots(1, 2)
300+
301+
ax1_test.set(xscale="log", yscale="log")
302+
ax1_test.scatter(data, data, color=colors)
303+
304+
view(ax2_test, ax1_test, scale_lines=False)
305+
ax2_test.set_xlim(-5, 15)
306+
ax2_test.set_ylim(-5, 15)
307+
308+
# Reference, just don't plot anything at all in the second axes...
309+
ax1_ref, ax2_ref = fig_ref.subplots(1, 2)
310+
311+
ax1_ref.set(xscale="log", yscale="log")
312+
ax1_ref.scatter(data, data, color=colors)
313+
ax2_ref.scatter(data, data, color=colors)
314+
ax2_ref.set_xlim(-5, 15)
315+
ax2_ref.set_ylim(-5, 15)

0 commit comments

Comments
 (0)