5

What I would like to achive are plots with equal scale aspect ratio, and fixed width, but a dynamically chosen height.

To make this more concrete, consider the following plotting example:

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

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

example_figure(1).show()

Line with slope of 1

example_figure(2).show()

Line with slope of 2

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


Bonus — Difficulty level: Gridspec

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

# 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()

The result is this:

The same graph twice, the above with fixed aspect ratio


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

Two points to avoid potential misunderstandings:

  • In the above example, both graphs have the same x-axis. This cannot be taken for granted.
  • I am aware of the height_ratios 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.
  • Another unsolved question is similar, but slightly more convoluted.

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

1
  • Oh, and just to be sure: All of these examples are using Matplotlib 1.5.1 (via anaconda, if that helps) Commented Jul 11, 2016 at 13:26

1 Answer 1

1

Have you tried to fix the width with fig.set_figwidth()?

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, and thanks for the suggestion! Indeed I looked at setting the figure width, but that hasn't really worked: What seems to happen is that the setting changes the width of the invisible bounding box around the graph, and resets the constraint on the graph width. A hackish kinda-solution (that really slows down mpl) is to define the figure width and set the height to some unrealistically large value. Matplotlib then cuts off the unused space. This, however, does not work (to my knowledge) in the Gridspec case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.