Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Avoid using np.matrix. #15992

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
Dec 22, 2019
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
32 changes: 14 additions & 18 deletions examples/axes_grid1/demo_axes_hbox_divider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

Hbox Divider to arrange subplots.
"""

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
Expand All @@ -13,19 +14,12 @@

def make_heights_equal(fig, rect, ax1, ax2, pad):
# pad in inches

h1, v1 = Size.AxesX(ax1), Size.AxesY(ax1)
h2, v2 = Size.AxesX(ax2), Size.AxesY(ax2)

pad_v = Size.Scaled(1)
pad_h = Size.Fixed(pad)

my_divider = HBoxDivider(fig, rect,
horizontal=[h1, pad_h, h2],
vertical=[v1, pad_v, v2])

ax1.set_axes_locator(my_divider.new_locator(0))
ax2.set_axes_locator(my_divider.new_locator(2))
divider = HBoxDivider(
fig, rect,
horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)],
vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)])
ax1.set_axes_locator(divider.new_locator(0))
ax2.set_axes_locator(divider.new_locator(2))


if __name__ == "__main__":
Expand All @@ -37,11 +31,13 @@ def make_heights_equal(fig, rect, ax1, ax2, pad):
ax1.imshow(arr1)
ax2.imshow(arr2)

rect = 111 # subplot param for combined axes
make_heights_equal(fig, rect, ax1, ax2, pad=0.5) # pad in inches

for ax in [ax1, ax2]:
ax.locator_params(nbins=4)
pad = 0.5 # inches.
divider = HBoxDivider(
fig, 111, # Position of combined axes.
horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)],
vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)])
ax1.set_axes_locator(divider.new_locator(0))
ax2.set_axes_locator(divider.new_locator(2))

# annotate
ax3 = plt.axes([0.5, 0.5, 0.001, 0.001], frameon=False)
Expand Down
24 changes: 10 additions & 14 deletions lib/mpl_toolkits/axes_grid1/axes_divider.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,26 +597,22 @@ def _determine_karray(equivalent_sizes, appended_sizes,
total_appended_size):

n = len(equivalent_sizes)
A = np.mat(np.zeros((n + 1, n + 1)))
eq_rs, eq_as = np.asarray(equivalent_sizes).T
ap_rs, ap_as = np.asarray(appended_sizes).T
A = np.zeros((n + 1, n + 1))
B = np.zeros(n + 1)
# AxK = B
np.fill_diagonal(A[:n, :n], eq_rs)
A[:n, -1] = -1
A[-1, :-1] = ap_rs
B[:n] = -eq_as
B[-1] = total_appended_size - sum(ap_as)

# populated A
for i, (r, a) in enumerate(equivalent_sizes):
A[i, i] = r
A[i, -1] = -1
B[i] = -a
A[-1, :-1] = [r for r, a in appended_sizes]
B[-1] = total_appended_size - sum([a for rs, a in appended_sizes])

karray_H = (A.I*np.mat(B).T).A1
karray_H = np.linalg.solve(A, B) # A @ K = B
karray = karray_H[:-1]
H = karray_H[-1]

if H > max_equivalent_size:
karray = ((max_equivalent_size -
np.array([a for r, a in equivalent_sizes]))
/ np.array([r for r, a in equivalent_sizes]))
karray = (max_equivalent_size - eq_as) / eq_rs
return karray

@staticmethod
Expand Down
26 changes: 25 additions & 1 deletion lib/mpl_toolkits/tests/test_axes_grid1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
image_comparison, remove_ticks_and_titles)

from mpl_toolkits.axes_grid1 import (
host_subplot, make_axes_locatable, AxesGrid, ImageGrid)
axes_size as Size, host_subplot, make_axes_locatable, AxesGrid, ImageGrid)
from mpl_toolkits.axes_grid1.anchored_artists import (
AnchoredSizeBar, AnchoredDirectionArrows)
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
from mpl_toolkits.axes_grid1.inset_locator import (
zoomed_inset_axes, mark_inset, inset_axes, BboxConnectorPatch)

Expand Down Expand Up @@ -461,3 +462,26 @@ def on_pick(event):
assert big in event_rects
if click_on == "small":
assert small in event_rects


def test_hbox_divider():
arr1 = np.arange(20).reshape((4, 5))
arr2 = np.arange(20).reshape((5, 4))

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(arr1)
ax2.imshow(arr2)

pad = 0.5 # inches.
divider = HBoxDivider(
fig, 111, # Position of combined axes.
horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)],
vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)])
ax1.set_axes_locator(divider.new_locator(0))
ax2.set_axes_locator(divider.new_locator(2))

fig.canvas.draw()
p1 = ax1.get_position()
p2 = ax2.get_position()
assert p1.height == p2.height
assert p2.width / p1.width == pytest.approx((4 / 5) ** 2)