問題描述
我想知道是否有辦法將滾動條(水平或垂直)放在 matplotlib 顯示頁面(plt.show
)上,該頁面包含多個子批次(sublot2grid
).目前,我找到的唯一解決方案是使子圖非常小,這根本不是很優雅.
I want to know if there is a manner to put a scrollbar (horizontal or vertical) on a matplotlib showing page (plt.show
) that contains several sublots (sublot2grid
). At the moment, the only solution I find is to make the subplots very small, which isn't very elegant at all.
推薦答案
顯示matplotlib圖的窗口沒有添加滾動條的選項.它會自動將自己調整為圖形大小.相反,如果調整大小,圖形也會調整大小.
The window showing the matplotlib figure does not have the option to add scrollbars. It will automatically resize itself to the figure size. And inversely, if it is resized the figure will resize as well.
一個選項是構建一個具有此功能的自定義窗口.為此,可以使用 PyQt.下面給出了一個示例,其中不是調用 plt.show()
而是調用自定義類,并將圖形作為參數進行繪制.圖形大小應事先設置為圖形 fig
并且該自定義類不會更改它.相反,它將圖形放入帶有滾動條的畫布中,以便圖形保持其原始大小并且可以在 Qt 窗口中滾動.您不必處理類中的細節,只需處理腳本末尾的調用即可.
An option would be to build a custom window which does have this ability. For that purpose, one can use PyQt. An example is given below, where instead of calling plt.show()
a custom class is called with the figure to draw as an argument. The figure size should be set to the figure fig
beforehands and that custom class will not change it. Instead it puts the figure into a canvas with scrollbars, such that the figure retains it's original size and can be scrolled within the Qt window. You wouln't have to deal with the details inside the class but only the call at the end of the script.
此示例適用于 PyQt4,請參閱下面的 PyQt5 示例.
This example is for PyQt4, see below for a PyQt5 example.
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)
雖然此答案顯示了一種滾動完整圖形的方法,但如果您對 滾動軸的內容感興趣,請查看 這個答案
這篇關于Matplotlib 顯示頁面上的滾動條的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!