Skip to content

Commit 546228f

Browse files
authored
Merge pull request #13614 from jklymak/fix-polar-get-window-extent
Fix polar get window extent
2 parents eb6eac1 + e297810 commit 546228f

File tree

3 files changed

+44
-41
lines changed

3 files changed

+44
-41
lines changed

examples/lines_bars_and_markers/markevery_demo.py

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,57 +36,53 @@
3636
# define the figure size and grid layout properties
3737
figsize = (10, 8)
3838
cols = 3
39-
gs = gridspec.GridSpec(len(cases) // cols + 1, cols)
40-
gs.update(hspace=0.4)
39+
rows = len(cases) // cols + 1
4140
# define the data for cartesian plots
4241
delta = 0.11
4342
x = np.linspace(0, 10 - 2 * delta, 200) + delta
4443
y = np.sin(x) + 1.0 + delta
4544

45+
46+
def trim_axs(axs, N):
47+
"""little helper to massage the axs list to have correct length..."""
48+
axs = axs.flat
49+
for ax in axs[N:]:
50+
ax.remove()
51+
return axs[:N]
52+
4653
###############################################################################
4754
# Plot each markevery case for linear x and y scales
4855

49-
fig1 = plt.figure(num=1, figsize=figsize)
50-
ax = []
51-
for i, case in enumerate(cases):
52-
row = (i // cols)
53-
col = i % cols
54-
ax.append(fig1.add_subplot(gs[row, col]))
55-
ax[-1].set_title('markevery=%s' % str(case))
56-
ax[-1].plot(x, y, 'o', ls='-', ms=4, markevery=case)
56+
fig1, axs = plt.subplots(rows, cols, figsize=figsize, constrained_layout=True)
57+
axs = trim_axs(axs, len(cases))
58+
for ax, case in zip(axs, cases):
59+
ax.set_title('markevery=%s' % str(case))
60+
ax.plot(x, y, 'o', ls='-', ms=4, markevery=case)
5761

5862
###############################################################################
5963
# Plot each markevery case for log x and y scales
6064

61-
fig2 = plt.figure(num=2, figsize=figsize)
62-
axlog = []
63-
for i, case in enumerate(cases):
64-
row = (i // cols)
65-
col = i % cols
66-
axlog.append(fig2.add_subplot(gs[row, col]))
67-
axlog[-1].set_title('markevery=%s' % str(case))
68-
axlog[-1].set_xscale('log')
69-
axlog[-1].set_yscale('log')
70-
axlog[-1].plot(x, y, 'o', ls='-', ms=4, markevery=case)
71-
fig2.tight_layout()
65+
fig2, axs = plt.subplots(rows, cols, figsize=figsize, constrained_layout=True)
66+
axs = trim_axs(axs, len(cases))
67+
for ax, case in zip(axs, cases):
68+
ax.set_title('markevery=%s' % str(case))
69+
ax.set_xscale('log')
70+
ax.set_yscale('log')
71+
ax.plot(x, y, 'o', ls='-', ms=4, markevery=case)
7272

7373
###############################################################################
7474
# Plot each markevery case for linear x and y scales but zoomed in
7575
# note the behaviour when zoomed in. When a start marker offset is specified
7676
# it is always interpreted with respect to the first data point which might be
7777
# different to the first visible data point.
7878

79-
fig3 = plt.figure(num=3, figsize=figsize)
80-
axzoom = []
81-
for i, case in enumerate(cases):
82-
row = (i // cols)
83-
col = i % cols
84-
axzoom.append(fig3.add_subplot(gs[row, col]))
85-
axzoom[-1].set_title('markevery=%s' % str(case))
86-
axzoom[-1].plot(x, y, 'o', ls='-', ms=4, markevery=case)
87-
axzoom[-1].set_xlim((6, 6.7))
88-
axzoom[-1].set_ylim((1.1, 1.7))
89-
fig3.tight_layout()
79+
fig3, axs = plt.subplots(rows, cols, figsize=figsize, constrained_layout=True)
80+
axs = trim_axs(axs, len(cases))
81+
for ax, case in zip(axs, cases):
82+
ax.set_title('markevery=%s' % str(case))
83+
ax.plot(x, y, 'o', ls='-', ms=4, markevery=case)
84+
ax.set_xlim((6, 6.7))
85+
ax.set_ylim((1.1, 1.7))
9086

9187
# define data for polar plots
9288
r = np.linspace(0, 3.0, 200)
@@ -95,14 +91,11 @@
9591
###############################################################################
9692
# Plot each markevery case for polar plots
9793

98-
fig4 = plt.figure(num=4, figsize=figsize)
99-
axpolar = []
100-
for i, case in enumerate(cases):
101-
row = (i // cols)
102-
col = i % cols
103-
axpolar.append(fig4.add_subplot(gs[row, col], projection='polar'))
104-
axpolar[-1].set_title('markevery=%s' % str(case))
105-
axpolar[-1].plot(theta, r, 'o', ls='-', ms=4, markevery=case)
106-
fig4.tight_layout()
94+
fig4, axs = plt.subplots(rows, cols, figsize=figsize,
95+
subplot_kw={'projection': 'polar'}, constrained_layout=True)
96+
axs = trim_axs(axs, len(cases))
97+
for ax, case in zip(axs, cases):
98+
ax.set_title('markevery=%s' % str(case))
99+
ax.plot(theta, r, 'o', ls='-', ms=4, markevery=case)
107100

108101
plt.show()

lib/matplotlib/spines.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ def get_window_extent(self, renderer=None):
159159
# correct:
160160
self._adjust_location()
161161
bb = super().get_window_extent(renderer=renderer)
162+
if self.axis is None:
163+
return bb
162164
bboxes = [bb]
163165
tickstocheck = [self.axis.majorTicks[0]]
164166
if len(self.axis.minorTicks) > 1:

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6271,6 +6271,14 @@ def test_minor_accountedfor():
62716271
atol=1e-2)
62726272

62736273

6274+
def test_get_tightbbox_polar():
6275+
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
6276+
fig.canvas.draw()
6277+
bb = ax.get_tightbbox(fig.canvas.get_renderer())
6278+
assert_allclose(bb.extents,
6279+
[107.7778, 29.2778, 539.7847, 450.7222], rtol=1e-03)
6280+
6281+
62746282
@check_figures_equal(extensions=["png"])
62756283
def test_axis_bool_arguments(fig_test, fig_ref):
62766284
# Test if False and "off" give the same

0 commit comments

Comments
 (0)