Created
September 6, 2019 18:13
-
-
Save zencd/daf8fe63ab2b002f719f5b97b99e9550 to your computer and use it in GitHub Desktop.
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
""" | |
REQUIREMENTS: | |
python 3.6+ (tested) | |
pip install plotly==3.3.0 | |
""" | |
import datetime | |
import itertools | |
import logging | |
import numbers | |
import os | |
COLOR_RED = +0.5 | |
COLOR_BLUE = -0.5 | |
DEFAULT_OPACITY = 0.8 | |
class PlotData(object): | |
def __init__(self, title=None): | |
self.xx = [] | |
self.yy = [] | |
self.tips = [] | |
self.colors = [] | |
self.title = title | |
self.title_x = None | |
self.title_y = None | |
def add(self, x, y, tip=None, color=None, title_x=None, title_y=None): | |
color = color if color is not None else 0.0 | |
self.xx.append(x) | |
self.yy.append(y) | |
self.tips.append(tip) | |
self.colors.append(color) | |
if title_x: | |
self.title_x = title_x | |
if title_y: | |
self.title_y = title_y | |
def __len__(self): | |
return len(self.xx) | |
def make_plot_file(title): | |
time = datetime.datetime.now().strftime("%Y-%m-%d %H%M%S") | |
html_simple_name = '%s.html' % time | |
html_file = os.path.join('.', html_simple_name) | |
return html_file | |
def plot_figure(fig, title): | |
import plotly | |
html_file = make_plot_file(title) | |
plotly.offline.plot(fig, filename=html_file, auto_open=True, show_link=False) | |
# plotly.offline.plot(fig, filename=png_file) | |
# fig.write_image("png/fig1.png") | |
def isnumber(x): | |
return (x is not None) and isinstance(x, numbers.Number) | |
def scatter_multi(lines: dict, opacity=0.5): | |
import plotly.graph_objs as go | |
data = [] | |
for key, line in lines.items(): | |
scatter = go.Scatter( | |
mode='markers', | |
x=line['x'], | |
y=line['y'], | |
text=line['tooltip'], | |
name=line['name'], | |
marker={ | |
'opacity': opacity, | |
'color': line['color'], | |
'size': 8, | |
} | |
) | |
data.append(scatter) | |
layout = go.Layout( | |
autosize=False, | |
width=1000, | |
height=550, | |
hovermode='closest', | |
) | |
fig = go.Figure(data=data, layout=layout) | |
plot_figure(fig, 'multi-scatter') | |
def line_multi(plot_datas: list, opacity=0.7): | |
import plotly.graph_objs as go | |
all_data = [] | |
for plot_data in plot_datas: | |
colors = itertools.cycle([ | |
# from https://www.color-hex.com/ | |
# '#ffb3ba', '#bae1ff', '#baffc9', '#ffdfba', '#ffffba' | |
'#6085c9', '#ffa500', '#588759', '#66cccc', '#dec6ee' | |
]) | |
for key in sorted(plot_data.lines.keys()): | |
line = plot_data.lines[key] | |
scatter = go.Scatter( | |
mode=plot_data.mode, | |
x=line.xx, | |
y=line.yy, | |
text=line.tips, | |
name=line.name if line.name else ('%s' % key), | |
marker={ | |
'opacity': opacity, | |
'color': colors.__next__(), | |
# 'size': 8, | |
} | |
) | |
all_data.append(scatter) | |
layout = go.Layout( | |
autosize=False, | |
width=1000, | |
height=550, | |
hovermode='closest', | |
) | |
fig = go.Figure(data=all_data, layout=layout) | |
plot_figure(fig, 'multi-line') | |
def scatter_from_object(data: PlotData): | |
scatter(data.xx, data.yy, data.tips, data.colors, title=data.title, title_x=data.title_x, title_y=data.title_y) | |
def scatter(x: list, y: list, text: list, colors: list, opacity=DEFAULT_OPACITY, title=None, title_x=None, | |
title_y=None): | |
import plotly.graph_objs as go | |
logging.debug('drawing scatter...') | |
if len(x) == 0 or len(x) != len(y): | |
logging.warning('no data supplied for plotting') | |
return | |
scat = go.Scatter( | |
mode='markers', | |
x=x, | |
y=y, | |
text=text, | |
marker=dict( | |
# color='rgba(17, 157, 255, 0.5)', | |
# color=np.random.randn(len(x)), | |
opacity=opacity, | |
color=colors, | |
size=10, | |
# colorbar=colorbar, | |
# colorscale=colorscale, | |
# colorscale='Jet', | |
# line=dict( | |
# color='rgb(231, 99, 250)', | |
# width=8 | |
# ) | |
), | |
# showlegend=False | |
) | |
if len(colors) > 0 and isnumber(colors[0]): | |
scat.marker['colorscale'] = 'Viridis' # 'Jet' also | |
scat.marker['colorbar'] = dict(title='Colorbar') | |
data = [scat] | |
layout = go.Layout( | |
autosize=False, | |
width=800, | |
height=800, | |
hovermode='closest', | |
title=title, | |
xaxis={ | |
'title': title_x if title_x else 'Axe X', | |
}, | |
yaxis={ | |
'title': title_y if title_y else 'Axe Y', | |
}, | |
# margin=go.Margin( | |
# l=50, | |
# r=50, | |
# b=100, | |
# t=100, | |
# pad=4 | |
# ), | |
# paper_bgcolor='#7f7f7f', | |
# plot_bgcolor='#c7c7c7' | |
) | |
fig = go.Figure(data=data, layout=layout) | |
plot_figure(fig, title) | |
if __name__ == '__main__': | |
import random | |
print("hi") | |
data = PlotData(title="Some Title") | |
for i in range(100): | |
x = random.randint(10, 100) | |
y = random.randint(0, 20) | |
color = random.random() | |
data.add(x, y, tip="Some tip", color=color, title_x="Title X", title_y="Title Y") | |
scatter_from_object(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment