def __init__(self, **kwargs): super(VisConfig, self).__init__(**kwargs) self.dtype = np.float # Set Plotly custom variables self.figure_image_filename = "temp-figure" self.figure_image_format = "png" self.figure_filename = "temp-plot.html" # Enable online plotting (default is offline plotting as it works perfectly without any issues) # @see: https://plot.ly/python/getting-started/#initialization-for-online-plotting online_plotting = kwargs.get('online', False) # Detect jupyter and/or ipython environment try: get_ipython from plotly.offline import download_plotlyjs, init_notebook_mode init_notebook_mode(connected=True) self.plotfn = plotly.plotly.iplot if online_plotting else plotly.offline.iplot self.no_ipython = False except NameError: self.plotfn = plotly.plotly.plot if online_plotting else plotly.offline.plot self.no_ipython = True # Get keyword arguments self.display_ctrlpts = kwargs.get('ctrlpts', True) self.display_evalpts = kwargs.get('evalpts', True) self.display_bbox = kwargs.get('bbox', False) self.display_trims = kwargs.get('trims', True) self.display_legend = kwargs.get('legend', True) self.display_axes = kwargs.get('axes', True) self.axes_equal = kwargs.get('axes_equal', True) self.figure_size = kwargs.get('figure_size', [1024, 768]) self.trim_size = kwargs.get('trim_size', 1) self.line_width = kwargs.get('line_width', 2)
def old_show(explanation, selector=None, index_map=None): from plotly.offline import iplot, init_notebook_mode init_notebook_mode(connected=True) # if not show.imported: # show.imported = True if isinstance(selector, str): if index_map is None: print( "If selector is a string, a list or dictionary index_map must be passed." ) if isinstance(index_map, list): selector = index_map.index(selector) elif isinstance(index_map, dict): selector = index_map[selector] else: print("Not supported index_feature_map type. Use list or dictionary.") return None elif isinstance(selector, int): selector = selector elif selector is None: selector = None else: print("Argument 'selector' must be an int, string, or None.") return None fig = explanation.visualize(selector) if fig is not None: iplot(fig) else: print("No overall graph for this explanation. Pass in a selector.")
def _oflline(self): if self._offline_mode: return if self.get_option('mode') == 'offline' and in_ipnb(): from plotly.offline import init_notebook_mode init_notebook_mode() self._offline_mode = True
def show( self, filename: Optional[str] = None, show_link: bool = True, auto_open: bool = True, detect_notebook: bool = True, ) -> None: """Display the chart. Parameters ---------- filename : str, optional Save plot to this filename, otherwise it's saved to a temporary file. show_link : bool, optional Show link to plotly. auto_open : bool, optional Automatically open the plot (in the browser). detect_notebook : bool, optional Try to detect if we're running in a notebook. """ kargs = {} if detect_notebook and _detect_notebook(): py.init_notebook_mode() plot = py.iplot else: plot = py.plot if filename is None: filename = NamedTemporaryFile(prefix='plotly', suffix='.html', delete=False).name kargs['filename'] = filename kargs['auto_open'] = auto_open plot(self, show_link=show_link, **kargs)
def save_plot(fig, file_name=None): try: from plotly.offline import iplot, init_notebook_mode init_notebook_mode(connected=True) iplot(fig, filename=file_name) except: from plotly.offline import plot plot(fig, auto_open=False, filename=file_name)
def _plotly(neuron, plane, title, inline, **kwargs): fig = get_figure(neuron, plane, title) plot_fun = iplot if inline else plot if inline: init_notebook_mode(connected=True) # pragma: no cover plot_fun(fig, filename=title + '.html', **kwargs) return fig
def start160Parralel(): import plotly.plotly as py from plotly.graph_objs import * py.sign_in('username', 'api_key') trace1 = Box( y=[17.107424020767212, 23.18073296546936, 23.362980842590332, 23.812504053115845, 23.346518993377686], boxmean='sd', marker=Marker( color='rgb(8, 81, 156)' ), name='Mininethosts (1W)', ysrc='setchring:103:df6b28' ) trace2 = Box( y=[11.409734010696411, 14.017661094665527, 14.139786958694458, 14.00459909439087, 13.936722993850708], boxmean='sd', marker=Marker( color='rgb(8, 81, 156)' ), name='Mininethosts (2W)', ysrc='setchring:103:e9dd30' ) trace3 = Box( y=[62.070091009140015, 56.22917199134827, 56.44520902633667, 57.48769211769104, 58.8430700302124], boxmean='sd', marker=Marker( color='rgb(10, 140, 208)' ), name='Ubuntu Container (1W)', ysrc='setchring:103:5a50e4' ) trace4 = Box( y=[32.30044984817505, 30.182456016540527, 30.279242038726807, 30.52851104736328, 30.67842388153076], boxmean='sd', marker=Marker( color='rgb(10, 140, 208)' ), name='Ubuntu Container (2W)', ysrc='setchring:103:58e6b9' ) data = Data([trace1, trace2, trace3, trace4]) layout = Layout( title='Startvergleich mit 160 Hosts bzw. Containern' ) layout = Layout( showlegend=False, title='Startvergleich mit 160 Hosts bzw. Containern (Parallel)', yaxis=dict( range=[0, 70], showgrid=True, zeroline=True, title="Startzeit in Sekunden" ), xaxis=dict(tickangle=45)) fig = Figure(data=data, layout=layout) from plotly.offline import init_notebook_mode, plot init_notebook_mode() plot(fig, image='svg', filename='startvergleich160paralel.html')
def _plot(self, grobby, filename): global notebook_mode, notebook_mode_init if notebook_mode: if not notebook_mode_init: ply.init_notebook_mode() notebook_mode_init = True ply.iplot(grobby) else: ply.plot(grobby, filename=filename)
def load_nb(cls, inline=True): """ Loads the plotly notebook resources. """ from IPython.display import publish_display_data cls._loaded = True init_notebook_mode(connected=not inline) publish_display_data(data={MIME_TYPES['jlab-hv-load']: get_plotlyjs()})
def _plot( data, layout, html_only=False, to_file=False, layout_updates=None, plotly_kwarg_updates=None, # kwargs are included as they get passed through from the # plotting accessor method, but are not actually used **kwargs): plotly_kwargs = dict( show_link=False, config={ 'displaylogo': False, 'modeBarButtonsToRemove': ['sendDataToCloud'], }, ) if type_of_script() == 'jupyter': plotter = pltly.iplot plotly_filename_key = 'filename' pltly.init_notebook_mode(connected=False) else: plotter = pltly.plot plotly_filename_key = 'image_filename' plotly_kwargs['auto_open'] = True plotly_kwargs['filename'] = 'temp_plot.html' if layout_updates: layout = {**layout, **layout_updates} if plotly_kwarg_updates: plotly_kwargs = {**plotly_kwargs, **plotly_kwarg_updates} if to_file: filename, image_type = to_file.rsplit('.', 1) # Plotly can only save certain file types if image_type not in ['png', 'jpeg', 'svg', 'webp']: raise TypeError( 'Unable to save plot as `{}`, choose from ' '[`png`, `jpeg`, `svg`, `webp`]'.format(image_type) ) if 'updatemenus' in layout: raise ValueError('Unable to save multiple arrays to SVG, pick one array only') else: plotly_kwargs.update(image=image_type) plotly_kwargs[plotly_filename_key] = filename if data: if html_only: return pltly.plot( {'data': data, 'layout': layout}, include_plotlyjs=False, output_type='div', **plotly_kwargs ) else: plotter({'data': data, 'layout': layout}, **plotly_kwargs) else: raise ValueError('No data to plot.')
def start160Sequenziell(): import plotly.plotly as py from plotly.graph_objs import * py.sign_in('username', 'api_key') trace1 = Box( y=[16.710778951644897, 23.12172794342041, 23.15219497680664, 23.376353979110718, 23.672327041625977], boxmean='sd', marker=Marker( color='rgb(8, 81, 156)' ), name='Mininethosts (1W)', ysrc='setchring:81:588b00' ) trace2 = Box( y=[18.521923065185547, 18.818742036819458, 18.903568029403687, 19.197312116622925, 19.296133041381836], boxmean='sd', marker=Marker( color='rgb(8, 81, 156)' ), name='Mininethosts (2W)', ysrc='setchring:81:188a28' ) trace3 = Box( y=[55.71610999107361, 50.11128902435303, 51.42167401313782, 52.32876491546631, 53.01005816459656], boxmean='sd', marker=Marker( color='rgb(10, 140, 208)' ), name='Ubuntu Container (1W)', ysrc='setchring:81:fb5f3d' ) trace4 = Box( y=[46.878589153289795, 47.398112058639526, 47.66672992706299, 48.80990219116211, 49.06117510795593], boxmean='sd', marker=Marker( color='rgb(10, 140, 208)' ), name='Ubuntu Container (2W)', ysrc='setchring:81:80411e' ) data = Data([trace1, trace2, trace3, trace4]) layout = Layout( showlegend=False, title='Startvergleich mit 160 Hosts bzw. Containern (Sequenziell)', yaxis = dict( range=[0, 60], showgrid=True, zeroline=True, title="Startzeit in Sekunden" ), xaxis=dict(tickangle=45) ) fig = Figure(data=data, layout=layout) from plotly.offline import init_notebook_mode, plot init_notebook_mode() plot(fig, image='svg', filename='startvergleich160seq.html')
def set_export_mode(_exp_mode=True): """ Calling this function will set export mode to true. It is necessary to do so if we use nbconvert to export a notebook containing interactive plotly graphics to HTML. In other words, this function should not be called in normal usage from within Jupyter but only when we export to HTML. It is then called once in the beginning of the notebook.""" global EXPORT_MODE EXPORT_MODE=_exp_mode if EXPORT_MODE: py.init_notebook_mode(connected=False)
def __init__(self, colormap=None, eye=None, zoom=1, init_notebook=True): if eye is None: self.eye = dict(x=1.25, y=1.25, z=1.25) else: self.eye = deepcopy(eye) for key, val in self.eye.items(): self.eye[key] = val / float(zoom) self.camera = dict(eye=self.eye) self.layout = dict(scene=dict(camera=self.camera)) self.cmap = plt.cm.Greys if colormap is None else colormap self.surfacedata = None self.scatterdata = None if init_notebook is True: offl.init_notebook_mode()
def ishow(figs, nb=True, **kwargs): """ Show multiple plotly figures in notebook or on web. """ if isinstance(figs, (list, tuple)): fig_main = figs[0] for fig in figs[1:]: fig_main["data"] += fig["data"] else: fig_main = figs if nb: from plotly.offline import init_notebook_mode from plotly.offline import iplot as plot init_notebook_mode() else: from plotly.plotly import plot plot(fig_main, **kwargs)
def go_offline(connected=None): """ connected : bool If True, the plotly.js library will be loaded from an online CDN. If False, the plotly.js library will be loaded locally from the plotly python package """ from .auth import get_config_file if connected is None: try: connected=True if get_config_file()['offline_connected'] is None else get_config_file()['offline_connected'] except: connected=True if run_from_ipython(): try: py_offline.init_notebook_mode(connected) except TypeError: #For older versions of plotly py_offline.init_notebook_mode() py_offline.__PLOTLY_OFFLINE_INITIALIZED=True
def plot_plotly(data): init_notebook_mode(connected=False) fig = ff.create_candlestick(dates=data.index, open=data.open, high=data.high, low=data.low, close=data.close) fig['layout'].update({ 'title': 'FX 1min', 'yaxis': {'title': 'AUDCAD'}, 'shapes': [{ 'x0': '20114-12-28', 'x1': '2014-12-30', 'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper', 'line': {'color': 'rgb(30,30,30)', 'width': 1} }], 'annotations': [{ 'x': '2014-12-29', 'y': 0.05, 'xref': 'x', 'yref': 'paper', 'showarrow': False, 'xanchor': 'left', 'text': 'Official start of the recession' }] }) plot(fig, filename='simple_candlestick.html', validate=True)
def xray_maker_2(df_xray, bz=50, bx=15, by=15): '''Helper function to generate the x-ray visualization for particle trapping plots.''' print('binning...') init_notebook_mode() xray_query = 'xstop<1000 and tstop< 200 and sqrt(xstop*xstop+ystop*ystop)<900' df_xray.query(xray_query, inplace=True) h, e = np.histogramdd(np.asarray([df_xray.zstop, df_xray.xstop, df_xray.ystop]).T, bins=(bz, bx, by), range=[(3500, 15000), (-900, 900), (-900, 900)]) h_adj = h/h.max() cube_list = [] for i in range(bz): for j in range(bx): for k in range(by): if h_adj[i][j][k] == 0: continue cube_list.append( go.Mesh3d( x=[e[0][i], e[0][i], e[0][i+1], e[0][i+1], e[0][i], e[0][i], e[0][i+1], e[0][i+1]], y=[e[1][j], e[1][j+1], e[1][j+1], e[1][j], e[1][j], e[1][j+1], e[1][j+1], e[1][j]], z=[e[2][k], e[2][k], e[2][k], e[2][k], e[2][k+1], e[2][k+1], e[2][k+1], e[2][k+1]], i=[7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2], j=[3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3], k=[0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6], color='00FFFF', opacity=h_adj[i][j][k]*0.5 ) ) layout = ptrap_layout(title='test xray') fig = go.Figure(data=go.Data(cube_list), layout=layout) iplot(fig)
def go_offline(offline=True): if offline: py_offline.init_notebook_mode() py_offline.__PLOTLY_OFFLINE_INITIALIZED = True else: py_offline.__PLOTLY_OFFLINE_INITIALIZED = False
# coding:utf8 # 这里可以找到各种图例 https://plot.ly/python/ import plotly.offline as py py.init_notebook_mode() # Otherwise, the figure will not be depicted. import plotly.graph_objs as go py.iplot(go.Data([go.Scatter(x=X, y=Y,)])) # 直接在interactive地在jupyter里显示 py.plot(fig, filename='the_graph.html') # 或者直接保存成文件 # 之后可以用加载html的方式显示出来 with open(html_path) as f: display(HTML(f.read())) # convert matplot to plotly # https://plot.ly/matplotlib/modifying-a-matplotlib-figure/ # https://plot.ly/matplotlib/ # # plot... py.iplot_mpl(plt.gcf()) # plot stacked traces = [] cum = None
def set_notebook_mode(): from plotly.offline import init_notebook_mode, iplot global pplot pplot = iplot init_notebook_mode(connected=True)
def mu2e_plot3d_ptrap_anim(df_group1, x, y, z, df_xray, df_group2=None, color=None, title=None): """Generate 3D scatter plots, with a slider widget typically for visualizing 3D positions of charged particles. Generate a 3D scatter plot for a given DF and three columns. Due to the large number of points that are typically plotted, :mod:`matplotlib` is not supported. A slider widget is generated, corresponding to the evolution in time. Notes: * Currently many options are hard-coded. * Use `g.plot(fig)` after executing this function. Args: df_group1 (pandas.DataFrame): The input DF, must contain columns with labels corresponding to the 'x', 'y', and 'z' args. x (str): Name of the first variable. y (str): Name of the second variable. z (str): Name of the third variable. df_xray: (:class:`pandas.DataFrame`): A seperate DF, representing the geometry of the material that is typically included during particle simulation. df_group2: (:class:`pandas.DataFrame`, optional): A seperate DF, representing the geometry of the material that is typically included during particle simulation. color (optional): Being implemented... title (str, optional): Title. Returns: (g, fig) for plotting. """ init_notebook_mode() layout = ptrap_layout(title=title) class time_shifter: def __init__(self, group2=False, color=False): self.x = df_group1[df_group1.sid == sids[0]][x] self.y = df_group1[df_group1.sid == sids[0]][y] self.z = df_group1[df_group1.sid == sids[0]][z] self.group2 = group2 self.docolor = color if self.docolor: self.color = df_group1[df_group1.sid == sids[0]].p if self.group2: self.x2 = df_group2[df_group2.sid == sids[0]][x] self.y2 = df_group2[df_group2.sid == sids[0]][y] self.z2 = df_group2[df_group2.sid == sids[0]][z] if self.docolor: self.color2 = df_group2[df_group2.sid == sids[0]].p def on_time_change(self, name, old_value, new_value): try: self.x = df_group1[df_group1.sid == sids[new_value]][x] self.y = df_group1[df_group1.sid == sids[new_value]][y] self.z = df_group1[df_group1.sid == sids[new_value]][z] if self.docolor: self.color = df_group1[df_group1.sid == sids[new_value]].p except: self.x = [] self.y = [] self.z = [] if self.docolor: self.color = [] if self.group2: try: self.x2 = df_group2[df_group2.sid == sids[new_value]][x] self.y2 = df_group2[df_group2.sid == sids[new_value]][y] self.z2 = df_group2[df_group2.sid == sids[new_value]][z] if self.docolor: self.color2 = df_group2[df_group2.sid == sids[new_value]].p except: self.x2 = [] self.y2 = [] self.z2 = [] if self.docolor: self.color2 = [] self.replot() def replot(self): if self.docolor: g.restyle({'x': [self.x], 'y': [self.y], 'z': [self.z], 'marker': dict(size=5, color=self.color, opacity=1, colorscale='Viridis', cmin=0, cmax=100, showscale=True, line=dict(color='black', width=1), colorbar=dict(title='Momentum (MeV)', xanchor='left')) }, indices=[0]) else: g.restyle({'x': [self.x], 'y': [self.y], 'z': [self.z]}, indices=[0]) if self.group2: if self.docolor: g.restyle({'x': [self.x2], 'y': [self.y2], 'z': [self.z2], 'marker': dict(size=5, color=self.color2, opacity=1, colorscale='Viridis', cmin=0, cmax=100, symbol='x', showscale=True, line=dict(color='black', width=1), colorbar=dict(title='Momentum (MeV)', xanchor='left')), }, indices=[1]) else: g.restyle({'x': [self.x2], 'y': [self.y2], 'z': [self.z2]}, indices=[1]) try: g1_name = df_group1.name except: g1_name = 'Group 1' group2 = False if isinstance(df_group2, pd.DataFrame): group2 = True try: g2_name = df_group2.name except: g2_name = 'Group 1' if group2: if len(df_group1.sid.unique()) > len(df_group2.sid.unique()): sids = np.sort(df_group1.sid.unique()) else: sids = np.sort(df_group2.sid.unique()) else: sids = np.sort(df_group1.sid.unique()) scats = [] if color: mdict = dict(size=5, color=df_group1[df_group1.sid == sids[0]].p, opacity=1, colorscale='Viridis', cmin=0, cmax=100, showscale=True, line=dict(color='black', width=1), colorbar=dict(title='Momentum (MeV)', xanchor='left')) else: mdict = dict(size=5, color='red', opacity=0.7) init_scat = go.Scatter3d( x=df_group1[df_group1.sid == sids[0]][x], y=df_group1[df_group1.sid == sids[0]][y], z=df_group1[df_group1.sid == sids[0]][z], mode='markers', # marker=dict(size=5, color='red', opacity=0.7), marker=mdict, name=g1_name, ) scats.append(init_scat) if group2: if color: mdict2 = dict(size=5, color=df_group2[df_group2.sid == sids[0]].p, opacity=1, colorscale='Viridis', cmin=0, cmax=100, showscale=True, line=dict(color='black', width=1), colorbar=dict(title='Momentum (MeV)', xanchor='left')) else: mdict2 = dict(size=5, color='blue', opacity=0.7) init_scat2 = go.Scatter3d( x=df_group2[df_group2.sid == sids[0]][x], y=df_group2[df_group2.sid == sids[0]][y], z=df_group2[df_group2.sid == sids[0]][z], mode='markers', marker=mdict2, name=g2_name) scats.append(init_scat2) xray_maker(df_xray, scats) p_slider = widgets.IntSlider(min=0, max=500, value=0, step=1, continuous_update=False) p_slider.description = 'Time Shift' p_slider.value = 0 p_state = time_shifter(group2, color) p_slider.on_trait_change(p_state.on_time_change, 'value') fig = go.Figure(data=scats, layout=layout) g = GraphWidget('https://plot.ly/~BroverCleveland/70/') display(g) display(p_slider) return g, fig
def inter_rater(): dict = {} for dir in directories: dict[dir] = None import numpy as np for dir in directories: arr = [] for i in range(1, 19): data = pd.read_csv(result_path + dir + "/" + str(i) + "_head.csv") arr.append(data["label"].values.tolist()) dict[dir] = np.array(arr) from sklearn.metrics import cohen_kappa_score kappas = [] for (userA, dataA) in dict.iteritems(): for (userB, dataB) in dict.iteritems(): dataA = dataA.ravel().ravel() dataB = dataB.ravel().ravel() kappas.append(cohen_kappa_score(dataA, dataB)) kappas.append(0) n_users = len(dict.items()) data = np.array(kappas).reshape((n_users, n_users+1)) annotations = [] keys = dict.keys() anons = [x[0:2] for x in keys] ys = keys # ys.append("empty") xs = keys xs.append("reference") print xs print ys for (x,row) in enumerate(data): for (y, v) in enumerate(row): v= float("{0:.2f}".format(v)) annotations.append(Annotation(text=str(v), x=xs[y],y=keys[x], showarrow=False)) init_notebook_mode() trace = Heatmap(z = data, x=xs, y=keys, colorscale=[[0, "red"],[1,"green"]])#"Greys" data = [trace] layout = Layout( title="Cohen-Kappa score per subject pairs", xaxis=XAxis( title="Anonimized subject" ), yaxis = YAxis( title= "Anonimized subject" ), showlegend=False, height=600, width=600, annotations=annotations, ) fig = Figure(data=data, layout=layout) plot(fig)
from sframe import SFrame from sframe import SArray from sframe import aggregate as agg # In[4]: #-------------------------------------------------- import plotly as plotly print "Plotly version", plotly.__version__ # version >1.9.4 required import plotly.graph_objs as go from plotly import tools # plotly.offline.init_notebook_mode() # run at the start of every notebook from plotly.offline import download_plotlyjs, init_notebook_mode, iplot #, plot # Difference .plot / .iplot ??? init_notebook_mode( ) # run at the start of every ipython notebook to use plotly.offline # this injects the plotly.js source files into the notebook #-------------------------------------------------- # %matplotlib inline # import matplotlib.pyplot as plt # import seaborn as sns #-------------------------------------------------- # --- # # Read data into SFrames # In[4]: usersSF = SFrame.read_csv("%s/users.dat" % DATADIR, delimiter='::', header=False,
def plot(self, mode='weights'): plotly.init_notebook_mode(connected=True) if mode == 'weights': self.plot_weights() elif mode == 'bias': self.plot_bias()
class JSWidget(widgets.DOMWidget): """ A widget for plotting and updating graphs OFFLINE in IPython Notebooks. """ _view_name = Unicode('JSView').tag(sync=True) _view_module = Unicode('JSViewModule').tag(sync=True) _message = Unicode().tag(sync=True) _new_graph_id = Unicode().tag(sync=True) _uid_dict = {} # used for on_completion # Inject JSWidget.js. js_widget_code = resource_string(__name__, 'JSWidget.js').decode('utf-8') display(Javascript(js_widget_code)) # Inject plotly.js (included in plotly). init_notebook_mode(connected=False) def __init__(self, initial_height=None, initial_width=None, **kwargs): super(JSWidget, self).__init__(**kwargs) self._switch = False self.on_msg(self._handle_msg) id = str(uuid.uuid4()) self._graph_id = id self._new_graph_id = id # sends id to JS self._cur_height = initial_height or 600 self._cur_width = initial_width or '100%' self._event_handlers = {} self._unsent_msgs = deque() self._displayed = False def _ipython_display_(self, **kwargs): if self._displayed: print("You can't display the same widget more than once!") else: super(JSWidget, self)._ipython_display_(**kwargs) self._displayed = True # We can finally send the unsent messages. while self._unsent_msgs: msg = self._unsent_msgs.popleft() self._send_message(msg) def _send_message(self, message): if self._displayed: self._message = json.dumps([self._switch, message], cls=utils.PlotlyJSONEncoder) self._switch = not self._switch else: # We can't send the message until the widget has been displayed. self._unsent_msgs.append(message) def _handle_msg(self, message): # ouch: don't change this name! content = message['content']['data']['content'] ctype = content['type'] if ctype == 'on': self._event_handlers[content.event_name]() elif ctype == 'on_completion': callback = self._uid_dict.pop(content['on_completion_id'], False) if callback: callback() else: raise ValueError('Unrecognized ctype: ' + ctype) def on(self, event_name, handler, drop_trace_data=True, remove=False): """ See https://plot.ly/javascript/plotlyjs-events/ handler will be passed a structure data which contains several information relative to the event. If `drop_trace_data` is True, then all the fields `data` and `full_data`, if present, will be dropped. This is useful for efficiency reasons. """ dispatcher = self._event_handlers.get(event_name) if remove: if not dispatcher or event_name not in dispatcher.callbacks: raise Exception('tried to remove a handler never registered') dispatcher.register_callback(handler, remove=True) else: if dispatcher: dispatcher.register_callback(handler) else: dispatcher = widgets.CallbackDispatcher() self._event_handlers[event_name] = dispatcher # The first registration must be done on the frontend as well. message = { 'method': 'on', 'event_name': event_name, 'drop_trace_data': drop_trace_data, 'graphId': self._graph_id } self._send_message(message) def clear_plot(self): """ Note: This calls newPlot which unregisters all your event handlers. """ message = { 'method': 'newPlot', 'data': [], 'layout': {}, 'graphId': self._graph_id, 'delay': 0, } self._send_message(message) # newPlot unregisters all the event handlers. self._event_handlers.clear() def new_plot(self, figure_or_data, validate=True, on_completion=None, delay=0): """ See https://plot.ly/javascript/plotlyjs-function-reference/ Note that JS functions are in camelCase. The only difference is that you need to pass a figure if you want to specify a layout. Note: newPlot unregisters all your event handlers. """ on_completion_id = None if on_completion: on_completion_id = str(uuid.uuid4()) self._uid_dict[on_completion_id] = on_completion figure = tools.return_figure_from_figure_or_data( figure_or_data, validate) message = { 'method': 'newPlot', 'data': figure.get('data', []), 'layout': figure.get('layout', {}), 'graphId': self._graph_id, 'delay': delay, 'on_completion_id': on_completion_id, } self._send_message(message) # newPlot unregisters all the event handlers. self._event_handlers.clear() def restyle(self, update, indices=None): """ See https://plot.ly/javascript/plotlyjs-function-reference/ Note that JS functions are in camelCase. """ message = { 'method': 'restyle', 'update': update, 'graphId': self._graph_id } if indices: message['indices'] = indices self._send_message(message) def relayout(self, layout): """ See https://plot.ly/javascript/plotlyjs-function-reference/ Note that JS functions are in camelCase. """ message = { 'method': 'relayout', 'update': layout, 'graphId': self._graph_id } self._send_message(message) def add_traces(self, traces, new_indices=None): """ See https://plot.ly/javascript/plotlyjs-function-reference/ Note that JS functions are in camelCase. """ message = { 'method': 'addTraces', 'traces': traces, 'graphId': self._graph_id } if new_indices: message['newIndices'] = new_indices self._send_message(message) def delete_traces(self, indices): """ See https://plot.ly/javascript/plotlyjs-function-reference/ Note that JS functions are in camelCase. """ message = { 'method': 'deleteTraces', 'indices': indices, 'graphId': self._graph_id } self._send_message(message) def move_traces(self, current_indices, new_indices=None): """ See https://plot.ly/javascript/plotlyjs-function-reference/ Note that JS functions are in camelCase. """ message = { 'method': 'moveTraces', 'currentIndices': current_indices, 'graphId': self._graph_id } if new_indices: message['newIndices'] = new_indices self._send_message(message) def download_image(self, format, width, height, filename): """ See https://plot.ly/javascript/plotlyjs-function-reference/ Note that JS functions are in camelCase. """ message = { 'method': 'downloadImage', 'imageProperties': dict(format=format, width=width, height=height, filename=filename) } self._send_message(message)
# import numpy as np import pandas as pd import re import nltk #plotly from plotly import __version__ from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot from chart_studio.grid_objs import Grid, Column import plotly.graph_objs as go import chart_studio.plotly as py from plotly import tools init_notebook_mode(connected=True) #LDA import pyLDAvis import pyLDAvis.gensim nltk.download('stopwords') from nltk.corpus import stopwords stop = stopwords.words('english') pyLDAvis.enable_notebook() from gensim import corpora, models from scipy import stats import warnings
@author: Soumen Sarkar """ import pandas as pd import numpy as np import os import matplotlib.pyplot as plt import seaborn as sns import itertools import warnings warnings.filterwarnings("ignore") import io import plotly.offline as py #visualization py.init_notebook_mode(connected=True) #visulatization import plotly.graph_objs as go #visualization import plotly.tools as tls #visualization import plotly.figure_factory as ff accident = pd.read_csv( 'D:/Kaggle-DataSet/Barcelona-Dataset/accidents_2017/accidents_2017.csv') accident.head() #accident.columns=[col.replace(' ', '_').lower() for col in accident.columns] accident.columns = [col.replace(' ', '_') for col in accident.columns] print("Rows :", accident.shape[0]) print("Columns :", accident.shape[1]) print("\nFeatures : \n", accident.columns.tolist()) print("\nMissing Values: ", accident.isnull().sum().values.sum()) print("\nUnique Values: \n", accident.nunique())
def plotplotly_mean_stdev(self, xaxis_label="Time", yaxis_label="Value", title=None, show_title=False, show_legend=True, included_species_list=[], return_plotly_figure=False, ddof=0, **layout_args): """ Plot a plotly graph depicting the mean and standard deviation of a results object :param xaxis_label: The label for the x-axis :type xaxis_label: str :param yaxis_label: The label for the y-axis :type yaxis_label: str :param title: The title of the graph :type title: str :param show_title: If True, title will be shown on graph. :type show_title: bool :param show_legend: Default True, if False, legend will not be shown on graph. :type show_legend: bool :param included_species_list: A list of strings describing which species to include. By default displays all species. :type included_species_list: list :param return_plotly_figure: Whether or not to return a figure dicctionary of data(graph object traces) and layout which may be edited by the user :type return_plotly_figure: bool :param ddof: Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of trajectories. Sample standard deviation uses ddof of 1. Defaults to population standard deviation where ddof is 0. :type ddof: int :param **layout_args: Optional additional arguments to be passed to plotlys layout constructor. :type **layout_args: dict """ # Backwards compatibility with xaxis_label argument (which duplicates plotly's xaxis_title argument) if layout_args.get('xaxis_title') is not None: xaxis_label = layout_args.get('xaxis_title') layout_args.pop('xaxis_title') if layout_args.get('yaxis_title') is not None: yaxis_label = layout_args.get('yaxis_title') layout_args.pop('yaxis_title') average_trajectory = self.average_ensemble().data[0] stddev_trajectory = self.stddev_ensemble(ddof=ddof).data[0] from plotly.offline import init_notebook_mode, iplot import plotly.graph_objs as go init_notebook_mode(connected=True) if not show_title: title = 'Mean and Standard Deviation' else: if title is None: title = (self._validate_title(show_title) + " - Mean and Standard Deviation") trace_list = [] for species in average_trajectory: if species != 'time': if species not in included_species_list and included_species_list: continue upper_bound = [] lower_bound = [] for i in range(0, len(average_trajectory[species])): upper_bound.append(average_trajectory[species][i] + stddev_trajectory[species][i]) lower_bound.append(average_trajectory[species][i] - stddev_trajectory[species][i]) # Append upper_bound list to trace_list trace_list.append( go.Scatter(name=species + ' Upper Bound', x=average_trajectory['time'], y=upper_bound, mode='lines', marker=dict(color="#444"), line=dict(width=1, dash='dot'), legendgroup=str(average_trajectory[species]), showlegend=False)) trace_list.append( go.Scatter( x=average_trajectory['time'], y=average_trajectory[species], name=species, fillcolor='rgba(68, 68, 68, 0.2)', fill='tonexty', legendgroup=str(average_trajectory[species]), )) # Append lower_bound list to trace_list trace_list.append( go.Scatter(name=species + ' Lower Bound', x=average_trajectory['time'], y=lower_bound, mode='lines', marker=dict(color="#444"), line=dict(width=1, dash='dot'), fillcolor='rgba(68, 68, 68, 0.2)', fill='tonexty', legendgroup=str(average_trajectory[species]), showlegend=False)) layout = go.Layout(showlegend=show_legend, title=title, xaxis_title=xaxis_label, yaxis_title=yaxis_label, legend={'traceorder': 'normal'}, **layout_args) fig = dict(data=trace_list, layout=layout) if return_plotly_figure: return fig else: iplot(fig)
def plotplotly(self, index=None, xaxis_label="Time", yaxis_label="Value", title=None, show_title=False, show_legend=True, multiple_graphs=False, included_species_list=[], return_plotly_figure=False, **layout_args): """ Plots the Results using plotly. Can only be viewed in a Jupyter Notebook. :param index: If not none, the index of the Trajectory to be plotted. :type index: int :param xaxis_label: The label for the x-axis :type xaxis_label: str :param yaxis_label: The label for the y-axis :type yaxis_label: str :param title: The title of the graph :type title: str :param show_title: If True, title will be shown on graph. :type show_title: bool :param show_legend: Default True, if False, legend will not be shown on graph. :type show_legend: bool :param multiple_graphs: IF each trajectory should have its own graph or if they should overlap. :type multiple_graphs: bool :param included_species_list: A list of strings describing which species to include. By default displays all species. :type included_species_list: list :param return_plotly_figure: Whether or not to return a figure dictionary of data(graph object traces) and layout which may be edited by the user :type return_plotly_figure: bool :param **layout_args: Optional additional arguments to be passed to plotlys layout constructor. :type **layout_args: dict """ from plotly.offline import init_notebook_mode, iplot import plotly.graph_objs as go # Backwards compatibility with xaxis_label argument (which duplicates plotly's xaxis_title argument) if layout_args.get('xaxis_title') is not None: xaxis_label = layout_args.get('xaxis_title') layout_args.pop('xaxis_title') if layout_args.get('yaxis_title') is not None: yaxis_label = layout_args.get('yaxis_title') layout_args.pop('yaxis_title') init_notebook_mode(connected=True) from collections.abc import Iterable trajectory_list = [] if isinstance(index, Iterable): for i in index: trajectory_list.append(self.data[i]) elif isinstance(index, int): trajectory_list.append(self.data[index]) else: trajectory_list = self.data number_of_trajectories = len(trajectory_list) if title is None: title = self._validate_title(show_title) fig = dict(data=[], layout=[]) if len(trajectory_list) < 2: multiple_graphs = False if multiple_graphs: from plotly import subplots fig = subplots.make_subplots(print_grid=False, rows=int(number_of_trajectories / 2) + int(number_of_trajectories % 2), cols=2) for i, trajectory in enumerate(trajectory_list): if i > 0: trace_list = _plotplotly_iterate( trajectory, trace_list=[], included_species_list=included_species_list, show_labels=False) else: trace_list = _plotplotly_iterate( trajectory, trace_list=[], included_species_list=included_species_list) for k in range(0, len(trace_list)): if i % 2 == 0: fig.append_trace(trace_list[k], int(i / 2) + 1, 1) else: fig.append_trace(trace_list[k], int(i / 2) + 1, 2) fig['layout'].update(autosize=True, height=400 * len(trajectory_list), showlegend=show_legend, title=title) else: trace_list = [] for i, trajectory in enumerate(trajectory_list): if i > 0: trace_list = _plotplotly_iterate( trajectory, trace_list=trace_list, included_species_list=included_species_list, show_labels=False) else: trace_list = _plotplotly_iterate( trajectory, trace_list=trace_list, included_species_list=included_species_list) layout = go.Layout(showlegend=show_legend, title=title, xaxis_title=xaxis_label, yaxis_title=yaxis_label, **layout_args) fig['data'] = trace_list fig['layout'] = layout if return_plotly_figure: return fig else: iplot(fig)
import pandas as pd import numpy as np import plotly.offline as plt import plotly.graph_objs as go plt.init_notebook_mode() import os import random import math from tqdm import tqdm from sklearn.metrics import mean_squared_error from scipy.stats import spearmanr import theano theano.config.compute_test_value = 'raise' from multiprocessing import Pool, cpu_count DATA_DIR = "../data/" SONGS_FILE = "songs.csv" NFEATURE = 15 #Number of Features S = 50 #Hyper Parameter totReco = 0 #Number of total recommendation till now startConstant = 5 #for low penalty in starting phase ###Read data Songs = pd.read_csv(DATA_DIR + SONGS_FILE, index_col=0) ratedSongs = set() def compute_utility(user_features, song_features, epoch, s=S): """ Compute utility U based on user preferences and song preferences """ user_features = user_features.copy()
import networkx as nx from mpl_toolkits.basemap import Basemap import plotly.plotly as py from plotly.graph_objs import * py.sign_in('vpreston', 'tcx2wn8rfb') from plotly.offline import download_plotlyjs, init_notebook_mode, iplot from plotly.offline import plot from plotly.graph_objs import Scatter init_notebook_mode() ############### Imports above to be added to the beginning m = Basemap() def make_scatter(x,y): return Scatter(x=x,y=y,mode='lines',line=Line(color='black'),name=' ') def polygons_to_traces(poly_paths, N_poly): ''' pos arg 1. (poly_paths): paths to polygons pos arg 2. (N_poly): number of polygon to convert ''' traces = [] # init. plotting list for i_poly in range(N_poly): poly_path = poly_paths[i_poly] # get the Basemap coordinates of each segment coords_cc = np.array(
summary_file = os.path.join(basecall_dir, "sequencing_summary.txt") html_outfile = os.path.join(basecall_dir, "pycoQC_summary_plots.html") min_pass_qual = min_qscore # In[13]: from pycoQC.pycoQC import pycoQC from pycoQC.pycoQC_plot import pycoQC_plot # Import helper functions from pycoQC from pycoQC.common import jhelp # Import and setup plotly for offline plotting in Jupyter from plotly.offline import plot, iplot, init_notebook_mode init_notebook_mode(connected=False) # ## Init pycoQC # In[14]: p = pycoQC(summary_file, html_outfile=html_outfile) # In[15]: html_outfile # In[16]: fig = p.summary() iplot(fig, show_link=False)
def display_curve(self, curve='ROC curve', title=None, model_line=None, random_line=None, legend=None): """ Plot the selected curve of this model Args: curve (str): curve to be diplayed, options are 'ROC curve', 'Gain curve', 'Lift curve', 'Purity curve' and 'Precision Recall'. Default is 'ROC curve'. title (str): Title of the diagram. Default is a custom model name model_line (dict): display options of model line, ex: dict(color=('rgb(205, 12, 24)'), dash='dash', width=1). Default is a blue line. see https://plot.ly/python/line-and-scatter/ random_line (dict): display options of random line. Default is a red dash line. legend (dict): legend options, ex: dict(orientation="h") or dict(x=-.1, y=1.2). Default is at the right of the diagram. see https://plot.ly/python/legend/ Returns: plot of the curve """ try: import plotly.graph_objs as go import plotly.offline as py from plotly.offline import init_notebook_mode except ImportError as E: raise ApiException( 'Plotly external package is required for this operation, please execute "!pip install plotly" and restart the kernel', str(E)) if curve not in [ 'ROC curve', 'Gain curve', 'Lift curve', 'Purity curve', 'Precision Recall' ]: print('Unexpected curve type : {}, valid options are : {}'.format( curve, "'ROC curve', 'Gain curve', 'Lift curve', 'Purity curve', 'Precision Recall'" )) return self.__load_confusion_matrix() x, y, x_name, y_name = self.__get_x_y_info(curve) init_notebook_mode(connected=False) if model_line: roc = go.Scatter(x=x, y=y, name='{}'.format(self.name), mode='lines', line=model_line) else: roc = go.Scatter(x=x, y=y, name='{}'.format(self.name), mode='lines') data = [roc] random_line_arg = random_line or dict( color=('rgb(205, 12, 24)'), dash='dash', width=1) if curve == 'ROC curve' or curve == 'Gain curve': random = go.Scatter(x=[0, 1], y=[0, 1], name='Random', mode='lines', line=random_line_arg) elif curve == 'Lift curve': random = go.Scatter(x=[0, 1], y=[1, 1], name='Random', mode='lines', line=random_line_arg) else: random = None if random: data.append(random) default_title = '{} of {}'.format(curve, self.name) if curve == 'ROC curve' or curve == 'Precision Recall': default_title = '{} (AUC = {:0.2f})'.format( default_title, self.__json_confusion_matrix[curve][-1]['auc']) curve_title = title or default_title layout = dict( title=curve_title, xaxis=dict(title=x_name, range=[0, 1]), yaxis=dict(title=y_name, range=[0, max(y) + 0.05]), ) if legend: layout['legend'] = legend fig = dict(data=data, layout=layout) py.iplot(fig, validate=False)
# **This notebook will always be a work in progress.** Please leave any comments about further improvements to the notebook! Any feedback or constructive criticism is greatly appreciated!.** If you like it or it helps you , you can upvote and/or leave a comment :).** # ### Problem Statement # --------------------------------------- # For the locations in which Kiva has active loans, our objective is to pair Kiva's data with additional data sources to estimate the welfare level of borrowers in specific regions, based on shared economic and demographic characteristics. # In[86]: import pandas as pd # package for high-performance, easy-to-use data structures and data analysis import numpy as np # fundamental package for scientific computing with Python import matplotlib import matplotlib.pyplot as plt # for plotting import seaborn as sns # for making plots with seaborn color = sns.color_palette() import plotly.offline as py py.init_notebook_mode(connected=True) import plotly.graph_objs as go import plotly.offline as offline offline.init_notebook_mode() import plotly.tools as tls import squarify # Supress unnecessary warnings so that presentation looks clean import warnings warnings.filterwarnings("ignore") # Print all rows and columns pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) # In[87]:
def widget_emso_qc(wf, depth_range: List[float] = [-10, 10000], range_test: List[float] = [-1000, 1000], spike_window: int = 100, spike_threshold: float = 3.5, spike_influence: float = 0.5, user: str = '', password: str = '', token: str = ''): """ It makes a Jupyter Notebook Widget used to download EMSO data and make data quality control tests. Parameters ---------- wf: WaterFrame depth_range: List[float] Range of depth user: str User for the EMSO ERIC API password: str Password for the EMSO ERIC API token: str Token for the EMSO ERIC API parameter: str Parameter range_test: List[float] Limits for the range test [min value, max value] spike_window: int Window for the spike test spike_threshold: float Float for the spike test spike_influence: float Influence of the bad data on the spike test Returns ------- main_box: ipwidgets.VBox Jupyter notebook widget """ pyo.init_notebook_mode() # Layout config label_width = '7rem' sublabel_width = '5rem' checkbox_width = '8rem' def show_result(wf, parameter_in, chart_title=''): # Change name of flags wf2 = wf.copy() qc_labels = {0: 'No QC', 1: 'Good data', 4: 'Bad data'} wf2.data[f'{parameter_in}_QC'].replace(qc_labels, inplace=True) fig = wf2.iplot_line( parameter_in, # color=f'{parameter_in}_QC', marginal_y=None, line_shape='linear', rangeslider_visible=False, line_dash_sequence=['dot', 'dot'], title=chart_title, ) # line_group='DEPTH') fig.show() # Sing in authentification_title = widgets.HTML('<b>Authentification</b>') user_label = widgets.Label('User:'******'Password:'******'or', layout=widgets.Layout(width=label_width)) token_label = widgets.Label('Token:', layout=widgets.Layout(width=label_width)) input_token = widgets.Text(value=token) # map emso = mooda.EMSO(user=input_user.value, password=input_password.value, token=input_token.value) map_dict = emso.get_fig_map() pio.show(map_dict) # Platform code data_title = widgets.HTML('<b>Downloading data</b>') platform_label = widgets.Label('Platform code:', layout=widgets.Layout(width=label_width)) platform_codes = emso.get_info_platform_code() input_platform = widgets.Dropdown(options=platform_codes, value=platform_codes[0], disabled=False) # Get metadata metadata_list = emso.get_metadata(platform_codes=[platform_codes[0]]) try: metadata = metadata_list[0] parameter_list = metadata.get('parameters') if parameter_list is None: parameter_list = emso.get_info_parameter() depth_min = metadata.get('geospatial_vertical_min', depth_range[0]) depth_max = metadata.get('geospatial_vertical_max', depth_range[1]) start_date = metadata.get('time_coverage_start') if start_date: start_date = start_date.split('T')[0] end_date = metadata.get('time_coverage_end') if end_date: end_date = end_date.split('T')[0] except: parameter_list = emso.get_info_parameter() depth_min = depth_range[0] depth_max = depth_range[1] start_date = None end_date = None # Parameters parameters_label = widgets.Label('Parameters:', layout=widgets.Layout(width=label_width)) input_parameters = widgets.SelectMultiple(options=parameter_list, rows=10, disabled=False, value=[parameter_list[0]]) # Size size_label = widgets.Label('Size:', layout=widgets.Layout(width=label_width)) input_size = widgets.BoundedIntText(value=10, min=1, max=100000, step=1) # Depth depth_label = widgets.Label('Depth:', layout=widgets.Layout(width=label_width)) input_depth = widgets.FloatRangeSlider(value=[depth_min, depth_max], min=depth_min, max=depth_max, step=0.1, disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f') # Time start_date_label = widgets.Label('Start date:', layout=widgets.Layout(width=label_width)) input_start_date = widgets.DatePicker( disabled=False, min_date=date(int(start_date.split('-')[0]), int(start_date.split('-')[1]), int(start_date.split('-')[2])), max_date=date(int(end_date.split('-')[0]), int(end_date.split('-')[1]), int(end_date.split('-')[2])), value=date(int(start_date.split('-')[0]), int(start_date.split('-')[1]), int(start_date.split('-')[2]))) end_date_label = widgets.Label('End date:', layout=widgets.Layout(width=label_width)) input_end_date = widgets.DatePicker( disabled=False, min_date=date(int(start_date.split('-')[0]), int(start_date.split('-')[1]), int(start_date.split('-')[2])), max_date=date(int(end_date.split('-')[0]), int(end_date.split('-')[1]), int(end_date.split('-')[2])), value=date(int(end_date.split('-')[0]), int(end_date.split('-')[1]), int(end_date.split('-')[2]))) qc_label = widgets.HTML('<b>Data QC Tests - Select the test to run</b>') # Layout config checkbox_width = '8rem' # Reset flags # reset_label = widgets.Label('Reset flags:') reset_checkbox = widgets.Checkbox( description='Reset flags', style={'description_width': '0'}, layout=widgets.Layout(width=checkbox_width)) # Flat test flat_checkbox = widgets.Checkbox( description='Flat test', style={'description_width': '0'}, layout=widgets.Layout(width=checkbox_width)) window_label = widgets.Label('Window:', layout=widgets.Layout(width=sublabel_width)) window_flat = widgets.IntText(value=2, disabled=False, color='black') # Range test # range_label = widgets.Label('Range test:') range_checkbox = widgets.Checkbox( description='Range test', style={'description_width': '0'}, layout=widgets.Layout(width=checkbox_width)) limit_label = widgets.Label('Limits:', layout=widgets.Layout(width=sublabel_width)) limits = widgets.FloatRangeSlider(value=[range_test[0], range_test[1]], min=range_test[0], max=range_test[1], step=0.1, disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f') # Spike test # spike_label = widgets.Label('Spike test:') spike_checkbox = widgets.Checkbox( description='Spike test', style={'description_width': '0'}, layout=widgets.Layout(width=checkbox_width)) window_spike = widgets.IntText(value=spike_window, disabled=False, color='black') threshold_label = widgets.Label( 'Threshold:', layout=widgets.Layout(width=sublabel_width)) threshold = widgets.FloatText(value=spike_threshold, disabled=False, color='black') influence_label = widgets.Label( 'Influence:', layout=widgets.Layout(width=sublabel_width)) influence = widgets.FloatSlider(value=spike_influence, min=0, max=1, step=0.1, disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', slider_color='white') # Replace # replace_label = widgets.Label('Replace QC:') replace_checkbox = widgets.Checkbox(description='Replace flags', style={'description_width': '0'}) def update_components(_): # Update all components emso = mooda.EMSO(user=input_user.value, password=input_password.value, token=input_token.value) # Get metadata metadata_list = emso.get_metadata( platform_codes=[input_platform.value]) try: metadata = metadata_list[0] parameter_list = metadata.get('parameters') if parameter_list is None: parameter_list = emso.get_info_parameter() depth_min = metadata.get('geospatial_vertical_min', depth_range[0]) depth_max = metadata.get('geospatial_vertical_max', depth_range[1]) start_date = metadata.get('time_coverage_start') if start_date: start_date = start_date.split('T')[0] end_date = metadata.get('time_coverage_end') if end_date: end_date = end_date.split('T')[0] except: parameter_list = emso.get_info_parameter() depth_min = depth_range[0] depth_max = depth_range[1] start_date = None end_date = None # Parameters input_parameters.options = parameter_list # Depth input_depth.min = depth_min input_depth.max = depth_max input_depth.value = [depth_min, depth_max] # Time input_start_date.min_date = date(int(start_date.split('-')[0]), int(start_date.split('-')[1]), int(start_date.split('-')[2])) input_start_date.max_date = date(int(end_date.split('-')[0]), int(end_date.split('-')[1]), int(end_date.split('-')[2])) input_start_date.value = date(int(start_date.split('-')[0]), int(start_date.split('-')[1]), int(start_date.split('-')[2])) input_end_date.min_date = date(int(start_date.split('-')[0]), int(start_date.split('-')[1]), int(start_date.split('-')[2])) input_end_date.max_date = date(int(end_date.split('-')[0]), int(end_date.split('-')[1]), int(end_date.split('-')[2])) input_end_date.value = date(int(end_date.split('-')[0]), int(end_date.split('-')[1]), int(end_date.split('-')[2])) input_platform.observe(update_components, names='value') # Button button = widgets.Button(description='Get data') out = widgets.Output() out2 = widgets.Output() def on_button_clicked(_): # "linking function with output" with out: # what happens when we press the button clear_output() parameters = [] for input_parameter in input_parameters.value: parameters.append(input_parameter.split('-')[0].strip()) if input_start_date.value is None: start_time = '' else: start_time = str(input_start_date.value) + ' 00:00:00' if input_end_date.value is None: end_time = '' else: end_time = str(input_end_date.value) + ' 00:00:00' if input_token.value is None: token = '' else: token = input_token.value print('Downloading data, please wait') wf2 = mooda.from_emso(platform_code=input_platform.value, parameters=parameters, start_time=start_time, end_time=end_time, depth_min=input_depth.value[0], depth_max=input_depth.value[1], user=input_user.value, password=input_password.value, size=input_size.value, token=token) wf.data = wf2.data.copy() wf.metadata = wf2.metadata.copy() wf.vocabulary = wf2.vocabulary.copy() clear_output() display(wf) with out2: # what happens when we press the button clear_output() print('Making tests') for parameter_one in wf.parameters: if reset_checkbox.value: wf.qc_replace(parameters=[parameter_one], to_replace=1, value=0) wf.qc_replace(parameters=[parameter_one], to_replace=4, value=0) if flat_checkbox.value: wf.qc_flat_test(window=window_flat.value, parameters=[parameter_one]) if range_checkbox.value: wf.qc_range_test(limits=limits.value, parameters=[parameter_one]) if spike_checkbox.value: wf.qc_spike_test(window=window_spike.value, influence=influence.value, threshold=threshold.value, parameters=[parameter_one]) if replace_checkbox.value: start = 0 if spike_checkbox.value: start = window_spike.value wf.qc_replace(parameters=[parameter_one], start=start) show_result(wf, parameter_one, f'Final result') # linking button and function together using a button's method button.on_click(on_button_clicked) user_box = widgets.HBox([user_label, input_user]) password_box = widgets.HBox([password_label, input_password]) token_box = widgets.HBox([token_label, input_token]) platform_box = widgets.HBox([platform_label, input_platform]) parameters_box = widgets.HBox([parameters_label, input_parameters]) size_box = widgets.HBox([size_label, input_size]) depth_box = widgets.HBox([depth_label, input_depth]) start_date_box = widgets.HBox([start_date_label, input_start_date]) end_date_box = widgets.HBox([end_date_label, input_end_date]) reset_box = widgets.HBox([reset_checkbox]) flat_box = widgets.HBox([flat_checkbox, window_label, window_flat]) range_box = widgets.HBox([range_checkbox, limit_label, limits]) spike_window_column = widgets.HBox([window_label, window_spike]) spike_threshold_column = widgets.HBox([threshold_label, threshold]) spike_influence_column = widgets.HBox([influence_label, influence]) spike_column = widgets.VBox( [spike_window_column, spike_threshold_column, spike_influence_column]) spike_box = widgets.HBox([spike_checkbox, spike_column]) replace_box = widgets.HBox([replace_checkbox]) main_box = widgets.VBox([ authentification_title, user_box, password_box, or_label, token_box, data_title, platform_box, parameters_box, size_box, depth_box, start_date_box, end_date_box, qc_label, reset_box, flat_box, range_box, spike_box, replace_box, button, out, out2 ]) return main_box
import plotly.graph_objs as go import numpy as np from ipywidgets import interact from plotly import offline offline.init_notebook_mode() #%% fig = go.FigureWidget() scatt = fig.add_scatter() fig xs=np.linspace(0, 6, 100) #%% @interact(a=(1.0, 4.0, 0.01), b=(0, 10.0, 0.01), color=['red', 'green', 'blue']) def update(a=3.6, b=4.3, color='blue'): with fig.batch_update(): scatt.x=xs scatt.y=np.sin(a*xs-b) scatt.line.color=color #%% from plotly import offline offline.init_notebook_mode() offline.iplot([{"y": [1, 2, 1]}])
def fs_plot_data(self, plot_type = 'mpl', bz_linewidth= 0.9, axis_labels = True, symmetry_labels = True, color_list = None, title_str = None): """Function for producing a plot of the Fermi surface. Will edit this to allow for custom plotting options. """ meshes = [] if plot_type == 'mpl': fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111, projection='3d') rlattvec = self._brillouin_zone._rlattvec # specify the colour list for plotting the bands. # Below is the deafult, can be overidden by specifying 'colour' in calling function. if color_list is None: color_list = plt.cm.Set1(np.linspace(0, 1, 9)) # create a mesh for each electron band which has an isosurface at the Fermi energy. # mesh data is generated by a marching cubes algorithm when the FermiSurface object is created. for i, band in enumerate(self._fermi_surface._iso_surface, 0): verts = band[0] faces = band[1] grid = [np.array([item[0] for item in verts]), np.array([item[1] for item in verts]), np.array([item[2] for item in verts])] # reposition the vertices so that the cente of the Brill Zone is at (0,0,0) # include if want gamma at centre of plot, along with "rearrange_data method in bulk_objects" # verts -= np.array([grid[0].max()/2,grid[1].max()/2,grid[2].max()/2]) # create a mesh and add it to the plot mesh = Poly3DCollection(verts[faces], linewidths=0.1) mesh.set_facecolor(color_list[i]) mesh.set_edgecolor('lightgray') ax.add_collection3d(mesh) # add the Brillouin Zone outline to the plot corners = self._recip_cell._faces ax.add_collection3d(Line3DCollection(corners, colors='k', linewidths=bz_linewidth)) # add high-symmetry points to the plot sym_pts = self._symmetry_pts x, y, z = zip(*sym_pts[0]) # Placeholders for the high-symmetry points in cartesian coordinates x_cart, y_cart, z_cart = [], [], [] # Convert high-symmetry point coordinates into cartesian, and shift to match mesh position for i in x: if i < 0: i = np.linalg.norm([rlattvec[:,0]])*((0.5+i)+0.5) else: i = np.linalg.norm(rlattvec[:,0])*(i) x_cart.append(i) for i in y: if i < 0: i = np.linalg.norm(rlattvec[:,1])*((0.5+i)+0.5) else: i = np.linalg.norm(rlattvec[:,1])*(i) y_cart.append(i) for i in z: if i < 0: i = np.linalg.norm(rlattvec[:,2])*((0.5+i)+0.5) else: i = np.linalg.norm(rlattvec[:,2])*(i) z_cart.append(i) ax.scatter(x_cart, y_cart, z_cart, s=10, c='k') for i, txt in enumerate(sym_pts[1]): ax.text(x_cart[i],y_cart[i],z_cart[i], '%s' % (txt), size=15, zorder=1, color='k') ax.axis('off') if title_str is not None: plt.title(title_str) ax.set_xlim(0, np.linalg.norm(rlattvec[:,0])+1) ax.set_ylim(0, np.linalg.norm(rlattvec[:,1])+1) ax.set_zlim(0, np.linalg.norm(rlattvec[:,2])+1) plt.tight_layout() plt.show() elif plot_type == 'plotly': init_notebook_mode(connected=True) # plotly.tools.set_credentials_file(username='******', api_key='PiOwBRIRWKGJWIFXe1vT') rlattvec = self._recip_cell._rlattvec # Different colours # colors = [ # '#1f77b4', # muted blue # '#ff7f0e', # safety orange # '#2ca02c', # cooked asparagus green # '#d62728', # brick red # '#9467bd', # muted purple # '#8c564b', # chestnut brown # '#e377c2', # raspberry yogurt pink # '#7f7f7f', # middle gray # '#bcbd22', # curry yellow-green # '#17becf', # blue-teal # '#00FFFF' #another blue teal # '#1f77b4', # muted blue # '#ff7f0e', # safety orange # '#2ca02c', # cooked asparagus green # '#d62728', # brick red # '#9467bd', # muted purple # '#8c564b', # chestnut brown # '#e377c2', # raspberry yogurt pink # '#7f7f7f', # middle gray # '#bcbd22', # curry yellow-green # '#17becf', # blue-teal # '#00FFFF' #another blue teal # ] colors = cl.scales['11']['qual']['Set3'] # create a mesh for each electron band which has an isosurface at the Fermi energy. # mesh data is generated by a marching cubes algorithm when the FermiSurface object is created. for i, band in enumerate(self._fermi_surface._iso_surface, 0): verts = band[0] faces = band[1] grid = [np.array([np.abs(item[0]) for item in verts]), np.array([np.abs(item[1]) for item in verts]), np.array([np.abs(item[2]) for item in verts])] x, y, z = zip(*verts) I, J, K = ([triplet[c] for triplet in faces] for c in range(3)) trace = go.Mesh3d(x=x, y=y, z=z, color=colors[i], opacity = 0.9, i=I, j=J, k=K) meshes.append(trace) # add the Brillouin Zone outline to the plot corners = self._recip_cell._faces for facet in corners: x, y, z = zip(*facet) trace = go.Scatter3d(x=x, y=y, z=z, mode = 'lines', line=dict( color='black', width=3 )) meshes.append(trace) # add the high symmetry points to the plot sym_pts = self._symmetry_pts # Get text labels into Latex form labels = [] for i in sym_pts[1]: labels.append(sympy.latex(i)) x, y, z = zip(*sym_pts[0]) # Placeholders for the high-symmetry points in cartesian coordinates x_cart, y_cart, z_cart = [], [], [] # Convert high-symmetry point coordinates into cartesian, and shift to match mesh position for i in x: if i < 0: i = np.linalg.norm([rlattvec[:,0]])*((0.5+i)+0.5) else: i = np.linalg.norm(rlattvec[:,0])*(i) x_cart.append(i) for i in y: if i < 0: i = np.linalg.norm(rlattvec[:,1])*((0.5+i)+0.5) else: i = np.linalg.norm(rlattvec[:,1])*(i) y_cart.append(i) for i in z: if i < 0: i = np.linalg.norm(rlattvec[:,2])*((0.5+i)+0.5) else: i = np.linalg.norm(rlattvec[:,2])*(i) z_cart.append(i) trace = go.Scatter3d(x=x_cart, y=y_cart, z=z_cart, mode='markers+text', marker = dict(size = 5, color = 'black' ), name='Markers and Text', text = labels, textposition='bottom center' ) meshes.append(trace) # Specify plot parameters layout = go.Layout(scene = dict(xaxis=dict(title = '', showgrid=True, zeroline=True, showline=False, ticks='', showticklabels=False), yaxis=dict(title = '', showgrid=True, zeroline=True, showline=False, ticks='', showticklabels=False), zaxis=dict(title = '', showgrid=True, zeroline=True, showline=False, ticks='', showticklabels=False)), showlegend=False, title=go.layout.Title( text=title_str, xref='paper', x=0 )) plot(go.Figure(data=meshes,layout= layout), include_mathjax='cdn') else: raise ValueError( "The type you have entered is not a valid option for the plot_type parameter." "Please enter one of {'mpl', 'plotly'} for a matplotlib or plotly-type plot" " respectively. For an interactive plot 'plotly' is recommended.")
def plotBoxes(): import plotly.plotly as py import plotly.graph_objs as go trace0 = go.Box( y=[ 1.220966100692749, 1.194838047027588, 1.1900928020477295, 1.1873359680175781, 1.1871559619903564 ], name='10 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace1 = go.Box( y=[ 3.0607070922851562, 2.8704521656036377, 2.8041749000549316, 2.836987018585205, 2.9370028972625732 ], name='10 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) trace2 = go.Box( y=[ 2.177574872970581, 2.158424139022827, 2.41457200050354, 2.20817494392395, 2.299355983734131 ], name='20 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace3 = go.Box( y=[ 5.638598918914795, 5.547015905380249, 5.462399005889893, 5.517163038253784, 5.487725019454956 ], name='20 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) trace4 = go.Box( y=[ 4.118584156036377, 4.858264923095703, 4.982507944107056, 5.070256948471069, 4.854703903198242 ], name='40 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace5 = go.Box( y=[ 12.242072105407715, 11.051261901855469, 11.07240915298462, 11.05888319015503, 11.168780088424683 ], name='40 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) trace6 = go.Box( y=[ 8.297120094299316, 10.70746111869812, 11.330874919891357, 11.29092288017273, 11.357799053192139 ], name='80 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace7 = go.Box( y=[ 25.65382409095764, 22.612013816833496, 22.563922882080078, 22.958635807037354, 23.199914932250977 ], name='80 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) trace8 = go.Box( y=[ 16.710778951644897, 23.12172794342041, 23.15219497680664, 23.376353979110718, 23.672327041625977 ], name='160 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace9 = go.Box( y=[ 55.71610999107361, 50.11128902435303, 51.42167401313782, 52.32876491546631, 53.01005816459656 ], name='160 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) # trace10 = go.Box( # y=[ # 25.571568965911865, # 36.26844000816345, # 36.47072696685791, # 36.47522687911987, # 36.80563712120056 # ], # name='240 Hosts', # marker=dict( # color='rgb(8, 81, 156)', # ), # boxmean='sd' # ) # trace11 = go.Box( # y=[ # 88.68352913856506, # 80.14780402183533, # 82.73843598365784, # 84.35875296592712, # 86.76403188705444 # ], # name='240 Container', # marker=dict( # color='rgb(10, 140, 208)', # ), # boxmean='sd' # ) # trace12 = go.Box( # y=[ # 30.141373872756958, # 42.5898220539093, # 43.233330965042114, # 44.23212289810181, # 44.816850900650024 # ], # name='280 Hosts', # marker=dict( # color='rgb(8, 81, 156)', # ), # boxmean='sd' # ) # trace13 = go.Box( # y=[ # 107.80886006355286, # 96.45325303077698, # 99.27463698387146, # 102.14648795127869, # 104.41336393356323 # ], # name='280 Container', # marker=dict( # color='rgb(10, 140, 208)', # ), # boxmean='sd' # ) trace14 = go.Box( y=[ 34.71859788894653, 48.50111699104309, 49.38033485412598, 51.087302923202515, 51.66889476776123 ], name='320 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace15 = go.Box( y=[-1], name='320 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) data = [trace0, trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8, trace9, trace14, trace15] layout = go.Layout(title='Startzeiten von Hosts bzw. Containern mit einem Worker', showlegend=False, yaxis=dict( title="Startzeit in Sekunden" )) figure = go.Figure(data=data, layout=layout) # py.plot(figure, auto_open=False) # py.iplot(figure, filename='svg75') from plotly.offline import init_notebook_mode, plot init_notebook_mode() plot(figure, image='svg', filename='einWorker.html')
def track_plot_list(list_of_df=[], **kwargs): global pivot, shift n_tracks = 1 title = 'Track plots' path = 'chart.html' opacity = 0.5 marker_size = 3 line_size = 3 len_list_df = len(list_of_df) list_of_colors = [ 'red', 'blue', 'green', 'magenta', 'chocolate', 'teal', 'indianred', 'yellow', 'orange', 'silver' ] assert (len_list_df <= len(list_of_colors) ), 'The list must contain less than 10 dataframes.' if kwargs.get('n_tracks'): n_tracks = kwargs.get('n_tracks') if n_tracks > list_of_df[0].shape[0]: n_tracks = abs(list_of_df[0].shape[0]) wrn_msg = ( 'The number of tracks to plot is greater than the number of tracks in ' 'the dataframe.\nn_tracks will be: ' + str(n_tracks) + ' (the number of tracks in the dataset)') warnings.warn(wrn_msg, RuntimeWarning, stacklevel=2) if kwargs.get('pivot'): pivot = kwargs.get('pivot') if kwargs.get('title'): title = kwargs.get('title') if kwargs.get('opacity'): opacity = kwargs.get('opacity') if opacity > 1.0: opacity = 1.0 wrn_msg = ('The opacity value is greater than 1.0\n' 'The opacity value is will be set with 1.0 value.') warnings.warn(wrn_msg, RuntimeWarning, stacklevel=2) if kwargs.get('marker_size'): marker_size = abs(kwargs.get('marker_size')) if kwargs.get('line_size'): line_size = abs(kwargs.get('line_size')) dft_size = int(list_of_df[0].shape[1]) len_xyz = int(dft_size / pivot) # Initializing lists of indexes selected_columns_x = np.zeros(len_xyz) selected_columns_y = np.zeros(len_xyz) selected_columns_z = np.zeros(len_xyz) # Generating indexes for i in range(len_xyz): selected_columns_x[i] = int(i * pivot + 0 + shift) selected_columns_y[i] = int(i * pivot + 1 + shift) selected_columns_z[i] = int(i * pivot + 2 + shift) # list of data to plot data = [] track = [None] * n_tracks for i in range(len_list_df): try: #df_name = str(list_of_df[i].name) df_name = str(list_of_df[i].columns.name) except: df_name = 'track[' + str(i) + ']' warnings.warn( 'For a better visualization, set the name of dataframe to plot:' '\nE.g.: df.name = \'track original\'', RuntimeWarning, stacklevel=2) for j in range(n_tracks): track[j] = go.Scatter3d( # Removing null values (zeroes) in the plot x=list_of_df[i].replace(0.0, np.nan).iloc[j, selected_columns_x], y=list_of_df[i].replace(0.0, np.nan).iloc[j, selected_columns_y], z=list_of_df[i].replace(0.0, np.nan).iloc[j, selected_columns_z], name=df_name + ' ' + str(j), opacity=opacity, marker=dict( size=marker_size, opacity=opacity, color=list_of_colors[i], ), line=dict(color=list_of_colors[i], width=line_size)) # append the track[i] in the list for plotting data.append(track[j]) layout = dict( #width = 900, #height = 750, autosize=True, title=title, scene=dict(xaxis=dict(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)', title='x (mm)'), yaxis=dict(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)', title='y (mm)'), zaxis=dict(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)', title='z (mm)'), camera=dict(up=dict(x=1, y=1, z=-0.5), eye=dict( x=-1.7428, y=1.0707, z=0.7100, )), aspectratio=dict(x=1, y=1, z=1), aspectmode='manual'), ) fig = go.Figure(data=data, layout=layout) init_notebook_mode(connected=True) if kwargs.get('path'): path = kwargs.get('path') fig.write_html(path, auto_open=True) else: iplot(fig)
def systemLastVergleich(): trace1 = Box( y=[174.05624198913574, 173.948890209198, 173.94454908370972, 173.9467408657074, 173.94518518447876, 173.95012092590332, 173.95043110847473, 173.94655680656433, 174.53029203414917, 173.9404320716858, 173.9495029449463, 173.95225501060486, 173.94485592842102, 173.94097995758057, 173.96589398384094, 173.9597988128662, 173.94549107551575, 173.9661078453064, 174.53516697883606, 173.98816299438477], boxmean='sd', marker=Marker( color='rgb(8, 81, 156)' ), name='2 Mininet Hosts (1W)', ysrc='setchring:97:12cf8c' ) trace2 = Box( y=[174.0329999923706, 173.96394085884094, 173.95319485664368, 173.95863509178162, 173.94559407234192, 173.95078206062317, 173.96819806098938, 173.94542288780212, 174.57363319396973, 173.9398968219757, 173.95656490325928, 173.9443531036377, 173.94898891448975, 173.95273303985596, 173.9473750591278, 173.95579290390015, 173.95104503631592, 173.97713804244995, 174.54663395881653, 173.96244096755981], boxmean='sd', marker=Marker( color='rgb(10, 140, 208)' ), name='2 Ubuntu Container (1W)', ysrc='setchring:97:28e33b' ) trace3 = Box( y=[87.11957716941833, 87.00569295883179, 87.00749588012695, 87.01642107963562, 87.00478601455688, 87.01821398735046, 87.0056049823761, 87.00564479827881, 87.00377297401428, 87.00858116149902, 87.01909303665161, 87.01344585418701, 87.00871896743774, 87.01720023155212, 87.00548887252808, 87.00659012794495, 87.00090003013611, 87.62443900108337, 87.60383987426758, 87.0222909450531], boxmean='sd', marker=Marker( color='rgb(8, 81, 156)' ), name='2 Mininet Hosts (2W)', ysrc='setchring:97:755dbf' ) trace4 = Box( y=[87.10864400863647, 87.03255987167358, 87.00552487373352, 87.00663709640503, 86.99984693527222, 87.00633597373962, 87.00061416625977, 87.0000250339508, 86.99881911277771, 87.00222516059875, 87.00338101387024, 87.00585508346558, 87.00474500656128, 87.01482605934143, 87.00232291221619, 87.00039505958557, 87.00300908088684, 87.0094940662384, 87.60664296150208, 87.00194597244263], boxmean='sd', marker=Marker( color='rgb(10, 140, 208)' ), name='2 Ubuntu Container (2W)', ysrc='setchring:97:056208' ) data = Data([trace1, trace2, trace3, trace4]) layout = Layout( showlegend=False, title='CPU Benchmark Laufzeiten von Hosts bzw. Containern', yaxis=dict( range=[0, 180], showgrid=True, zeroline=True, title="Laufzeit in Sekunden" ), xaxis=dict(tickangle=45) ) fig = Figure(data=data, layout=layout) from plotly.offline import init_notebook_mode, plot init_notebook_mode() plot(fig, image='svg', filename='SystemLastVergleich.html')
# Visualization # seaborn import seaborn as sns sns.set(rc={'figure.figsize':(10,5)}) import matplotlib.pyplot as plt import matplotlib # increase size of standar plots plt.rcParams["figure.figsize"] = [10, 5] #plt.style.use('ggplot') # plotly import plotly import plotly.graph_objs as go from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot init_notebook_mode(connected=True) # plot save paths print(f''' python\t{sys.version} --------------------- Versions: ---------------------- pandas {pd.__version__} numpy {np.__version__} matplotlib {matplotlib.__version__} seaborn {sns.__version__} plotly {plotly.__version__} ---------------------- ''')
def graph_from_table(table_name): ''' This function creates an interactive plotly graph based on a Qtable taken as input. ''' # Putting this here so we don't have to worry about it in the notebooks init_notebook_mode(connected=True) types = ['scatter', 'line', 'bar'] try: updated_table = table_name.get_changed_df() except NameError: print( "The variable " + str(name) + " does not exist. Please make sure you entered the correct table" ) return def graph(x_axes, y_axes, kind, title='', y_name='', x_name=''): ''' This function is to simplify creating a graph from an interactive Qtable and allows us to use it with IPywidgets interactive ''' # Choose plot type if kind == 'line': trace = Scatter(x=updated_table[x_axes], y=updated_table[y_axes], mode='lines+markers') if kind == 'bar': trace = Bar(x=updated_table[x_axes], y=updated_table[y_axes]) if kind == 'scatter': trace = Scatter(x=updated_table[x_axes], y=updated_table[y_axes], mode='markers') # Choose axes stuff, as well as create defaults if x_name != '': x_lab = x_name else: x_lab = "Column: " + x_axes # if y_name != '': y_lab = y_name else: y_lab = "Column: " + y_axes layout = Layout(title=title, xaxis=dict(title=x_lab), yaxis=dict(title=y_lab)) fig = Figure(data=Data([trace]), layout=layout) iplot(fig) # Now we need to specify our widgets x_widget = widgets.Dropdown(options=updated_table.columns.tolist(), value=updated_table.columns.tolist()[0], description='X axis data', disabled=False, width='250px') y_widget = widgets.Dropdown(options=updated_table.columns.tolist(), value=updated_table.columns.tolist()[1], description='Y axis data', disabled=False) kind_widget = widgets.Dropdown(options=types, value=types[0], description='Plot Type', disabled=False) title_widget = widgets.Text(value='', placeholder='Enter plot title', description='Plot title:', disabled=False, continuous_update=False) x_name_widget = widgets.Text(value='', placeholder='X axis title', description='X axis label:', disabled=False, continuous_update=False) y_name_widget = widgets.Text(value='', placeholder='Y axis title', description='Y axis label:', disabled=False, continuous_update=False) graph_widg = interactive(graph, x_axes=x_widget, y_axes=y_widget, kind=kind_widget, title=title_widget, x_name=x_name_widget, y_name=y_name_widget) box_layout = widg_layout(display='flex', flex_flow='column', align_items='center', border='solid', width='100%') # Set up widget layout. left_box = widgets.VBox([graph_widg.children[i] for i in range(3)]) right_box = widgets.VBox([graph_widg.children[i] for i in range(3, 6)]) widget_layout = widgets.HBox([left_box, right_box]) # graph_widg.children[-1] is the output of interactive (plotly plot) to_display = widgets.VBox([graph_widg.children[-1], widget_layout], layout=box_layout) graph_widg.children = [to_display] display.display(graph_widg)
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot import plotly import plotly.graph_objs as go init_notebook_mode(connected=True)
# In[57]: one_genre = movie_rel[movie_rel.count_of_genres == 1] one_genre['budget'] = one_genre['budget'].astype('int') # In[66]: from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot import plotly import plotly.graph_objs as go init_notebook_mode(connected=True) #инициализация методов # In[120]: rcParams['figure.figsize'] = 8, 6 genre_vs_budget1 = one_genre.loc[:, ['genres_per_one', 'budget', 'year']].groupby(by=['genres_per_one'])[['budget']].sum() #genre_vs_budget2 = one_genre.loc[:, ['genres_per_one', 'budget']].groupby(by=['genres_per_one']).sum() #data = genre_vs_budget2.join(genre_vs_budget1) genre_vs_budget1.plot(kind='bar', rot=45, label='year') # In[144]:
def plotBoxes2(): import plotly.plotly as py import plotly.graph_objs as go trace0 = go.Box( y=[ 1.35548996925354, 1.3441359996795654, 1.3321261405944824, 1.3552589416503906, 1.4191999435424805 ], name='10 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace1 = go.Box( y=[ 3.0793659687042236, 3.071715831756592, 2.9316048622131348, 2.977012872695923, 3.0160839557647705 ], name='10 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) trace2 = go.Box( y=[ 2.4199891090393066, 2.305997133255005, 2.3410701751708984, 2.398021936416626, 2.390981912612915 ], name='20 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace3 = go.Box( y=[ 5.805190086364746, 5.519806861877441, 5.623938083648682, 5.696949005126953, 5.692356109619141 ], name='20 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) trace4 = go.Box( y=[ 4.37345290184021, 4.27826714515686, 4.418522119522095, 4.3067498207092285, 4.399266004562378 ], name='40 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace5 = go.Box( y=[ 10.92971396446228, 10.761906862258911, 10.886162996292114, 11.101104974746704, 10.988735914230347 ], name='40 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) trace6 = go.Box( y=[ 8.743327856063843, 8.655654907226562, 8.607124090194702, 8.785256147384644, 8.714077949523926 ], name='80 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace7 = go.Box( y=[ 22.36103916168213, 22.424012899398804, 22.47895622253418, 22.36395001411438, 22.528213024139404 ], name='80 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) trace8 = go.Box( y=[ 18.521923065185547, 18.818742036819458, 18.903568029403687, 19.197312116622925, 19.296133041381836 ], name='160 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace9 = go.Box( y=[ 46.878589153289795, 47.398112058639526, 47.66672992706299, 48.80990219116211, 49.06117510795593 ], name='160 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) # trace10 = go.Box( # y=[ # 29.72091507911682, # 30.075870037078857, # 30.25898790359497, # 30.211423873901367, # 30.677078008651733 # ], # name='240 Hosts', # marker=dict( # color='rgb(8, 81, 156)', # ), # boxmean='sd' # ) # trace11 = go.Box( # y=[ # 74.89307904243469, # 74.9702980518341, # 76.87759709358215, # 78.20884490013123, # 79.46793007850647 # ], # name='240 Container', # marker=dict( # color='rgb(10, 140, 208)', # ), # boxmean='sd' # ) # trace12 = go.Box( # y=[ # 36.64970803260803, # 36.93167185783386, # 37.233668088912964, # 37.48106002807617, # 37.63134694099426 # ], # name='280 Hosts', # marker=dict( # color='rgb(8, 81, 156)', # ), # boxmean='sd' # ) # trace13 = go.Box( # y=[ # 89.43371391296387, # 92.05170917510986, # 93.3434431552887, # 95.22685503959656, # 96.73871803283691 # ], # name='280 Container', # marker=dict( # color='rgb(10, 140, 208)', # ), # boxmean='sd' # ) trace14 = go.Box( y=[ 42.13955593109131, 35.43569993972778, 35.852705001831055, 35.9057719707489, 36.216508865356445 ], name='320 Hosts', marker=dict( color='rgb(8, 81, 156)', ), boxmean='sd' ) trace15 = go.Box( y=[ 99.17356204986572, 97.3440489768982, 98.98178291320801, 100.91142106056213, 102.85055303573608 ], name='320 Container', marker=dict( color='rgb(10, 140, 208)', ), boxmean='sd' ) data = [trace0, trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8, trace9, trace14, trace15] layout = go.Layout(title='Startzeiten von Hosts bzw. Containern mit zwei Workern', showlegend=False,yaxis=dict( title="Startzeit in Sekunden" )) figure = go.Figure(data=data, layout=layout) # py.plot(figure, auto_open=False) from plotly.offline import init_notebook_mode, plot init_notebook_mode() plot(figure, image='svg', filename='zweiWorker.html')
def simulate(planet_radius, star_radius, inclination, imsize=(400, 400)): if planet_radius > star_radius: print("Can the planet's radius really be greater than the stars radius?") return if star_radius > 200: print("Please choose the star's radius to be less than 200") return if planet_radius > 200: print("Please choose the planet's radius to be less than 200") return if inclination > 200: print("Please choose an inclination less than 200 or -200") return else: time, flux = lightcurve(planet_radius, star_radius, i=inclination, imsize=imsize) dfs = pd.DataFrame({'time': time, 'flux': flux, 'star_radius': star_radius, 'planet_radius': planet_radius}) first_dip = np.where(np.diff(flux) < 0)[0][0] first_full = np.where(np.diff(flux) < 0)[0][-1] sec_dip = np.where(np.diff(flux) > 0)[0][0] sec_full = np.where(np.diff(flux) > 0)[0][-1] init_notebook_mode(connected = True) fig = go.Figure( data=[go.Scatter(x=dfs.time.values, y=dfs.flux.values, mode="lines", name="Imaginary Planet", line=dict(width=2, color="blue")), go.Scatter(x=dfs.time.values, y=dfs.flux.values, mode="lines", name="Light from a Star", line=dict(width=2, color="blue"))], layout=go.Layout( xaxis=dict(range=[0, imsize[0]], autorange=False, zeroline=False), yaxis=dict(range=[0, 1.3], autorange=False, zeroline=False), title_text="Exoplanet Transit", hovermode="closest", xaxis_title='Time', yaxis_title='f, Flux', updatemenus=[dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None])])]), frames=[go.Frame( data=[go.Scatter( x=[dfs.time.values[::5][k]], y=[dfs.flux.values[::5][k]], mode="markers", marker=dict(color="red", size=10))]) for k in range(dfs.time.values[::5].shape[0])] ) fig.update_layout( showlegend=False, annotations=[ dict( x=time[first_dip], y=flux[first_dip], xref="x", yref="y", text="Planet starts to occult the star", showarrow=True, arrowhead=7, ax=0, ay=-40 ), dict( x=time[first_full], y=flux[first_full], xref="x", yref="y", text="Planet is fully in front of the star", showarrow=True, arrowhead=7, ax=0, ay=-40 ), dict( x=time[sec_dip], y=flux[sec_dip], xref="x", yref="y", text="Planet has reached the edge of the star", showarrow=True, arrowhead=7, ax=0, ay=40, ), dict( x=time[sec_full], y=flux[sec_full], xref="x", yref="y", text="Planet has stopped occulting the star", showarrow=True, arrowhead=7, ax=0, ay=-40 ) ] ) return fig
import helper import plotly.graph_objs as go import plotly.offline as offline_py offline_py.init_notebook_mode(connected=True) def _generate_stock_trace(prices): return go.Scatter( name='Index', x=prices.index, y=prices, line={'color': helper.color_scheme['major_line']}) def _generate_traces(name_df_color_data): traces = [] for name, df, color in name_df_color_data: traces.append(go.Scatter( name=name, x=df.index, y=df, mode='line', line={'color': color})) return traces def plot_stock(prices, title): config = helper.generate_config() layout = go.Layout(title=title)
def plot_3d(df_real, model, name_x, name_y, name_z, name_pred, only_margin=False): from plotly.offline import init_notebook_mode, iplot import plotly.graph_objs as go import numpy as np import pandas as pd init_notebook_mode(connected=True) # 1 generate predictions cloud--------------------------------------- d = model._parameters col_x = [] col_y = [] col_z = [] col_pred = [] last_a = None last_i3 = -1 for i1 in range(100, 1000, 20): # mass for i2 in np.arange(4, 10, 0.1): # widths for i3 in range(0, 200, 2): # color_scores col_x.append(i1) col_y.append(i2) col_z.append(i3) A = int(np.squeeze(model.predict(np.array([[i1], [i2], [i3]])))) if only_margin: if A == last_a or i3 < last_i3: col_pred.append(None) else: if len(col_pred) > 0: col_pred[-1] = last_a col_pred.append(A) else: col_pred.append(A) last_a = A last_i3 = i3 df_model = pd.DataFrame({ name_x: col_x, name_y: col_y, name_z: col_z, name_pred: col_pred }) # 2 delete some values from cloud------------------------------------ if not only_margin: df_model = df_model.sample(frac=1, random_state=0) df_model.reset_index(inplace=True, drop=True) df_model = df_model.iloc[-500:] # 3 plot------------------------------------------------------------- df0 = df_model[df_model['fruit_label'] == 0] df1 = df_model[df_model['fruit_label'] == 1] df2 = df_real[df_real['fruit_label'] == 0] df3 = df_real[df_real['fruit_label'] == 1] trace0 = go.Scatter3d(x=df0[name_x], y=df0[name_y], z=df0[name_z], mode='markers', marker=dict(color='rgb(100, 0, 0)', size=6, line=dict( color='rgba(217, 217, 217, 0.14)', width=0.5), opacity=1)) trace1 = go.Scatter3d(x=df1[name_x], y=df1[name_y], z=df1[name_z], mode='markers', marker=dict(color='rgb(0, 100, 0)', size=6, symbol='circle', line=dict(color='rgb(204, 204, 204)', width=1), opacity=1)) trace2 = go.Scatter3d(x=df2[name_x], y=df2[name_y], z=df2[name_z], mode='markers', marker=dict(color='rgb(255, 0, 0)', size=12, line=dict( color='rgba(217, 217, 217, 0.14)', width=0.5), opacity=1)) trace3 = go.Scatter3d(x=df3[name_x], y=df3[name_y], z=df3[name_z], mode='markers', marker=dict(color='rgb(0, 255, 0)', size=12, symbol='circle', line=dict(color='rgb(204, 204, 204)', width=1), opacity=1)) data = [trace0, trace1, trace2, trace3] layout = go.Layout( scene=dict( xaxis=dict(title=name_x), yaxis=dict(title=name_y), zaxis=dict(title=name_z), ), width=700, margin=dict(r=20, b=10, l=10, t=10), ) fig = dict(data=data, layout=layout) iplot(fig, filename='elevations-3d-surface')
# coding: utf-8 # This document is a basic demonstration of the type of data analysis that I could do for your company. It's not a full fledged and detailed analysis, but is more of a showing of my capabilities of using programming to do data analysis and create data visualizations. # In[1]: import numpy as np import pandas as pd import plotly.plotly as py import plotly.offline as ploff from ggplot import * from subprocess import check_output #from plotly.offline import init_notebook_mode, plot ploff.init_notebook_mode() # We will be looking at **ACA US Health Insurance Marketplace** data on health and dental plans offered to individuals and small businesses over the years 2014-2016. # # This data was initially released by the **Centers for Medicare & Medicaid Services (CMS)**, and downloaded from https://www.kaggle.com/hhsgov/health-insurance-marketplace where the zipped file was 734.9MB and took 4-5 minutes to download over a 150MBs internet connection. The uncompressed file came out to be 11.53GB. This is pretty huge for my Macbook Air to handle, so we will only take a look at the Rates.csv (1.97GB) data set where each record relates to one issuer’s rates based on plan, geographic rating area, and subscriber eligibility requirements over plan years 2014, 2015, and 2016. # # A data dictionary can be found for this data set at https://www.cms.gov/CCIIO/Resources/Data-Resources/Downloads/Rate_DataDictionary_2016.pdf # # There are several variables in the Rates data set, and we'll only be reading in a subset of them for this quick exploration of the data. In particular, we will explore the variables # # * **BusinessYear** - year of the insurance plan, i.e., 2014, 2015, or 2016 # * **StateCode** - state where the data record is from # * **Age** - Age of the individuals plan, or FamilyOption if the plan covers an entire family, or fo the high end we simply have the age '65 or older' # * **IndividualRate** - monthly premium rate for an individual # * **Couple** - monthly premium for a couple
def init_notebook_mode(): plotly_offline.init_notebook_mode() global INTERACTIVE_MODE INTERACTIVE_MODE = True
def go_offline(): py_offline.init_notebook_mode() py_offline.__PLOTLY_OFFLINE_INITIALIZED=True
# This Python 3 environment comes with many helpful analytics libraries installed # It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python # For example, here's several helpful packages to load in import numpy as np # linear algebra import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) import re import sklearn as sk import xgboost as xgb import seaborn as sns import matplotlib.pyplot as plt get_ipython().magic(u'matplotlib inline') import plotly.offline as py py.init_notebook_mode(connected=True) import plotly.graph_objs as go import plotly.tools as tls import warnings warnings.filterwarnings('ignore') # Going to use these 5 base models for the stacking from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier, ExtraTreesClassifier from sklearn.svm import SVC from sklearn.neighbors import KNeighborsClassifier from sklearn.cross_validation import KFold from sklearn.cross_validation import train_test_split # In[ ]:
#!/usr/bin/python3 # coding: utf-8 # 在执行脚本时,它将打开一个Web浏览器,并绘制绘图。 from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot from plotly.graph_objs import Scatter, Figure, Layout plot([Scatter(x=[1, 2, 3], y=[3, 1, 6])]) # 在Jupyter Notebook环境中离线绘制图形。首先,您需要启动Plotly Notebook模式 init_notebook_mode(connected=True) iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}]) from plotly.graph_objs import * import numpy as np x = np.random.randn(2000) y = np.random.randn(2000) iplot([Histogram2dContour(x=x, y=y, contours=Contours(coloring='heatmap')), Scatter(x=x, y=y, mode='markers', marker=Marker(color='white', size=3, opacity=0.3))], show_link=False) from plotly.graph_objs import * import pandas as pd df = pd.read_csv('https://plot.ly/~etpinard/191.csv') iplot({ 'data': [ Scatter(x=df['"{}{}"'.format(continent,', x')],
def plotlyInteractive(): pyo.init_notebook_mode(connected=True)
def set_notebook_mode(): pyo.init_notebook_mode(connected=True)
def miplot(*args, **kwargs): configure_plotly_browser_state() init_notebook_mode(connected=False) iplot(*args, **kwargs)
def go_offline(offline=True): if offline: py_offline.init_notebook_mode() py_offline.__PLOTLY_OFFLINE_INITIALIZED=True else: py_offline.__PLOTLY_OFFLINE_INITIALIZED=False
# + [markdown] run_control={"frozen": false, "read_only": false} # <img src='__docs/__all/notebook_rules.png' /> # + [markdown] run_control={"frozen": false, "read_only": false} # # Select Your IPTS # + run_control={"frozen": false, "read_only": false} from __code import system from __code.bragg_edge import BraggEdge, Interface system.System.select_working_dir(facility='SNS', instrument='SNAP') from __code.__all import custom_style custom_style.style() from plotly.offline import plot, init_notebook_mode, iplot init_notebook_mode() # - # ## Prepare UI engine # + run_control={"frozen": false, "read_only": false} # %gui qt # + [markdown] run_control={"frozen": false, "read_only": false} # # Select Raw Data Input Folder # # Data and time spectra files will be loaded # + run_control={"frozen": false, "read_only": false} o_bragg = BraggEdge(working_dir=system.System.get_working_dir()) o_bragg.select_working_folder()
# Load PySpark sc = pyspark.SparkContext() sqlContext = SQLContext(sc) # Read the descriptor of a Dataiku dataset intCS_ds = dataiku.Dataset("MyInternetClickStreamDataSet") # And read it as a Spark dataframe intCS_df = dkuspark.get_dataframe(sqlContext, intCS_ds) # Discern a sample dataset from the source Spark dataset intCS_df intCS_sdf = intCS_df.sample(False, 0.5, 10000) # Initiate offline notebook mode py.init_notebook_mode() # Insantiate toPandas() and assign sampled Spark df to it intCS_sdf_pandas = intCS_sdf.toPandas() # Plot the Linechart x = intCS_sdf_pandas['parsed_date_month'] #X axis dataset attribute y0 = intCS_sdf_pandas['search_qty_sum'] #Y axis dataset attribute y1 = intCS_sdf_pandas['sales_qty_sum'] # Y axis dataset attribute Search_Quantity = go.Scatter( x=x, y=y0, mode='line' ) Sales_Quantity = go.Scatter(
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns get_ipython().run_line_magic('matplotlib', 'inline') import ipywidgets as widgets from ipywidgets import interact # Plotly plotting support import plotly.offline as py py.init_notebook_mode() import plotly.graph_objs as go import plotly.figure_factory as ff import cufflinks as cf cf.set_config_file(offline=True, world_readable=True, theme='ggplot') # this file attempts to figure out the box walmart would have used and the dimenions of the box # import all the files i need wms_cwxl = pd.read_csv('wms_cwxl_with_itemsize.csv') del wms_cwxl['Unnamed: 0.1'] # figure out the actual box used by walmart box_type = {