python - pyqt5如何在绘图中使用yticks?
问题描述
在 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(也许在画布中)?
解决方案
也许我知道如何自己解决它。
list1=list(range(1,6))
ax.set_yticks(list1)
ax.set_yticklabels(['fault'+str(i) for i in list1])
推荐阅读
- asp.net-mvc - Modernizr 还需要吗?
- python - QuantLib Python 中的黑色方差函数如何在校准原始隐含波动率表面中发挥作用
- sql - 计算有多个联系人的公司
- java - 在数组中查找最大子数组
- function - PopupMenuButton 发送 Buildcontext 到 handleclick 函数
- java - Eclipse 在将项目导出为可执行 jar 期间找不到主类
- php - 从 JSON 中获取特定数据,由 PHP 值过滤
- c++ - 无法使用“int [m][n]”类型的左值初始化“int **”类型的参数注意:在此处将参数传递给参数“dp”
- reactjs - next-auth 自定义登录表单不将用户名和密码传递给 [...nextauth].js
- ios - UIButton 被禁用 - Swift