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

Skip to content

Commit 1d6cc11

Browse files
authored
Merge pull request #15992 from anntzer/matless
Avoid using np.matrix.
2 parents a070ad5 + c751f33 commit 1d6cc11

File tree

3 files changed

+49
-33
lines changed

3 files changed

+49
-33
lines changed

examples/axes_grid1/demo_axes_hbox_divider.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
Hbox Divider to arrange subplots.
77
"""
8+
89
import numpy as np
910
import matplotlib.pyplot as plt
1011
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
@@ -13,19 +14,12 @@
1314

1415
def make_heights_equal(fig, rect, ax1, ax2, pad):
1516
# pad in inches
16-
17-
h1, v1 = Size.AxesX(ax1), Size.AxesY(ax1)
18-
h2, v2 = Size.AxesX(ax2), Size.AxesY(ax2)
19-
20-
pad_v = Size.Scaled(1)
21-
pad_h = Size.Fixed(pad)
22-
23-
my_divider = HBoxDivider(fig, rect,
24-
horizontal=[h1, pad_h, h2],
25-
vertical=[v1, pad_v, v2])
26-
27-
ax1.set_axes_locator(my_divider.new_locator(0))
28-
ax2.set_axes_locator(my_divider.new_locator(2))
17+
divider = HBoxDivider(
18+
fig, rect,
19+
horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)],
20+
vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)])
21+
ax1.set_axes_locator(divider.new_locator(0))
22+
ax2.set_axes_locator(divider.new_locator(2))
2923

3024

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

40-
rect = 111 # subplot param for combined axes
41-
make_heights_equal(fig, rect, ax1, ax2, pad=0.5) # pad in inches
42-
43-
for ax in [ax1, ax2]:
44-
ax.locator_params(nbins=4)
34+
pad = 0.5 # inches.
35+
divider = HBoxDivider(
36+
fig, 111, # Position of combined axes.
37+
horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)],
38+
vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)])
39+
ax1.set_axes_locator(divider.new_locator(0))
40+
ax2.set_axes_locator(divider.new_locator(2))
4541

4642
# annotate
4743
ax3 = plt.axes([0.5, 0.5, 0.001, 0.001], frameon=False)

lib/mpl_toolkits/axes_grid1/axes_divider.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -597,26 +597,22 @@ def _determine_karray(equivalent_sizes, appended_sizes,
597597
total_appended_size):
598598

599599
n = len(equivalent_sizes)
600-
A = np.mat(np.zeros((n + 1, n + 1)))
600+
eq_rs, eq_as = np.asarray(equivalent_sizes).T
601+
ap_rs, ap_as = np.asarray(appended_sizes).T
602+
A = np.zeros((n + 1, n + 1))
601603
B = np.zeros(n + 1)
602-
# AxK = B
604+
np.fill_diagonal(A[:n, :n], eq_rs)
605+
A[:n, -1] = -1
606+
A[-1, :-1] = ap_rs
607+
B[:n] = -eq_as
608+
B[-1] = total_appended_size - sum(ap_as)
603609

604-
# populated A
605-
for i, (r, a) in enumerate(equivalent_sizes):
606-
A[i, i] = r
607-
A[i, -1] = -1
608-
B[i] = -a
609-
A[-1, :-1] = [r for r, a in appended_sizes]
610-
B[-1] = total_appended_size - sum([a for rs, a in appended_sizes])
611-
612-
karray_H = (A.I*np.mat(B).T).A1
610+
karray_H = np.linalg.solve(A, B) # A @ K = B
613611
karray = karray_H[:-1]
614612
H = karray_H[-1]
615613

616614
if H > max_equivalent_size:
617-
karray = ((max_equivalent_size -
618-
np.array([a for r, a in equivalent_sizes]))
619-
/ np.array([r for r, a in equivalent_sizes]))
615+
karray = (max_equivalent_size - eq_as) / eq_rs
620616
return karray
621617

622618
@staticmethod

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
image_comparison, remove_ticks_and_titles)
1313

1414
from mpl_toolkits.axes_grid1 import (
15-
host_subplot, make_axes_locatable, AxesGrid, ImageGrid)
15+
axes_size as Size, host_subplot, make_axes_locatable, AxesGrid, ImageGrid)
1616
from mpl_toolkits.axes_grid1.anchored_artists import (
1717
AnchoredSizeBar, AnchoredDirectionArrows)
18+
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
1819
from mpl_toolkits.axes_grid1.inset_locator import (
1920
zoomed_inset_axes, mark_inset, inset_axes, BboxConnectorPatch)
2021

@@ -461,3 +462,26 @@ def on_pick(event):
461462
assert big in event_rects
462463
if click_on == "small":
463464
assert small in event_rects
465+
466+
467+
def test_hbox_divider():
468+
arr1 = np.arange(20).reshape((4, 5))
469+
arr2 = np.arange(20).reshape((5, 4))
470+
471+
fig, (ax1, ax2) = plt.subplots(1, 2)
472+
ax1.imshow(arr1)
473+
ax2.imshow(arr2)
474+
475+
pad = 0.5 # inches.
476+
divider = HBoxDivider(
477+
fig, 111, # Position of combined axes.
478+
horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)],
479+
vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)])
480+
ax1.set_axes_locator(divider.new_locator(0))
481+
ax2.set_axes_locator(divider.new_locator(2))
482+
483+
fig.canvas.draw()
484+
p1 = ax1.get_position()
485+
p2 = ax2.get_position()
486+
assert p1.height == p2.height
487+
assert p2.width / p1.width == pytest.approx((4 / 5) ** 2)

0 commit comments

Comments
 (0)