简体   繁体   English

如何放大subplot matplotlib?

[英]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 注意:这是在Mac OS X 10.6.x上的matplotlib v0.99.3中

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? 我将如何去做呢?

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. 在这里,我使用show()更新了图表,但是您将需要使用更适合事件处理程序的东西。 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. 此演示适用于iPython:首先run文件,然后调用make()绘制两个轴,然后re_form()删除第二个轴并加宽第一个轴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM