带有pyqt窗口的可伸缩matplotlib图
因为我的 stackoverflow 上一个问题我的数字的边缘:
但是,您可以看到它不能保持pyqt窗口缩放,我不知道如何使图形大小与我的窗口大小一致。
import sys
import matplotlib; matplotlib.use("Qt5Agg")
from PyQt5 import QtWidgets, QtCore, Qt # <- additional import
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from netgraph import EditableGraph
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None):
figure = plt.figure()
figure.set_figheight(6)
figure.set_figwidth(6)
plt.tight_layout()
figure.patch.set_visible(False)
super(MplCanvas, self).__init__(figure)
self.setParent(parent)
self.ax = plt.axes([0,0,1,1], frameon=False)
self.ax.axis('off')
self.ax.get_xaxis().set_visible(False)
self.ax.get_yaxis().set_visible(False)
self.graph = EditableGraph([(0, 1)], ax=self.ax)
plt.autoscale(axis='both', tight=True)
self.updateGeometry()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.canvas = MplCanvas(self)
self.vbl = QtWidgets.QVBoxLayout()
self.vbl.addWidget(self.canvas)
self.setLayout(self.vbl)
def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
if __name__ == "__main__":
main()
我也面临着一种奇怪的行为。如果我将数字的高度和宽度更改为不同的值(8和6 por示例),那么
figure.set_figheight(8)
figure.set_figwidth(6)
我无法弄清楚为什么它在矩形图上不起作用。
有人知道我能做什么才能完善这种行为?
Since my previous question here at StackOverflow I was able to make my plot touch the edges of my Figure:
However as you can see it does not keep the PyQt Window scaling, I do not know how to make the Figure size coincide with my Window size.
import sys
import matplotlib; matplotlib.use("Qt5Agg")
from PyQt5 import QtWidgets, QtCore, Qt # <- additional import
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from netgraph import EditableGraph
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None):
figure = plt.figure()
figure.set_figheight(6)
figure.set_figwidth(6)
plt.tight_layout()
figure.patch.set_visible(False)
super(MplCanvas, self).__init__(figure)
self.setParent(parent)
self.ax = plt.axes([0,0,1,1], frameon=False)
self.ax.axis('off')
self.ax.get_xaxis().set_visible(False)
self.ax.get_yaxis().set_visible(False)
self.graph = EditableGraph([(0, 1)], ax=self.ax)
plt.autoscale(axis='both', tight=True)
self.updateGeometry()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.canvas = MplCanvas(self)
self.vbl = QtWidgets.QVBoxLayout()
self.vbl.addWidget(self.canvas)
self.setLayout(self.vbl)
def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
if __name__ == "__main__":
main()
And I am also facing a strange behaviour. If I change the figure height and width to different values (8 and 6 por exemple) like so:
figure.set_figheight(8)
figure.set_figwidth(6)
I can not figure why it would not work for rectangle plots.
Does anyone have a clue of what I can do to perfect this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用此代码:
You can use this code: