我可以在我的 PyQt4 应用程序中嵌入绘图(离线)吗?
Can I embed plotly graphs (offline) in my PyQt4 application?
我知道 plotly 渲染成 HTML 并且可以嵌入到类似网络的环境中。我想知道是否可以在 PyQt 应用程序的 HTML window 中做到这一点?具体来说,我想知道它是否可以离线使用,没有互联网连接。
编辑:
这是我最终如何使用 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)
然后在另一个函数中:
# 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)
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')
import os,sys
from PyQt4 import QtGui
pic.setPixmap(QtGui.QPixmap("graphs.png"))
不确定这是否完全满足您的需求,因为您有
import plotly.plotly as py
你可以保存图片然后
py.image.save_as({'data': data}, 'your_image_filename.png')
pic.setPixmap(QtGui.QPixmap("your_image_filename.png"))
警告,我还没有尝试过这个
我曾尝试使用:
import plotly.offline as plt
.
.
.
plt.plot(fig, filename=testName + '__plot.html')
然后尝试生成一个图.. 这给了我一个 HTML 文件,然后我还尝试将 QWebView 作为它的 URL 属性 [只是为了看看是否它呈现]。
这是供您参考的图片:
您可以使用 QWebEngineWidgets
模块中的 QWebEngineView
(我在这里使用 PyQt5
,但我想它也适用于 PyQt4
)。您使用 plotly.offline.plot
创建 html 代码并将其设置为 QWebEngineView
实例的 html 文本。如果你指定 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_()
我知道 plotly 渲染成 HTML 并且可以嵌入到类似网络的环境中。我想知道是否可以在 PyQt 应用程序的 HTML window 中做到这一点?具体来说,我想知道它是否可以离线使用,没有互联网连接。
编辑:
这是我最终如何使用 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)
然后在另一个函数中:
# 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)
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')
import os,sys
from PyQt4 import QtGui
pic.setPixmap(QtGui.QPixmap("graphs.png"))
不确定这是否完全满足您的需求,因为您有
import plotly.plotly as py
你可以保存图片然后
py.image.save_as({'data': data}, 'your_image_filename.png')
pic.setPixmap(QtGui.QPixmap("your_image_filename.png"))
警告,我还没有尝试过这个
我曾尝试使用:
import plotly.offline as plt
.
.
.
plt.plot(fig, filename=testName + '__plot.html')
然后尝试生成一个图.. 这给了我一个 HTML 文件,然后我还尝试将 QWebView 作为它的 URL 属性 [只是为了看看是否它呈现]。
这是供您参考的图片:
您可以使用 QWebEngineWidgets
模块中的 QWebEngineView
(我在这里使用 PyQt5
,但我想它也适用于 PyQt4
)。您使用 plotly.offline.plot
创建 html 代码并将其设置为 QWebEngineView
实例的 html 文本。如果你指定 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_()