Created
March 8, 2024 09:45
-
-
Save Maik93/cbf4c2c0d6d724dea37dbdcd80248c3b to your computer and use it in GitHub Desktop.
Basic Plotly usage for lines and scatters, that can be both interactively shown or stored
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import plotly as pl | |
import plotly.graph_objects as pl_go | |
class PlotlyUtils: | |
def __init__(self): | |
self.figure = pl.graph_objs.Figure() | |
def insert_line(self, data, **kwargs): | |
obj = pl_go.Scatter(data, mode='lines+markers', *kwargs) | |
self.figure.add_trace(obj) | |
def insert_scatter(self, data, **kwargs): | |
obj = pl_go.Scatter(data, mode='markers', *kwargs) | |
self.figure.add_trace(obj) | |
def show(self): | |
""" open a new browser page with the current figure (not storing it anywhere) """ | |
self.figure.update_layout(yaxis=dict(scaleanchor="x", scaleratio=1)) # axis equal | |
self.figure.show() | |
def store_and_show(self, filename): | |
""" store a html file and open it """ | |
self.figure.update_layout(yaxis=dict(scaleanchor="x", scaleratio=1)) # axis equal | |
pl.offline.plot(self.figure, filename=filename) | |
if __name__ == '__main__': | |
data = { | |
'x': [1, 2, 3, 4, 5], | |
'y': [4, 6, 5, 8, 2] | |
} | |
plotly_utils = PlotlyUtils() | |
plotly_utils.insert_line(data) | |
plotly_utils.insert_scatter(data) | |
plotly_utils.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment