Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust one subplot's height in absolute way (not relative) in Matplotlib

I would like to have two subplots in Matplotlib (embedded in a GUI):

  • Subplot 1: Height is fixed at a certain amount of pixels.
  • Subplot 2: Height takes up the remainder of the whole figure's height (allowing for margins).

In both cases these are the same width.

GridSpec seems to not allow absolute sizing, only relative sizing. I don't want Subplot 1 to stretch its height with the window resizing.

How can I do this?

like image 623
Chelonian Avatar asked Mar 26 '12 21:03

Chelonian


People also ask

How to set the width and height of a Matplotlib figure?

When using Matplotlib to create a figure that contains a grid of subplots, you can set the width and height of the figure by using the set_figwidth () and set_figheight () methods, respectively. Let’s examine the two examples below for more clarity.

How do you adjust the left side of a subplot?

Syntax: subplots_adjust (self, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) left : This parameter is the left side of the subplots of the figure.

What is the use of subplots_adjust () method in Matplotlib?

This module is used to control the default spacing of the subplots and top level container for all plot elements. The subplots_adjust () method figure module of matplotlib library is used to Update the SubplotParams with kwargs. Syntax: subplots_adjust (self, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

How to create different subplot sizes in Matplotlib?

How to Create Different Subplot Sizes in Matplotlib? 1 Python3. import matplotlib.pyplot as plt. from matplotlib import gridspec. import numpy as np. fig = plt.figure () fig.set_figheight (8) fig. 2 Python3. 3 Python3.


1 Answers

If I understood your question then this code might help. It uses axes instead of subplots, which give more absolute control over the position of the plots within the figure.

    import pylab as pl
    # use pylab.Axes(figure, [left, bottom, width, height]) 
    # where each value in the frame is between 0 and 1

    #top
    figure = pl.figure()
    axes = pl.Axes(figure, [.4,.4,.25,.25])
    figure.add_axes(axes) 
    pl.plot([1, 2, 3], [1, 2, 3])

    #bottom
    axes = pl.Axes(figure, [.4,.1,.25,.25])
    figure.add_axes(axes) 
    pl.plot([1, 2, 3], [1, 2, 3])

    pl.show()

If this is not what you meant, please post a snippet of code or an example to clarify.

like image 121
sbraden Avatar answered Oct 26 '22 03:10

sbraden