Python GUI绘图轴和标题标签



我在标记轴和为绘图命名时遇到问题。我正在使用QT Designer创建一个ui文件。有人能帮忙吗?

class MplCanvas(FigureCanvas):
def __init__(self, parent=None, width=6, height=5, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)

x_pos = np.arange(len(All_Runs_Names))
# plot figure
sc = MplCanvas(self, width=5, height=4, dpi=100)
# assign data here
sc.axes.bar(x_pos, All_Runs_Total_Errors, color = (0.5,0.1,0.5,0.6))

# toolbar and layout creation
toolbar = NavigationToolbar(sc, self.MyWindow)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(toolbar)
layout.addWidget(sc)
self.MyWindow.plotWidget.setLayout(layout)

由于您使用的是add_subplot,因此了解如何为子图设置标题和标签。如果看不到绘图标题,则可能需要adjust_subplots

此外,考虑使用QtAgg后端(版本取决于您使用的pyqt版本(,原因如下所述。

干杯。

PS。出于我自己的目的,我做了类似的事情,尽管使用了add_axes而不是add_subplot:

import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from PyQt5 import QtWidgets, uic
import sys
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
uic.loadUi('path_to_file\file.ui', self)

self.my_chart = MplCanvas(self)
layout = self.mychart_layout
layout.addWidget(self.my_chart)
self.something_to_do([1,2,3,4],[1,2,3,4])
self.show()
def something_to_do(self, xdata, ydata):
self.my_chart.axes.set_title("title")
self.my_chart.axes.set_position([0.07,0.1,0.87,0.8]) # here is repositioning of the axes
# if the default (in the MplCanvas class) cannot reveal the title set
# definitely something you should tweak.
line, = self.my_chart.axes.plot(xdata, ydata) # line handle for updating the line during runtime

class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_axes([0.07,0.165,0.925,0.83]) # initial rectangle for any canvas' axes ([left, bottom, width, height])
self.axes.set_ylabel('y label')
self.axes.set_xlabel('x label')
super(MplCanvas, self).__init__(fig)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
app.exec_()

相关内容

  • 没有找到相关文章

最新更新