pyqt5如何在绘图中使用yticks? [复制]
Posted
技术标签:
【中文标题】pyqt5如何在绘图中使用yticks? [复制]【英文标题】:pyqt5 how to use yticks in drawing? [duplicate] 【发布时间】:2018-05-06 06:26:12 【问题描述】:在 matplotlib 中,我可以使用 plt.yticks 来设置我喜欢的比例。
def plot(self,data):
''' plot some random stuff '''
plt.figure()
plt.plot(data,'ro')
plt.yticks([1, 2, 3, 4,5,],['Fault 1', 'Fault 2','Fault 3','Fault 4','Fault 5',])
plt.show()
但是,在使用pyqt5时,我必须将matplotlib与canvas结合起来,使用yticks似乎没用。
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
# a figure instance to plot on
self.figure = plt.figure()
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)
# Just some button connected to `plot` method
self.button = QtWidgets.QPushButton('Plot')
self.button.clicked.connect(self.plot)
# set the layout
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.setLayout(layout)
def plot(self,data):
''' plot some random stuff '''
# random data
# data = [random.random() for i in range(10)]
# create an axis
ax = self.figure.add_subplot(111)
# discards the old graph
ax.hold(False)
# plot data
ax.plot(data, '*-')
ax.yticks([1, 2, 3, 4,5,],['Fault 1', 'Fault 2','Fault 3','Fault 4','Fault 5',])
#ax.set_yticks([1, 2, 3, 4,5,],['Fault 1', 'Fault 2','Fault 3','Fault 4','Fault 5',])
# refresh canvas
self.canvas.draw()
有人知道如何在 pyqt5 中使用 yticks(也许在画布中)?
【问题讨论】:
【参考方案1】:也许我知道如何自己解决。
list1=list(range(1,6))
ax.set_yticks(list1)
ax.set_yticklabels(['fault'+str(i) for i in list1])
【讨论】:
以上是关于pyqt5如何在绘图中使用yticks? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
matplotlib set_yticks 删除 imshow 的上半行和下半行 [重复]
输入用户数据时如何更新 Matplotlib 中的绘图(通过 PyQt5 按钮)