Skip to content

Fixes png showing inconsistent inset_axes position #10756

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
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
16 changes: 10 additions & 6 deletions lib/mpl_toolkits/axes_grid1/inset_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def __call__(self, ax, renderer):
class AnchoredSizeLocator(AnchoredLocatorBase):
def __init__(self, bbox_to_anchor, x_size, y_size, loc,
borderpad=0.5, bbox_transform=None):

super().__init__(
bbox_to_anchor, None, loc,
borderpad=borderpad, bbox_transform=bbox_transform
Expand All @@ -105,24 +104,23 @@ def get_extent(self, renderer):
dpi = renderer.points_to_pixels(72.)

r, a = self.x_size.get_size(renderer)
width = w*r + a*dpi
width = w * r + a * dpi

r, a = self.y_size.get_size(renderer)
height = h*r + a*dpi
height = h * r + a * dpi
xd, yd = 0, 0

fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
pad = self.pad * fontsize

return width+2*pad, height+2*pad, xd+pad, yd+pad
return width + 2 * pad, height + 2 * pad, xd + pad, yd + pad


class AnchoredZoomLocator(AnchoredLocatorBase):
def __init__(self, parent_axes, zoom, loc,
borderpad=0.5,
bbox_to_anchor=None,
bbox_transform=None):

self.parent_axes = parent_axes
self.zoom = zoom

Expand All @@ -141,7 +139,7 @@ def get_extent(self, renderer):
fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
pad = self.pad * fontsize

return abs(w*self.zoom)+2*pad, abs(h*self.zoom)+2*pad, pad, pad
return abs(w * self.zoom) + 2 * pad, abs(h * self.zoom) + 2 * pad, pad, pad


class BboxPatch(Patch):
Expand Down Expand Up @@ -184,6 +182,7 @@ def get_path(self):
Path.CLOSEPOLY]

return Path(verts, codes)

get_path.__doc__ = Patch.get_path.__doc__


Expand Down Expand Up @@ -318,6 +317,7 @@ def __init__(self, bbox1, bbox2, loc1, loc2=None, **kwargs):
def get_path(self):
return self.connect_bbox(self.bbox1, self.bbox2,
self.loc1, self.loc2)

get_path.__doc__ = Patch.get_path.__doc__


Expand Down Expand Up @@ -373,6 +373,7 @@ def get_path(self):
list(path2.vertices) +
[path1.vertices[0]])
return Path(path_merged)

get_path.__doc__ = BboxConnector.get_path.__doc__


Expand Down Expand Up @@ -453,6 +454,9 @@ def inset_axes(parent_axes, width, height, loc=1,
if bbox_to_anchor is None:
bbox_to_anchor = parent_axes.bbox

if bbox_transform is None:
bbox_transform = parent_axes.transAxes

axes_locator = AnchoredSizeLocator(bbox_to_anchor,
width, height,
loc=loc,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 72 additions & 1 deletion lib/mpl_toolkits/tests/test_axes_grid1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from mpl_toolkits.axes_grid1 import host_subplot
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.axes_grid1 import AxesGrid
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
from mpl_toolkits.axes_grid1.inset_locator import (
zoomed_inset_axes,
mark_inset,
inset_axes
)
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar

from matplotlib.colors import LogNorm
Expand Down Expand Up @@ -155,6 +159,73 @@ def get_demo_image():
ax.add_artist(asb)


@image_comparison(
baseline_images=['inset_axes'], style='default', extensions=['png'],
remove_text=True)
def test_inset_axes():
def get_demo_image():
from matplotlib.cbook import get_sample_data
import numpy as np
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
z = np.load(f)
# z is a numpy array of 15x15
return z, (-3, 4, -4, 3)

fig, ax = plt.subplots(figsize=[5, 4])

# prepare the demo image
Z, extent = get_demo_image()
Z2 = np.zeros([150, 150], dtype="d")
ny, nx = Z.shape
Z2[30:30 + ny, 30:30 + nx] = Z

# extent = [-3, 4, -4, 3]
ax.imshow(Z2, extent=extent, interpolation="nearest",
origin="lower")

# creating our inset axes without a bbox_transform parameter
axins = inset_axes(ax, width=1., height=1., bbox_to_anchor=(1, 1))

axins.imshow(Z2, extent=extent, interpolation="nearest",
origin="lower")
axins.yaxis.get_major_locator().set_params(nbins=7)
axins.xaxis.get_major_locator().set_params(nbins=7)
# sub region of the original image
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)

plt.xticks(visible=False)
plt.yticks(visible=False)

# draw a bbox of the region of the inset axes in the parent axes and
# connecting lines between the bbox and the inset axes area
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")

asb = AnchoredSizeBar(ax.transData,
0.5,
'0.5',
loc=8,
pad=0.1, borderpad=0.5, sep=5,
frameon=False)
ax.add_artist(asb)


def test_inset_axes_without_transform_should_use_parent_axes():
# creating our figure
fig = plt.figure(dpi=150)

# gca method gets current axes of the figure
ax = plt.gca()
ax.plot([0.0, 0.25, 0.50, 1.0], [0.1, 0.2, 0.4, 0.9], color='b')

# creating our inset_axes. without a bbox_transform parameter
ax_ins = inset_axes(ax, width=1., height=1., bbox_to_anchor=(1, 1))
ax_ins.plot([0.0, 0.25, 0.50, 1.0], [0.9, 0.4, 0.2, 0.1], color='r')

assert ax.transAxes == ax_ins.transAxes


@image_comparison(baseline_images=['zoomed_axes',
'inverted_zoomed_axes'],
extensions=['png'])
Expand Down