Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions mplexporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"""
import warnings
import io
import numpy as np
from . import utils

import matplotlib
from matplotlib import transforms, collections
from matplotlib import transforms, collections, path as mpath
from matplotlib.backends.backend_agg import FigureCanvasAgg

class Exporter(object):
Expand Down Expand Up @@ -235,7 +236,7 @@ def draw_collection(self, ax, collection,
force_offsettrans=None):
"""Process a matplotlib collection and call renderer.draw_collection"""
(transform, transOffset,
offsets, paths) = collection._prepare_points()
offsets, paths) = _collections_prepare_points(collection, ax)

offset_coords, offsets = self.process_transform(
transOffset, ax, offsets, force_trans=force_offsettrans)
Expand Down Expand Up @@ -279,3 +280,52 @@ def draw_image(self, ax, image):
style={"alpha": image.get_alpha(),
"zorder": image.get_zorder()},
mplobj=image)


def _collections_prepare_points(self, ax):
# Code is from mpl.collections._prepare_points, but we preserves data-space
# vertices/offsets whenever a non-affine ax.transData branch is involved
# (log scales, semilog, etc.) ; that's the `contains_branch` test.
# As a result, fill_between, scatter, and other PathCollections on log axes
# export with pathcoordinates/offsetcoordinates == 'data', so the rendered
# polygons obey zooming/panning.
#
# The first arg is called self, to keep code close to original.
transform = self.get_transform()
transOffset = self.get_offset_transform()
offsets = self.get_offsets()
paths = self.get_paths()

if self.have_units():
paths = []
for path in self.get_paths():
vertices = path.vertices
xs, ys = vertices[:, 0], vertices[:, 1]
xs = self.convert_xunits(xs)
ys = self.convert_yunits(ys)
paths.append(mpath.Path(np.column_stack([xs, ys]), path.codes))

if offsets.size:
xs = self.convert_xunits(offsets[:, 0])
ys = self.convert_yunits(offsets[:, 1])
offsets = np.column_stack([xs, ys])

def contains_branch(tr):
return ax is not None and tr.contains_branch(ax.transData)

if not transform.is_affine and not contains_branch(transform):
paths = [transform.transform_path_non_affine(path)
for path in paths]
transform = transform.get_affine()

if not transOffset.is_affine and not contains_branch(transOffset):
offsets = transOffset.transform_non_affine(offsets)
# This might have changed an ndarray into a masked array.
transOffset = transOffset.get_affine()

if isinstance(offsets, np.ma.MaskedArray):
offsets = offsets.filled(np.nan)
# Changing from a masked array to nan-filled ndarray
# is probably most efficient at this point.

return transform, transOffset, offsets, paths