pyqt5中能用 seaborn吗
时间: 2024-01-24 07:03:10 浏览: 98
Python数据可视化库Seaborn
在PyQt5中可以使用seaborn,但需要先在PyQt5应用程序中加载Matplotlib模块。以下是一个简单的示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import seaborn as sns
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Seaborn in PyQt5'
self.left = 0
self.top = 0
self.width = 600
self.height = 400
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createFigure()
def createFigure(self):
# Create the figure and axes objects
fig = Figure()
ax = fig.add_subplot(111)
# Plot the data with seaborn
sns.set()
iris = sns.load_dataset('iris')
sns.scatterplot(x='petal_length', y='petal_width', hue='species', data=iris, ax=ax)
# Add the figure canvas to the PyQt5 application
canvas = FigureCanvas(fig)
canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
# Add the figure canvas to a widget and set the layout
widget = QWidget()
layout = QVBoxLayout(widget)
layout.addWidget(canvas)
self.setCentralWidget(widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
```
在这个示例中,我们首先从Matplotlib中导入FigureCanvas,然后使用Seaborn绘制了一个散点图,并将其添加到了PyQt5应用程序中。注意,我们在绘图之前调用了`sns.set()`,以便使用Seaborn的默认样式。
阅读全文