Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BUGFIX: use true bbox for rasters in backend_mixed#17182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
brunobeltran wants to merge1 commit intomatplotlib:main
base:main
Choose a base branch
Loading
frombrunobeltran:colorbar-rounding-fix
Draft
Show file tree
Hide file tree
Changes fromall 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
111 changes: 96 additions & 15 deletionslib/matplotlib/backends/backend_mixed.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
import numpy as np

from matplotlib import cbook
from .backend_agg import RendererAgg
from matplotlib._tight_bbox import process_figure_for_rasterizing
from matplotlib.backends.backend_agg import RendererAgg
from matplotlib.transforms import Bbox, Affine2D, IdentityTransform


class MixedModeRenderer:
Expand DownExpand Up@@ -68,6 +69,71 @@ def __getattr__(self, attr):
# to the underlying C implementation).
return getattr(self._renderer, attr)

# need to wrap each drawing function that might be called on the rasterized
# version of the renderer to save what the "true" bbox is for scaling the
# output correctly
# the functions we might want to overwrite are:
# `draw_path`, `draw_image`, `draw_gouraud_triangle`, `draw_text`,
# `draw_markers`, `draw_path_collection`, `draw_quad_mesh`

def _update_true_bbox(self, bbox, transform=None):
"""Convert to real units and update"""
if transform is None:
transform = IdentityTransform()
bbox = bbox.transformed(transform + Affine2D().scale(
self._figdpi / self.dpi))
if self._true_bbox is None:
self._true_bbox = bbox
else:
self._true_bbox = Bbox.union([self._true_bbox, bbox])

def draw_path(self, gc, path, transform, rgbFace=None):
if self._rasterizing > 0:
bbox = Bbox.null()
bbox.update_from_path(path, ignore=True)
self._update_true_bbox(bbox, transform)
return self._renderer.draw_path(gc, path, transform, rgbFace)

def draw_path_collection(self, gc, master_transform, paths, all_transforms,
offsets, offsetTrans, facecolors, edgecolors,
linewidths, linestyles, antialiaseds, urls,
offset_position):
if self._rasterizing > 0:
bbox = Bbox.null()
# TODO probably faster to merge all coordinates from path using
# numpy for large lists of paths, such as the one produced by the
# test case tests/test_backed_pgf.py:test_mixed_mode
for path in paths:
bbox.update_from_path(path, ignore=False)
self._update_true_bbox(bbox, master_transform)
return self._renderer.draw_path_collection(
gc, master_transform, paths, all_transforms, offsets,
offsetTrans, facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position)

def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,
coordinates, offsets, offsetTrans, facecolors,
antialiased, edgecolors):
if self._rasterizing > 0:
# TODO should check if this is always Bbox.unit for efficiency
bbox = Bbox.null()
cshape = coordinates.shape
flat_coords = coordinates.reshape((cshape[0]*cshape[1], cshape[2]))
bbox.update_from_data_xy(flat_coords, ignore=True)
self._update_true_bbox(bbox, master_transform)

return self._renderer.draw_quad_mesh(
gc, master_transform, meshWidth, meshHeight, coordinates,
offsets, offsetTrans, facecolors, antialiased, edgecolors)

def draw_gouraud_triangle(self, gc, points, colors, transform):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This should probably bedraw_gouraud_triangles:

@_api.deprecated("3.7",alternative="draw_gouraud_triangles")
defdraw_gouraud_triangle(self,gc,points,colors,transform):

https://matplotlib.org/stable/api/prev_api_changes/api_changes_3.7.0.html#draw-gouraud-triangle

if self._rasterizing > 0:
bbox = Bbox.null()
bbox.update_from_data_xy(points, ignore=True)
self._update_true_bbox(bbox, transform)
return self._renderer.draw_gouraud_triangle(
gc, points, colors, transform)

