本文介紹了如何將繪圖作為 PyQt5 小部件?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在做 GUI,其中應(yīng)該有一個(gè)帶有以下情節(jié)的小部件:
I am doing GUI where there should be a widget with the following plot:
import plotly.express as px
df = px.data.tips()
fig = px.box(df, x="day", y="total_bill", color="smoker")
fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
fig.show()
你能提出一些建議嗎?
推薦答案
您可以使用 QtWebEngineWidgets.QWebEngineView
小部件來顯示圖形.您可能需要先安裝 PyQtWebEngine
包.這是一個(gè)小例子:
You can use a QtWebEngineWidgets.QWebEngineView
widget to show the figure. You might need to install the PyQtWebEngine
package first. Here is a small example:
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
import plotly.express as px
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.button = QtWidgets.QPushButton('Plot', self)
self.browser = QtWebEngineWidgets.QWebEngineView(self)
vlayout = QtWidgets.QVBoxLayout(self)
vlayout.addWidget(self.button, alignment=QtCore.Qt.AlignHCenter)
vlayout.addWidget(self.browser)
self.button.clicked.connect(self.show_graph)
self.resize(1000,800)
def show_graph(self):
df = px.data.tips()
fig = px.box(df, x="day", y="total_bill", color="smoker")
fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = Widget()
widget.show()
app.exec()
這篇關(guān)于如何將繪圖作為 PyQt5 小部件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!