<div dir="ltr"><div dir="ltr"><div>If you're okay with a fixed figure size, you can use plt.figaspect to set this up fairly easily: (i.e. this will be incorrect as soon as you resize the plot window)</div><div style="margin-left:40px"><br></div><div><pre class="gmail-lang-py gmail-prettyprint gmail-prettyprinted" style="margin-left:40px"><code><span class="gmail-kwd">import</span><span class="gmail-pln"> numpy </span><span class="gmail-kwd">as</span><span class="gmail-pln"> np
</span><span class="gmail-kwd">import</span><span class="gmail-pln"> matplotlib</span><span class="gmail-pun">.</span><span class="gmail-pln">pyplot </span><span class="gmail-kwd">as</span><span class="gmail-pln"> plt
nrows</span><span class="gmail-pun">,</span><span class="gmail-pln"> ncols </span><span class="gmail-pun">=</span><span class="gmail-pln"> </span><span class="gmail-lit">8</span><span class="gmail-pun">,</span><span class="gmail-pln"> </span><span class="gmail-lit">12</span><span class="gmail-pln">
dx</span><span class="gmail-pun">,</span><span class="gmail-pln"> dy </span><span class="gmail-pun">=</span><span class="gmail-pln"> </span><span class="gmail-lit">1</span><span class="gmail-pun">,</span><span class="gmail-pln"> </span><span class="gmail-lit">2</span><span class="gmail-pln">
figsize </span><span class="gmail-pun">=</span><span class="gmail-pln"> plt</span><span class="gmail-pun">.</span><span class="gmail-pln">figaspect</span><span class="gmail-pun">(</span><span class="gmail-pln">float</span><span class="gmail-pun">(</span><span class="gmail-pln">dy </span><span class="gmail-pun">*</span><span class="gmail-pln"> nrows</span><span class="gmail-pun">)</span><span class="gmail-pln"> </span><span class="gmail-pun">/</span><span class="gmail-pln"> float</span><span class="gmail-pun">(</span><span class="gmail-pln">dx </span><span class="gmail-pun">*</span><span class="gmail-pln"> ncols</span><span class="gmail-pun">))</span><span class="gmail-pln">
fig</span><span class="gmail-pun">,</span><span class="gmail-pln"> axes </span><span class="gmail-pun">=</span><span class="gmail-pln"> plt</span><span class="gmail-pun">.</span><span class="gmail-pln">subplots</span><span class="gmail-pun">(</span><span class="gmail-pln">nrows</span><span class="gmail-pun">,</span><span class="gmail-pln"> ncols</span><span class="gmail-pun">,</span><span class="gmail-pln"> figsize</span><span class="gmail-pun">=</span><span class="gmail-pln">figsize</span><span class="gmail-pun">)</span><span class="gmail-pln">
</span><span class="gmail-kwd">for</span><span class="gmail-pln"> ax </span><span class="gmail-kwd">in</span><span class="gmail-pln"> axes</span><span class="gmail-pun">.</span><span class="gmail-pln">flat</span><span class="gmail-pun">:</span><span class="gmail-pln">
data </span><span class="gmail-pun">=</span><span class="gmail-pln"> np</span><span class="gmail-pun">.</span><span class="gmail-pln">random</span><span class="gmail-pun">.</span><span class="gmail-pln">random</span><span class="gmail-pun">((</span><span class="gmail-lit">10</span><span class="gmail-pun">*</span><span class="gmail-pln">dy</span><span class="gmail-pun">,</span><span class="gmail-pln"> </span><span class="gmail-lit">10</span><span class="gmail-pun">*</span><span class="gmail-pln">dx</span><span class="gmail-pun">))</span><span class="gmail-pln">
ax</span><span class="gmail-pun">.</span><span class="gmail-pln">imshow</span><span class="gmail-pun">(</span><span class="gmail-pln">data</span><span class="gmail-pun">,</span><span class="gmail-pln"> interpolation</span><span class="gmail-pun">=</span><span class="gmail-str">'none'</span><span class="gmail-pun">,</span><span class="gmail-pln"> cmap</span><span class="gmail-pun">=</span><span class="gmail-str">'gray'</span><span class="gmail-pun">)</span><span class="gmail-pln">
ax</span><span class="gmail-pun">.</span><span class="gmail-pln">set</span><span class="gmail-pun">(</span><span class="gmail-pln">xticks</span><span class="gmail-pun">=[],</span><span class="gmail-pln"> yticks</span><span class="gmail-pun">=[])</span><span class="gmail-pln">
pad </span><span class="gmail-pun">=</span><span class="gmail-pln"> </span><span class="gmail-lit">0.05</span><span class="gmail-pln"> </span><span class="gmail-com"># Padding around the edge of the figure</span><span class="gmail-pln">
xpad</span><span class="gmail-pun">,</span><span class="gmail-pln"> ypad </span><span class="gmail-pun">=</span><span class="gmail-pln"> dx </span><span class="gmail-pun">*</span><span class="gmail-pln"> pad</span><span class="gmail-pun">,</span><span class="gmail-pln"> dy </span><span class="gmail-pun">*</span><span class="gmail-pln"> pad
fig</span><span class="gmail-pun">.</span><span class="gmail-pln">subplots_adjust</span><span class="gmail-pun">(</span><span class="gmail-pln">left</span><span class="gmail-pun">=</span><span class="gmail-pln">xpad</span><span class="gmail-pun">,</span><span class="gmail-pln"> right</span><span class="gmail-pun">=</span><span class="gmail-lit">1</span><span class="gmail-pun">-</span><span class="gmail-pln">xpad</span><span class="gmail-pun">,</span><span class="gmail-pln"> top</span><span class="gmail-pun">=</span><span class="gmail-lit">1</span><span class="gmail-pun">-</span><span class="gmail-pln">ypad</span><span class="gmail-pun">,</span><span class="gmail-pln"> bottom</span><span class="gmail-pun">=</span><span class="gmail-pln">ypad</span><span class="gmail-pun">)</span><span class="gmail-pln">
plt</span><span class="gmail-pun">.</span><span class="gmail-pln">show</span><span class="gmail-pun">()</span></code><br></pre><pre class="gmail-lang-py gmail-prettyprint gmail-prettyprinted"><font face="arial,helvetica,sans-serif">(From here, for reference: <a href="https://stackoverflow.com/questions/32633322/changing-aspect-ratio-of-subplots-in-matplotlib/32635933#32635933">https://stackoverflow.com/questions/32633322/changing-aspect-ratio-of-subplots-in-matplotlib/32635933#32635933</a> )</font><br></pre><pre class="gmail-lang-py gmail-prettyprint gmail-prettyprinted"><span style="font-family:arial,helvetica,sans-serif">If you need the spacing to remain constant as the figure size changes, it's possible, but more complex. You may find it's easiest to use draw callbacks in that case.<br></span></pre><pre class="gmail-lang-py gmail-prettyprint gmail-prettyprinted"><span style="font-family:arial,helvetica,sans-serif">Hope that helps,<br></span></pre><pre class="gmail-lang-py gmail-prettyprint gmail-prettyprinted"><span style="font-family:arial,helvetica,sans-serif">-Joe<br></span></pre></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Oct 30, 2018 at 4:44 PM Daniele Nicolodi <<a href="mailto:daniele@grinta.net">daniele@grinta.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I would like to arrange a number of square subplots (as in subplots with<br>
square axes as obtained with `ax.axis('equal')`) in a grid such that the<br>
distance between rows and columns is exactly the same.<br>
<br>
I can use a GridSpec but, as soon as I ask for the axes to be square,<br>
the distance between axes changes. Is there a way to enforce the equal<br>
distances constrain (namely stretch the padding between the axes and the<br>
margin of the figure but do not stretch the padding between axes) or the<br>
only solution is to do it manually?<br>
<br>
Thank you in advance.<br>
<br>
Best,<br>
Dan<br>
_______________________________________________<br>
Matplotlib-users mailing list<br>
<a href="mailto:Matplotlib-users@python.org" target="_blank">Matplotlib-users@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/matplotlib-users" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/matplotlib-users</a><br>
</blockquote></div>