Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enlarge subplot matplotlib?

I currently have two subplots. Note: this is in matplotlib v0.99.3 on Mac OS X 10.6.x

I have an event-handler that when one of the subplots are clicked, it prints something. This is only a temporary place holder. What I want to happen is when the subplot is clicked, I want it to take up the whole figure (delete the other subplot and maximize to fill up the whole figure). How would I go about doing this?

like image 570
pybindo Avatar asked Mar 23 '26 12:03

pybindo


1 Answers

You can do this by modifying the axes that subplot returns. That is, axes can be positioned and sized in any desired way, and subplot is just a function that returns axes positioned in a uniform grid; but once you have these axes from subplot you can arbitrarily resize and reposition them. Here's an example:

from pylab import *

axes = [None, None]

def make():
    figure()
    axes[0] = subplot(1, 2, 1)
    axes[1] = subplot(1, 2, 2)
    show()

def re_form():
    xmax = axes[1].get_position().get_points()[1][0]
    axes[1].set_axis_off()
    (x0, y0), (x1, y1) = axes[0].get_position().get_points() # xmin, ymin, xmax, ymax
    axes[0].set_position([x0, y0, xmax-x0, y1-y0])  # x, y, width, height
    show()

Here I used show() to update the plot, but you'll want to use something more appropriate for your event handler. This demo works with iPython: first run the file, then call make(), which draws the two axes, and then re_form(), which removes the second axis and widens the first.

like image 82
tom10 Avatar answered Mar 26 '26 02:03

tom10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!