问题
Is there any way to use plotly just like matplotlib, that is, making the plot appear in a pop up window? Eg, is there an simple equivalent of
plt.plot([1,2,3], [2, 3, 2.5])
plt.show()
I've tried various functions, but all of them seem to either create an html file or an image file.
回答1:
Show in system image viewer
You can open the image that is created by plotly in your default system image viewer, which is kind of a standalone window.
import numpy as np
import plotly.graph_objs as go
fig = go.Figure()
fig.add_scatter(x=np.random.rand(100), y=np.random.rand(100), mode='markers',
marker={'size': 30, 'color': np.random.rand(100), 'opacity': 0.6,
'colorscale': 'Viridis'});
def show(fig):
import io
import plotly.io as pio
from PIL import Image
buf = io.BytesIO()
pio.write_image(fig, buf)
img = Image.open(buf)
img.show()
show(fig)
The drawback of this is of course that you don't have any interaction.
Create browser window
The alternative can be to create a webbrowser window to show the html page generated by plotly. To this end you need to make use of a GUI toolkit that allows to create a browser. PyQt5 would be one.
So the following creates a PyQt5 window with a browser and loads the html page created by plotly inside of it for you to interact with. (This is tested with pyqt 5.9, it may not work with much older versions.)
import numpy as np
import plotly.graph_objs as go
fig = go.Figure()
fig.add_scatter(x=np.random.rand(100), y=np.random.rand(100), mode='markers',
marker={'size': 30, 'color': np.random.rand(100), 'opacity': 0.6,
'colorscale': 'Viridis'});
def show_in_window(fig):
import sys, os
import plotly.offline
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
plotly.offline.plot(fig, filename='name.html', auto_open=False)
app = QApplication(sys.argv)
web = QWebEngineView()
file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "name.html"))
web.load(QUrl.fromLocalFile(file_path))
web.show()
sys.exit(app.exec_())
show_in_window(fig)
回答2:
Here's a standalone class made from ImportanceOfBeingErnest's answer incase anyone needs that
import numpy as np
import plotly.graph_objs as go
import plotly.offline
fig = go.Figure()
fig.add_scatter(x=np.random.rand(100), y=np.random.rand(100), mode='markers',
marker={'size': 30, 'color': np.random.rand(100), 'opacity': 0.6,
'colorscale': 'Viridis'});
import os, sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5 import QtWebEngineWidgets
class PlotlyViewer(QtWebEngineWidgets.QWebEngineView):
def __init__(self, fig, exec=True):
# Create a QApplication instance or use the existing one if it exists
self.app = QApplication.instance() if QApplication.instance() else QApplication(sys.argv)
super().__init__()
self.file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "temp.html"))
plotly.offline.plot(fig, filename=self.file_path, auto_open=False)
self.load(QUrl.fromLocalFile(self.file_path))
self.setWindowTitle("Plotly Viewer")
self.show()
if exec:
self.app.exec_()
def closeEvent(self, event):
os.remove(self.file_path)
win = PlotlyViewer(fig)
来源:https://stackoverflow.com/questions/53570384/plotly-how-to-make-a-standalone-plot-in-a-window