Matplotlib:Gridspec 或 plt.subplot2grid 的 OOP 等价物是什么
Matplotlib: What is the OOP equivalent of Gridspec or plt.subplot2grid
要在可以具有 colspans 和 rowspans 的表中排列子图,Matplotlib 的 Pyplot-API 使用:
在这个问题中,用户 tcaswell 说不应该将两者混为一谈 API:
- On handling the layout of subfigures
考虑到这个建议,如何在不使用 Pyplot-API 的情况下执行 colspans 和 rowspans?
编辑 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()
部分答案在 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)
要在可以具有 colspans 和 rowspans 的表中排列子图,Matplotlib 的 Pyplot-API 使用:
在这个问题中,用户 tcaswell 说不应该将两者混为一谈 API:
- On handling the layout of subfigures
考虑到这个建议,如何在不使用 Pyplot-API 的情况下执行 colspans 和 rowspans?
编辑 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()
部分答案在 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)