我完全不知道如何使用OO (嵌入在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将不胜感激。
发布于 2015-02-22 12:29:58
由于我在上一篇文章中提到的注释,我设法在PyQt4中使用PyQt4,但使用qt4后端显示。下面是一个冗长的(道歉)示例。代码中有一个bug,尽管这两幅图似乎都是在画对方,但不确定那里发生了什么:
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_())https://stackoverflow.com/questions/28654812
复制相似问题