找回密码
 立即注册
Qt开源社区 门户 查看内容

PyQt5 plotnine

2019-9-17 08:02| 发布者: admin| 查看: 1029| 评论: 0

摘要: 先给出链接:https://github.com/has2k1/plotnine/issues/298这方面的资料比较少,反正没百度到。最后还是https://cn.bing.com/找到了。python做数据展示,尤其是静态图,肯定是要用plotnine了。之前只是简单的生成 ...
先给出链接:
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。
----------------------------------------------------------------------------------------------------------------------

鲜花

握手

雷人

路过

鸡蛋

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了