Matplotlib使用OO API显示寄生虫轴

时间:2015-02-22 05:16:12

标签: python matplotlib pyqt4

我完全不知道如何使用OO API使用寄生虫轴(图中嵌入了PyQt4)。我想根据下面的代码绘制图中的轴偏移。但是,当在PyQt4中嵌入matplotlib时,不能使用pyplot或host_subplot。

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)

par1 = host.twinx()
par2 = host.twinx()

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
                                axes=par2,
                                offset=(offset, 0))

par2.axis["right"].toggle(all=True)

host.set_xlim(0, 2)
host.set_ylim(0, 2)

par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")

p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")

par1.set_ylim(0, 4)
par2.set_ylim(1, 65)

plt.draw()
plt.show()

我确实设法通过创建两个图并根据这里的问题链接它们的轴来绘制偏移轴:

Matplotlib show one axis and no data

这是我首选的方法,因为它允许我在图形之间放置一个QSplitter,但是我不能让上图仅显示X轴 - 一些图形图及其数据总是显示无论我设置多小y轴是。

然后我尝试通过获取绘图轴几何图形在单独的QWidget中绘制我自己的偏移轴:

ax = foo.figure.add_subplot(1,1,1)
lineGeometry = [ax.bbox.x0, etc.]

然后使用轴几何图形在Qt.QPainter()中绘制一条相同的开始/结束位置,但在单独的QWidget中偏离绘图。这有点笨重,我更喜欢更简单的方式。

任何有关使用Qt4中嵌入的寄生轴的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

感谢上一篇文章中的评论,我设法确定你可以在PyQt4中使用matplotlib.pyplot,但是使用qt4后端进行显示。下面是一个冗长的(道歉)例子。代码有一个错误,虽然这两个情节似乎都是在彼此之上绘图,不知道那里有什么:

import sys
from PyQt4 import QtGui, QtCore
import numpy as np
import matplotlib
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as     FigureCanvas
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
from matplotlib.figure import Figure

class ApplicationWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.main_widget = QtGui.QWidget(self)
        l = QtGui.QVBoxLayout(self.main_widget)
        dc = PyPlotCanvas(self.main_widget, width=5, height=4, dpi=100)
        ac = AxisArtistOO(self.main_widget, width=5, height=4, dpi=100)
        l.addWidget(dc)
        l.addWidget(ac)
        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

class PyPlotCanvas(FigureCanvas):
    """Matplotlib pyplot as FigureCanvas"""
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111)
        self.compute_initial_figure()
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        FigureCanvas.setSizePolicy(self,
                               QtGui.QSizePolicy.Expanding,
                               QtGui.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        fig, ax = plt.subplots()
        N = 3
        x       = np.random.rand(N)
        y       = np.random.rand(N)
        radii   = 0.1*np.random.rand(N)
        patches = []
        for x1,y1,r in zip(x, y, radii):
            circle = Circle((x1,y1), r)
            patches.append(circle)
        colors = 100*np.random.rand(len(patches))
        p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
        p.set_array(np.array(colors))
        ax.add_collection(p)
        plt.colorbar(p)
        self.fig = fig

class AxisArtistOO(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111)
        self.compute_initial_figure()
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        FigureCanvas.setSizePolicy(self,
                               QtGui.QSizePolicy.Expanding,
                               QtGui.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        host = host_subplot(111, axes_class=AA.Axes)
        plt.subplots_adjust(right=0.75)
        par1 = host.twinx()
        par2 = host.twinx()
        offset = 60
        new_fixed_axis = par2.get_grid_helper().new_fixed_axis
        par2.axis["right"] = new_fixed_axis(loc="right",
                                    axes=par2,
                                    offset=(offset, 0))
        par2.axis["right"].toggle(all=True)
        host.set_xlim(0, 2)
        host.set_ylim(0, 2)
        par1.set_ylabel("Temperature")
        par2.set_ylabel("Velocity")
        p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
        p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")
        par1.set_ylim(0, 4)
        par2.set_ylim(1, 65)
        fig, ax = plt.subplots()
        self.fig = fig

qApp = QtGui.QApplication(sys.argv)
aw = ApplicationWindow()
aw.show()
sys.exit(qApp.exec_())