Matplotlib:什么是Gridspec或plt.subplot2grid的OOP等价物

时间:2015-01-31 20:23:50

标签: matplotlib

要在可以拥有colspans和rowspans的表中安排子图,Matplotlib的Pyplot-API使用:

在这个问题中,用户 tcaswell 表示不应该混合两个API:

考虑到这个建议,如何在不使用Pyplot-API的情况下进行colspans和rowpans?

编辑1

我认为用一个有效的例子来扩展这个问题可能会更好。我在他的回答中实现了用户 Julien 的内容,一切正常:

#!/usr/bin/python3

from gi.repository import Gtk
from matplotlib.backends.backend_gtk3cairo import (FigureCanvasGTK3Cairo
    as FigureCanvas)
from matplotlib.backends.backend_gtk3 import (NavigationToolbar2GTK3 
    as NavigationToolbar)
import mplstereonet
from matplotlib.gridspec import GridSpec
from matplotlib.figure import Figure

class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_size_request(500, 500)
        self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(self.box)

        self.fig = Figure(dpi = 80)
        gridspec = GridSpec(3, 3)

        subplot_one = gridspec.new_subplotspec((0, 0),
                                            rowspan = 1, colspan = 3)
        subplot_two = gridspec.new_subplotspec((1, 0), rowspan = 2,
                                            colspan = 1)
        subplot_three = gridspec.new_subplotspec((1, 1), rowspan = 2,
                                            colspan = 2)

        ax_one = self.fig.add_subplot(subplot_one)
        ax_two = self.fig.add_subplot(subplot_two)
        ax_three = self.fig.add_subplot(subplot_three)

        xdata = [0.5, 0.2, 0.7, 0.3]
        ydata = [0.2, 0.8, 0.2, 0.4]

        ax_one.scatter(xdata, ydata)
        ax_two.scatter(xdata, ydata)
        ax_three.scatter(xdata, ydata)

        self.canvas = FigureCanvas(self.fig)
        self.box.pack_start(self.canvas, True, True, 0)

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

1 个答案:

答案 0 :(得分:2)

部分答案在于pyplot.subplot2grid文档:

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot2grid

假设您已定义数字fig

from matplotlib.gridspec import GridSpec
gridspec = GridSpec(nrows, ncols)
subplotspec = gridspec.new_subplotspec((row, col), rowspan, colspan)
ax = fig.add_subplot(subplotspec)