Matplotlib: Constrain plot width while allowing flexible height
<p>What I would like to achive are plots with <strong>equal scale aspect ratio</strong>, and <strong>fixed width</strong>, but a <strong>dynamically chosen height</strong>.</p>

<p>To make this more concrete, consider the following plotting example:</p>

<pre><code>import matplotlib as mpl
import matplotlib.pyplot as plt

def example_figure(slope):
    # Create a new figure
    fig = plt.figure()
    ax = fig.add_subplot(111)

    # Set axes to equal aspect ratio
    ax.set_aspect('equal')

    # Plot a line with a given slope,
    # starting from the origin
    ax.plot([x * slope for x in range(5)])

    # Output the result
    return fig
</code></pre>

<p>This example code will result in figures of different widths, depending on the data:</p>

<pre><code>example_figure(1).show()
</code></pre>

<p><a href="https://i.stack.imgur.com/LqINa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LqINa.png" alt="Line with slope of 1"></a></p>

<pre><code>example_figure(2).show()
</code></pre>

<p><a href="https://i.stack.imgur.com/H9fsj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H9fsj.png" alt="Line with slope of 2"></a></p>

<p>Matplotlib seems to fit the plots into a certain height, and then chooses the width to accomodate the aspect ratio. The <strong>ideal outcome</strong> for me would be the opposite -- the two plots above would have the <strong>same width</strong>, but the second plot would be twice as tall as the first.</p>

<hr>

<p><strong>Bonus &mdash; Difficulty level: Gridspec</strong></p>

<p>In the long run, I would like to create a <strong>grid</strong> in which <strong>one of the plots has a fixed aspect ratio</strong>, and I would again like to align the graphs exactly.</p>

<pre><code># Create a 2x1 grid
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 1)

# Create the overall graphic, containing
# the top and bottom figures
fig = plt.figure()
ax1 = fig.add_subplot(gs[0, :], aspect='equal')
ax2 = fig.add_subplot(gs[1, :])

# Plot the lines as before
ax1.plot(range(5))
ax2.plot(range(5))

# Show the figure
fig.show()
</code></pre>

<p>The result is this:</p>

<p><a href="https://i.stack.imgur.com/2nrVk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2nrVk.png" alt="The same graph twice, the above with fixed aspect ratio"></a></p>

<hr>

<p>So again, my question is: <strong>How does one create graphs that vary flexibly in height depending on the data, while having a fixed width?</strong></p>

<p>Two points to avoid potential misunderstandings:</p>

<ul>
<li>In the above example, both graphs have the same x-axis. This cannot be
taken for granted.</li>
<li>I am aware of the <code>height_ratios</code> option in the gridspec. I can compute
the dimensions of the data, and set the ratios, but this unfortunately 
does not control the graphs directly, but rather their bounding boxes,
so (depending on the axis labels), graphs of different widths still occur. 
Ideally, the plots' canvas would be aligned exactly.</li>
<li><a href="https://stackoverflow.com/questions/30603720/how-do-you-force-python-subplots-to-have-equal-widths-and-separate-heights-grid">Another unsolved question</a> is similar, but slightly more convoluted.</li>
</ul>

<p>Any ideas and suggestions are very welcome, and I'm happy to specify the question further, if required. <strong>Thank you very much for considering this!</strong></p>