我想知道是否有一种方法可以在matplotlib上显示滚动条(水平或垂直),显示包含多个子切片(plt.show
)的页面(sublot2grid
)。目前,我找到的唯一解决方案是使子图非常小,这根本不是很优雅。
答案 0 :(得分:8)
显示matplotlib图的窗口没有添加滚动条的选项。它会自动调整大小到图形大小。相反,如果调整大小,数字也会调整大小。
一个选项是构建一个具有此功能的自定义窗口。为此,可以使用PyQt。下面给出一个示例,其中不是调用plt.show()
而是调用自定义类,并将该图作为参数绘制。图形大小应设置为图形fig
beforehands,而自定义类不会更改它。相反,它将图形放入带有滚动条的画布中,这样图形保留了原始大小,并且可以在Qt窗口中滚动。你不必处理课堂内的细节,只需处理剧本末尾的电话。
此示例适用于 PyQt4 ,请参阅下面的PyQt5示例。
import matplotlib.pyplot as plt
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
class ScrollableWindow(QtGui.QMainWindow):
def __init__(self, fig):
self.qapp = QtGui.QApplication([])
QtGui.QMainWindow.__init__(self)
self.widget = QtGui.QWidget()
self.setCentralWidget(self.widget)
self.widget.setLayout(QtGui.QVBoxLayout())
self.widget.layout().setContentsMargins(0,0,0,0)
self.widget.layout().setSpacing(0)
self.fig = fig
self.canvas = FigureCanvas(self.fig)
self.canvas.draw()
self.scroll = QtGui.QScrollArea(self.widget)
self.scroll.setWidget(self.canvas)
self.nav = NavigationToolbar(self.canvas, self.widget)
self.widget.layout().addWidget(self.nav)
self.widget.layout().addWidget(self.scroll)
self.show()
exit(self.qapp.exec_())
# create a figure and some subplots
fig, axes = plt.subplots(ncols=4, nrows=5, figsize=(16,16))
for ax in axes.flatten():
ax.plot([2,3,5,1])
# pass the figure to the custom window
a = ScrollableWindow(fig)
<小时/> 这是 PyQt5 的版本。
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
class ScrollableWindow(QtWidgets.QMainWindow):
def __init__(self, fig):
self.qapp = QtWidgets.QApplication([])
QtWidgets.QMainWindow.__init__(self)
self.widget = QtWidgets.QWidget()
self.setCentralWidget(self.widget)
self.widget.setLayout(QtWidgets.QVBoxLayout())
self.widget.layout().setContentsMargins(0,0,0,0)
self.widget.layout().setSpacing(0)
self.fig = fig
self.canvas = FigureCanvas(self.fig)
self.canvas.draw()
self.scroll = QtWidgets.QScrollArea(self.widget)
self.scroll.setWidget(self.canvas)
self.nav = NavigationToolbar(self.canvas, self.widget)
self.widget.layout().addWidget(self.nav)
self.widget.layout().addWidget(self.scroll)
self.show()
exit(self.qapp.exec_())
# create a figure and some subplots
fig, axes = plt.subplots(ncols=4, nrows=5, figsize=(16,16))
for ax in axes.flatten():
ax.plot([2,3,5,1])
# pass the figure to the custom window
a = ScrollableWindow(fig)
答案 1 :(得分:3)
因为代码对我来说有点复杂和混乱,所以我将其重构为更具可读性和重构性,如下所示。我希望这会有所帮助。谢谢!
import sys
import random
import matplotlib
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import (
QWidget,
QApplication,
QMainWindow,
QVBoxLayout,
QScrollArea,
)
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigCanvas,
NavigationToolbar2QT as NabToolbar,
)
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
# create a figure and some subplots
FIG, AXES = plt.subplots(ncols=4, nrows=5, figsize=(16,16))
for AX in AXES.flatten():
random_array = [random.randint(1, 30) for i in range(10)]
AX.plot(random_array)
def main():
app = QApplication(sys.argv)
window = MyApp(FIG)
sys.exit(app.exec_())
class MyApp(QWidget):
def __init__(self, fig):
super().__init__()
self.title = 'VERTICAL, HORIZONTAL SCROLLABLE WINDOW : HERE!'
self.posXY = (700, 40)
self.windowSize = (1200, 800)
self.fig = fig
self.initUI()
def initUI(self):
QMainWindow().setCentralWidget(QWidget())
self.setLayout(QVBoxLayout())
self.layout().setContentsMargins(0, 0, 0, 0)
self.layout().setSpacing(0)
canvas = FigCanvas(self.fig)
canvas.draw()
scroll = QScrollArea(self)
scroll.setWidget(canvas)
nav = NabToolbar(canvas, self)
self.layout().addWidget(nav)
self.layout().addWidget(scroll)
self.show_basic()
def show_basic(self):
self.setWindowTitle(self.title)
self.setGeometry(*self.posXY, *self.windowSize)
self.show()
if __name__ == '__main__':
main()