先给出链接: https://github.com/has2k1/plotnine/issues/298 这方面的资料比较少,反正没百度到。最后还是https://cn.bing.com/找到了。 python做数据展示,尤其是静态图,肯定是要用plotnine了。之前只是简单的生成一张图,插入ppt用用。学了几天PyQt5,突然想将其插入软件界面,用处应还比较大。 主要程序如下: """ Example adapted for plotnine from https://stackoverflow.com/a/12465861/11918518 """ import sys import random import pandas as pd import numpy as np import matplotlib matplotlib.use('agg') from plotnine import ggplot, geom_point, aes, geom_line from PyQt5.QtWidgets import QApplication, QPushButton, QDialog, QVBoxLayout import matplotlib.pyplot as plt from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar from matplotlib.figure import Figure class Window(QDialog): def __init__(self, parent=None): super(Window, self).__init__(parent) self.setWindowTitle("Plotnine in PyQt5") self.figure = Figure() self.canvas = FigureCanvas(self.figure) self.toolbar = NavigationToolbar(self.canvas, self) self.button = QPushButton("Plot") self.button.clicked.connect(self.plot) layout = QVBoxLayout() layout.addWidget(self.toolbar) layout.addWidget(self.canvas) layout.addWidget(self.button) self.setLayout(layout) def plot(self): # generate some 'data' to plot data = np.empty([100, 4]) data[:, 0] = x = np.linspace(0, 2 * np.pi, 100) data[:, 1] = np.random.rand(1, 100) data[:, 2] = np.sin(x) data[:, 3] = np.cos(x) * np.sin(x) df = pd.DataFrame(data, columns=["x", "y1", "y2", "y3"]) # change the dependent variable and color each time this method is called y = random.choice(["y1", "y2", "y3"]) color = random.choice(["blue", "red", "green"]) # specify the plot and get the figure object ff = ggplot(df, aes("x", y)) + geom_point(color=color) + geom_line() fig = ff.draw() # update to the new figure self.canvas.figure = fig # refresh canvas self.canvas.draw() # close the figure so that we don't create too many figure instances plt.close(fig) if __name__ == "__main__": app = QApplication(sys.argv) main = Window() main.show() sys.exit(app.exec_()) 效果图如下: ![]() 今天,我只是一个搬运工。 ---------------------------------------------------------------------------------------------------------------------- 我们尊重原创,也注重分享,文章来源于微信公众号:legendfly,建议关注公众号查看原文。如若侵权请联系qter@qter.org。 ---------------------------------------------------------------------------------------------------------------------- |