chi*_*nne 6 python pyqt4 plotly
我知道这可以简单地呈现为HTML,并且可以嵌入到类似Web的环境中.我想知道是否可以在PyQt应用程序的HTML窗口中执行此操作?具体来说,我想知道它是否可以离线工作,没有互联网连接.
编辑:
这是我最终使用matplotlib嵌入图形的摘录:
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg \
import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg \
import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
class Contour(QtGui.QFrame):
def __init__(self, parent=None):
super(Contour, self).__init__(parent)
self.parent = parent
# a figure instance to plot on
self.figure = plt.figure(figsize=(20, 30))
r, g, b = 100./255., 100./255., 100./255.
self.figure.patch.set_facecolor(color=(r, g, b))
# 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)
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
self.setLayout(layout)
Run Code Online (Sandbox Code Playgroud)
然后在另一个功能:
# create an axis
ax1 = self.figure.add_subplot(211, frame_on=False)
ax2 = self.figure.add_subplot(212, frame_on=False)
# plot data
r, g, b = 39./255., 40./255., 34./255.
ax1.plot(x, y, ls='o', color=(r, g, b), linewidth=3)
ax1.plot(coo[0], coo[1], 'go', zorder=20) # leading edge
ax1.plot(xg, yg, 'mo', zorder=30) # leading edge
ax1.plot(xr, yr, 'yo', zorder=30) # curvature circle center
ax1.add_patch(circle)
ax1.set_title('Contour', fontsize=14)
ax1.set_xlim(-10.0, 110.0)
# ax1.set_ylim(-10.0, 14.0)
r, g, b = 249./255., 38./255., 114./255.
ax1.fill(x, y, color=(r, g, b))
ax1.set_aspect('equal')
ax2.plot(coo[0], gradient, 'go-', linewidth=3)
ax2.set_title('Gradient', fontsize=14)
ax2.set_xlim(-10.0, 110.0)
Run Code Online (Sandbox Code Playgroud)
小智 12
您可以QWebEngineView从QWebEngineWidgets模块中使用(我PyQt5在这里使用过,但我想它也适用于PyQt4)。您使用创建 html 代码plotly.offline.plot并将其设置为 .html 实例的 html 文本QWebEngineView。如果你指定output_type='div'它会直接给你一个字符串。我不知道为什么,但在我的情况下,它只适用于include_plotlyjs='cdn',但在这种情况下,您需要在互联网上才能根据plotly文档工作。通过这样做,绘图在您的PyQt应用程序中也保持交互。
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow
from plotly.graph_objects import Figure, Scatter
import plotly
import numpy as np
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# some example data
x = np.arange(1000)
y = x**2
# create the plotly figure
fig = Figure(Scatter(x=x, y=y))
# we create html code of the figure
html = '<html><body>'
html += plotly.offline.plot(fig, output_type='div', include_plotlyjs='cdn')
html += '</body></html>'
# we create an instance of QWebEngineView and set the html code
plot_widget = QWebEngineView()
plot_widget.setHtml(html)
# set the QWebEngineView instance as main widget
self.setCentralWidget(plot_widget)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
Run Code Online (Sandbox Code Playgroud)
mrk*_*sco 10
我曾经尝试使用:
import plotly.offline as plt
.
.
.
plt.plot(fig, filename=testName + '__plot.html')
Run Code Online (Sandbox Code Playgroud)
然后尝试生成一个图..这给了我一个 HTML 文件,然后我还尝试将 QWebView 作为其 URL 属性[只是为了查看它是否呈现]。
这是供您参考的图像:
小智 2
Plotly 的主要开发目的是使浏览器中的绘图变得容易。我不认为它可以嵌入到像 PyQT 或 Tkinter 这样的 UI 框架中。Plotly 有一个企业版本,可以在我们公司网络中运行,无需互联网连接。
如果您确实需要在 PyQT 中嵌入图形,PyQtgraph 或 MatPlotLib 可能是您的选择。
以下是将图形导出为 png,然后将 png 图像嵌入到 PyQT 应用程序中的示例代码。
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5], [10,20,30])
plt.savefig('graphs.png')
Run Code Online (Sandbox Code Playgroud)
import os,sys
from PyQt4 import QtGui
pic.setPixmap(QtGui.QPixmap("graphs.png"))
Run Code Online (Sandbox Code Playgroud)