def start_rasterizing(self):
"""
Enter "raster" mode. All subsequent drawing commands (until
Expand All@@ -83,6 +149,7 @@ def start_rasterizing(self):
self._raster_renderer = self._raster_renderer_class(
self._width*self.dpi, self._height*self.dpi, self.dpi)
self._renderer = self._raster_renderer
self._true_bbox = None

def stop_rasterizing(self):
"""
Expand All@@ -92,21 +159,35 @@ def stop_rasterizing(self):
"""

self._renderer = self._vector_renderer

height = self._height * self.dpi
img = np.asarray(self._raster_renderer.buffer_rgba())
slice_y, slice_x = cbook._get_nonzero_slices(img[..., 3])
cropped_img = img[slice_y, slice_x]
if cropped_img.size:
gc = self._renderer.new_gc()
# TODO: If the mixedmode resolution differs from the figure's
# dpi, the image must be scaled (dpi->_figdpi). Not all
# backends support this.
self._renderer.draw_image(
gc,
slice_x.start * self._figdpi / self.dpi,
(height - slice_y.stop) * self._figdpi / self.dpi,
cropped_img[::-1])
# these bounds are in pixels, relative to the figure when pixelated
# at the requested DPI. However, the vectorized backends draw at a
# fixed DPI of 72, and typically aren't snapped to the
# requested-DPI pixel grid, so we have to grab the actual bounds to
# put the image into some other way
if self._true_bbox is not None:
# raise NotImplementedError(
# "Something was drawn using a method not wrapped by "
# "MixedModeRenderer.")
img = np.asarray(self._raster_renderer.buffer_rgba())
slice_y, slice_x = cbook._get_nonzero_slices(img[..., 3])
cropped_img = img[slice_y, slice_x]
if cropped_img.size:
gc = self._renderer.new_gc()
# TODO: If the mixedmode resolution differs from the figure's
# dpi, the image must be scaled (dpi->_figdpi). Not all
# backends support this.
# because rasterizing will have rounded size to nearest
# pixel, we need to rescale our drawing to fit the original
# intended Bbox. This results in a slightly different DPI than
# requested, but that's better than the drawing not fitting
# into the space requested, see Issue #6827

self._renderer.draw_image(
gc, self._true_bbox.x0, self._true_bbox.y0, cropped_img[::-1],
true_size=(self._true_bbox.width, self._true_bbox.height)
)

self._raster_renderer = None

# restore the figure dpi.
Expand Down
13 changes: 10 additions & 3 deletionslib/matplotlib/backends/backend_pdf.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1980,21 +1980,28 @@ def check_gc(self, gc, fillcolor=None):
def get_image_magnification(self):
return self.image_dpi/72.0

def draw_image(self, gc, x, y, im, transform=None):
def option_true_bbox_image(self):
return True

def draw_image(self, gc, x, y, im, transform=None, true_size=None):
# docstring inherited

h, w = im.shape[:2]
if w == 0 or h == 0:
return

if true_size is not None:
w, h = true_size

if transform is None:
# If there's no transform, alpha has already been applied
gc.set_alpha(1.0)

self.check_gc(gc)

w = 72.0 * w / self.image_dpi
h = 72.0 * h / self.image_dpi
if true_size is None:
w = 72.0 * w / self.image_dpi
h = 72.0 * h / self.image_dpi

imob = self.file.imageObject(im)

Expand Down
8 changes: 7 additions & 1 deletionlib/matplotlib/backends/backend_pgf.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -605,13 +605,19 @@ def option_image_nocomposite(self):
# docstring inherited
return not mpl.rcParams['image.composite_image']

def draw_image(self, gc, x, y, im, transform=None):
def option_true_bbox_image(self):
return True

def draw_image(self, gc, x, y, im, transform=None, true_size=None):
# docstring inherited

h, w = im.shape[:2]
if w == 0 or h == 0:
return

if true_size is not None:
w, h = true_size

if not os.path.exists(getattr(self.fh, "name", "")):
raise ValueError(
"streamed pgf-code does not support raster graphics, consider "
Expand Down
23 changes: 17 additions & 6 deletionslib/matplotlib/backends/backend_ps.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -424,30 +424,41 @@ def _get_clip_cmd(self, gc):
clip.append(f"{custom_clip_cmd}\n")
return "".join(clip)

def option_true_bbox_image(self):
return True

@_log_if_debug_on
def draw_image(self, gc, x, y, im, transform=None):
def draw_image(self, gc, x, y, im, transform=None, true_size=None):
# docstring inherited

h, w = im.shape[:2]

if h == 0 or w == 0:
return

imagecmd = "false 3 colorimage"
data = im[::-1, :, :3] # Vertically flipped rgb values.
hexdata = data.tobytes().hex("\n", -64) # Linewrap to 128 chars.

if transform is None:
matrix = "1 0 0 1 0 0"
xscale = w / self.image_magnification
yscale = h / self.image_magnification
if true_size is None:
xscale = w / self.image_magnification
yscale = h / self.image_magnification
else:
xscale = true_size[0]
yscale = true_size[1]
else:
matrix = " ".join(map(str, transform.frozen().to_values()))
xscale = 1.0
yscale = 1.0
matrix = " ".join(map(str, transform.frozen().to_values()))

self._pswriter.write(f"""\
gsave
{self._get_clip_cmd(gc)}
{x:g} {y:g} translate
{x:.2f} {y:.2f} translate
[{matrix}] concat
{xscale:g} {yscale:g} scale
{xscale:.2f} {yscale:.2f} scale
/DataString {w:d} string def
{w:d} {h:d} 8 [ {w:d} 0 0 -{h:d} 0 {h:d} ]
{{
Expand Down
25 changes: 22 additions & 3 deletionslib/matplotlib/backends/backend_svg.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -915,10 +915,13 @@ def option_scale_image(self):
# docstring inherited
return True

def option_true_bbox_image(self):
return True

def get_image_magnification(self):
return self.image_dpi / 72.0

def draw_image(self, gc, x, y, im, transform=None):
def draw_image(self, gc, x, y, im, transform=None, true_size=None):
# docstring inherited

h, w = im.shape[:2]
Expand DownExpand Up@@ -960,12 +963,28 @@ def draw_image(self, gc, x, y, im, transform=None):
w = 72.0 * w / self.image_dpi
h = 72.0 * h / self.image_dpi

if true_size is not None:
width, height = true_size
# because rasterization happens only for integer pixels, the
# round-trip width w = # int(width/72*image_dpi)*72/image_dpi
# need not match the "real" width
scale_x = width/w
scale_y = height/h
real_h = height
else:
scale_x = 1
scale_y = 1
real_h = h

self.writer.element(
'image',
transform=_generate_transform([
('scale', (1, -1)), ('translate', (0, -h))]),
('translate',
(x*(1 - scale_x), y*(1 - scale_y) + real_h)),
('scale', (scale_x, -scale_y))
]),
x=_short_float_fmt(x),
y=_short_float_fmt(-(self.height - y -h)),
y=_short_float_fmt(-(self.height - y -real_h)),
width=_short_float_fmt(w), height=_short_float_fmt(h),
attrib=attrib)
else:
Expand Down
Binary file modifiedlib/matplotlib/tests/baseline_images/test_axes/hist2d.pdf
View file
Open in desktop
Binary file not shown.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp