# In this script, we will analyze Heatmaps using plotly functions import plotly.offline as pyo import plotly.graph_objs as go import pandas as pd #dataset read df = pd.read_csv('2010SantaBarbaraCA.csv') # Heatmap data data = [go.Heatmap(x=df['DAY'], #x - xaxis, y- yaxis and z- color axis y=df['LST_TIME'], z=df['T_HR_AVG'].values.tolist())] # for color axis, we need a lsit so we convert with .values.tolist() from column to list layout = go.Layout(title='Heatmap for Temperature in Santa Barbara 2010') fig=go.Figure(data=data,layout=layout) pyo.plot(fig,filename='Basic-Heatmap-Temps.html') #-------------------------------------------------------------------------------------------------------------------------------------------------# #Different color scales data1 = [go.Heatmap(x=df['DAY'], #x - xaxis, y- yaxis and z- color axis y=df['LST_TIME'], z=df['T_HR_AVG'].values.tolist(), colorscale='Jet')] layout1 = go.Layout(title='Heatmap for Temperature in Santa Barbara 2010 - Jet colormap')
def plot_heatmap( df_corr, colorscale="brwnyl", cut_in_half=False, showscale=True, textfont="default", xgap=1, ygap=1, transparent=True, ): if textfont == "default": textfont = { "color": "#000000", "size": 14, "family": "Times New Roman" } trace = go.Heatmap( colorscale=colorscale, hoverinfo="text", xgap=xgap, ygap=ygap, showscale=showscale, ) if cut_in_half: corr_triangle = np.array([[None for k in range(df_corr.shape[1] - 1)] for j in range(df_corr.shape[0] - 1)]) for k, vals in enumerate(df_corr[1:].values): corr_triangle[k][:k + 1] = np.round(vals[:k + 1], 2) trace.update( z=corr_triangle[::-1], x=df_corr.index[:-1], y=df_corr.index[1:][::-1], text=corr_triangle[::-1], ) fig = go.Figure(data=[trace]) annotations = [] for k, y in enumerate(df_corr.index): for x in df_corr.index[:k]: anot = go.layout.Annotation( x=x, y=y, xref="x", yref="y", text=f"<b>{df_corr[x][y]: .2f}</b>", showarrow=False, font=textfont, ) annotations.append(anot) else: trace.update( z=df_corr.values[::-1], x=df_corr.index, y=df_corr.index[::-1], text=np.round(df_corr.values[::-1], 2), ) fig = go.Figure(data=[trace]) annotations = [] for k, y in enumerate(df_corr.index): for x in df_corr.index: anot = go.layout.Annotation( x=x, y=y, xref="x", yref="y", text=f"<b>{df_corr[x][y]: .2f}</b>", showarrow=False, font=textfont, ) annotations.append(anot) fig.update_layout(showlegend=False, annotations=annotations) if transparent: fig.update_layout(plot_bgcolor="rgba(0,0,0,0)", paper_bgcolor="rgba(0,0,0,0)") return fig
import plotly.offline as pyo import plotly.graph_objs as go import pandas as pd # Load CSV file from Datasets folder df = pd.read_csv('../Datasets/Weather2014-15.csv') # Preparing data data = [ go.Heatmap(x=df['day'], y=df['month'], z=df['actual_max_temp'].values.tolist(), colorscale='Jet') ] # Preparing layout layout = go.Layout(title='Max Temperature on Day of Week and Month of Year', xaxis_title="Day of Week", yaxis_title="Month") # Plot the figure and saving in a html file fig = go.Figure(data=data, layout=layout) pyo.plot(fig, filename='heatmap.html')
Barcelona, 29/11/2012 """ import argparse, os, sys from datetime import datetime import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np import plotly.plotly as py import plotly.graph_objs as go data = [ go.Heatmap(z=[[1, 20, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]], x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], y=['Morning', 'Afternoon', 'Evening']) ] plot_url = py.plot(data, filename='labelled-heatmap') def histplot(ax, x, handle, bins, title, xlab, ylab, xlog, ylog, \ normed=False, alpha=0.75): """ """ #fig = plt.figure() #ax = fig.add_subplot(111) if xlog: ax.set_xscale('log') if ylog: ax.set_yscale('log', nonposy='clip') # the histogram of the data
def imshow( img, zmin=None, zmax=None, origin=None, labels={}, x=None, y=None, animation_frame=None, facet_col=None, facet_col_wrap=None, facet_col_spacing=None, facet_row_spacing=None, color_continuous_scale=None, color_continuous_midpoint=None, range_color=None, title=None, template=None, width=None, height=None, aspect=None, contrast_rescaling=None, binary_string=None, binary_backend="auto", binary_compression_level=4, binary_format="png", text_auto=False, ): """ Display an image, i.e. data on a 2D regular raster. Parameters ---------- img: array-like image, or xarray The image data. Supported array shapes are - (M, N): an image with scalar data. The data is visualized using a colormap. - (M, N, 3): an image with RGB values. - (M, N, 4): an image with RGBA values, i.e. including transparency. zmin, zmax : scalar or iterable, optional zmin and zmax define the scalar range that the colormap covers. By default, zmin and zmax correspond to the min and max values of the datatype for integer datatypes (ie [0-255] for uint8 images, [0, 65535] for uint16 images, etc.). For a multichannel image of floats, the max of the image is computed and zmax is the smallest power of 256 (1, 255, 65535) greater than this max value, with a 5% tolerance. For a single-channel image, the max of the image is used. Overridden by range_color. origin : str, 'upper' or 'lower' (default 'upper') position of the [0, 0] pixel of the image array, in the upper left or lower left corner. The convention 'upper' is typically used for matrices and images. labels : dict with str keys and str values (default `{}`) Sets names used in the figure for axis titles (keys ``x`` and ``y``), colorbar title and hoverlabel (key ``color``). The values should correspond to the desired label to be displayed. If ``img`` is an xarray, dimension names are used for axis titles, and long name for the colorbar title (unless overridden in ``labels``). Possible keys are: x, y, and color. x, y: list-like, optional x and y are used to label the axes of single-channel heatmap visualizations and their lengths must match the lengths of the second and first dimensions of the img argument. They are auto-populated if the input is an xarray. animation_frame: int or str, optional (default None) axis number along which the image array is sliced to create an animation plot. If `img` is an xarray, `animation_frame` can be the name of one the dimensions. facet_col: int or str, optional (default None) axis number along which the image array is sliced to create a facetted plot. If `img` is an xarray, `facet_col` can be the name of one the dimensions. facet_col_wrap: int Maximum number of facet columns. Wraps the column variable at this width, so that the column facets span multiple rows. Ignored if `facet_col` is None. facet_col_spacing: float between 0 and 1 Spacing between facet columns, in paper units. Default is 0.02. facet_row_spacing: float between 0 and 1 Spacing between facet rows created when ``facet_col_wrap`` is used, in paper units. Default is 0.0.7. color_continuous_scale : str or list of str colormap used to map scalar data to colors (for a 2D image). This parameter is not used for RGB or RGBA images. If a string is provided, it should be the name of a known color scale, and if a list is provided, it should be a list of CSS- compatible colors. color_continuous_midpoint : number If set, computes the bounds of the continuous color scale to have the desired midpoint. Overridden by range_color or zmin and zmax. range_color : list of two numbers If provided, overrides auto-scaling on the continuous color scale, including overriding `color_continuous_midpoint`. Also overrides zmin and zmax. Used only for single-channel images. title : str The figure title. template : str or dict or plotly.graph_objects.layout.Template instance The figure template name or definition. width : number The figure width in pixels. height: number The figure height in pixels. aspect: 'equal', 'auto', or None - 'equal': Ensures an aspect ratio of 1 or pixels (square pixels) - 'auto': The axes is kept fixed and the aspect ratio of pixels is adjusted so that the data fit in the axes. In general, this will result in non-square pixels. - if None, 'equal' is used for numpy arrays and 'auto' for xarrays (which have typically heterogeneous coordinates) contrast_rescaling: 'minmax', 'infer', or None how to determine data values corresponding to the bounds of the color range, when zmin or zmax are not passed. If `minmax`, the min and max values of the image are used. If `infer`, a heuristic based on the image data type is used. binary_string: bool, default None if True, the image data are first rescaled and encoded as uint8 and then passed to plotly.js as a b64 PNG string. If False, data are passed unchanged as a numerical array. Setting to True may lead to performance gains, at the cost of a loss of precision depending on the original data type. If None, use_binary_string is set to True for multichannel (eg) RGB arrays, and to False for single-channel (2D) arrays. 2D arrays are represented as grayscale and with no colorbar if use_binary_string is True. binary_backend: str, 'auto' (default), 'pil' or 'pypng' Third-party package for the transformation of numpy arrays to png b64 strings. If 'auto', Pillow is used if installed, otherwise pypng. binary_compression_level: int, between 0 and 9 (default 4) png compression level to be passed to the backend when transforming an array to a png b64 string. Increasing `binary_compression` decreases the size of the png string, but the compression step takes more time. For most images it is not worth using levels greater than 5, but it's possible to test `len(fig.data[0].source)` and to time the execution of `imshow` to tune the level of compression. 0 means no compression (not recommended). binary_format: str, 'png' (default) or 'jpg' compression format used to generate b64 string. 'png' is recommended since it uses lossless compression, but 'jpg' (lossy) compression can result if smaller binary strings for natural images. text_auto: bool or str (default `False`) If `True` or a string, single-channel `img` values will be displayed as text. A string like `'.2f'` will be interpreted as a `texttemplate` numeric formatting directive. Returns ------- fig : graph_objects.Figure containing the displayed image See also -------- plotly.graph_objects.Image : image trace plotly.graph_objects.Heatmap : heatmap trace Notes ----- In order to update and customize the returned figure, use `go.Figure.update_traces` or `go.Figure.update_layout`. If an xarray is passed, dimensions names and coordinates are used for axes labels and ticks. """ args = locals() apply_default_cascade(args) labels = labels.copy() nslices_facet = 1 if facet_col is not None: if isinstance(facet_col, str): facet_col = img.dims.index(facet_col) nslices_facet = img.shape[facet_col] facet_slices = range(nslices_facet) ncols = int(facet_col_wrap) if facet_col_wrap is not None else nslices_facet nrows = ( nslices_facet // ncols + 1 if nslices_facet % ncols else nslices_facet // ncols ) else: nrows = 1 ncols = 1 if animation_frame is not None: if isinstance(animation_frame, str): animation_frame = img.dims.index(animation_frame) nslices_animation = img.shape[animation_frame] animation_slices = range(nslices_animation) slice_dimensions = (facet_col is not None) + ( animation_frame is not None ) # 0, 1, or 2 facet_label = None animation_label = None img_is_xarray = False # ----- Define x and y, set labels if img is an xarray ------------------- if xarray_imported and isinstance(img, xarray.DataArray): dims = list(img.dims) img_is_xarray = True if facet_col is not None: facet_slices = img.coords[img.dims[facet_col]].values _ = dims.pop(facet_col) facet_label = img.dims[facet_col] if animation_frame is not None: animation_slices = img.coords[img.dims[animation_frame]].values _ = dims.pop(animation_frame) animation_label = img.dims[animation_frame] y_label, x_label = dims[0], dims[1] # np.datetime64 is not handled correctly by go.Heatmap for ax in [x_label, y_label]: if np.issubdtype(img.coords[ax].dtype, np.datetime64): img.coords[ax] = img.coords[ax].astype(str) if x is None: x = img.coords[x_label].values if y is None: y = img.coords[y_label].values if aspect is None: aspect = "auto" if labels.get("x", None) is None: labels["x"] = x_label if labels.get("y", None) is None: labels["y"] = y_label if labels.get("animation_frame", None) is None: labels["animation_frame"] = animation_label if labels.get("facet_col", None) is None: labels["facet_col"] = facet_label if labels.get("color", None) is None: labels["color"] = xarray.plot.utils.label_from_attrs(img) labels["color"] = labels["color"].replace("\n", "<br>") else: if hasattr(img, "columns") and hasattr(img.columns, "__len__"): if x is None: x = img.columns if labels.get("x", None) is None and hasattr(img.columns, "name"): labels["x"] = img.columns.name or "" if hasattr(img, "index") and hasattr(img.index, "__len__"): if y is None: y = img.index if labels.get("y", None) is None and hasattr(img.index, "name"): labels["y"] = img.index.name or "" if labels.get("x", None) is None: labels["x"] = "" if labels.get("y", None) is None: labels["y"] = "" if labels.get("color", None) is None: labels["color"] = "" if aspect is None: aspect = "equal" # --- Set the value of binary_string (forbidden for pandas) if isinstance(img, pd.DataFrame): if binary_string: raise ValueError("Binary strings cannot be used with pandas arrays") is_dataframe = True else: is_dataframe = False # --------------- Starting from here img is always a numpy array -------- img = np.asanyarray(img) # Reshape array so that animation dimension comes first, then facets, then images if facet_col is not None: img = np.moveaxis(img, facet_col, 0) if animation_frame is not None and animation_frame < facet_col: animation_frame += 1 facet_col = True if animation_frame is not None: img = np.moveaxis(img, animation_frame, 0) animation_frame = True args["animation_frame"] = ( "animation_frame" if labels.get("animation_frame") is None else labels["animation_frame"] ) iterables = () if animation_frame is not None: iterables += (range(nslices_animation),) if facet_col is not None: iterables += (range(nslices_facet),) # Default behaviour of binary_string: True for RGB images, False for 2D if binary_string is None: binary_string = img.ndim >= (3 + slice_dimensions) and not is_dataframe # Cast bools to uint8 (also one byte) if img.dtype == np.bool: img = 255 * img.astype(np.uint8) if range_color is not None: zmin = range_color[0] zmax = range_color[1] # -------- Contrast rescaling: either minmax or infer ------------------ if contrast_rescaling is None: contrast_rescaling = "minmax" if img.ndim == (2 + slice_dimensions) else "infer" # We try to set zmin and zmax only if necessary, because traces have good defaults if contrast_rescaling == "minmax": # When using binary_string and minmax we need to set zmin and zmax to rescale the image if (zmin is not None or binary_string) and zmax is None: zmax = img.max() if (zmax is not None or binary_string) and zmin is None: zmin = img.min() else: # For uint8 data and infer we let zmin and zmax to be None if passed as None if zmax is None and img.dtype != np.uint8: zmax = _infer_zmax_from_type(img) if zmin is None and zmax is not None: zmin = 0 # For 2d data, use Heatmap trace, unless binary_string is True if img.ndim == 2 + slice_dimensions and not binary_string: y_index = slice_dimensions if y is not None and img.shape[y_index] != len(y): raise ValueError( "The length of the y vector must match the length of the first " + "dimension of the img matrix." ) x_index = slice_dimensions + 1 if x is not None and img.shape[x_index] != len(x): raise ValueError( "The length of the x vector must match the length of the second " + "dimension of the img matrix." ) texttemplate = None if text_auto is True: texttemplate = "%{z}" elif text_auto is not False: texttemplate = "%{z:" + text_auto + "}" traces = [ go.Heatmap( x=x, y=y, z=img[index_tup], coloraxis="coloraxis1", name=str(i), texttemplate=texttemplate, ) for i, index_tup in enumerate(itertools.product(*iterables)) ] autorange = True if origin == "lower" else "reversed" layout = dict(yaxis=dict(autorange=autorange)) if aspect == "equal": layout["xaxis"] = dict(scaleanchor="y", constrain="domain") layout["yaxis"]["constrain"] = "domain" colorscale_validator = ColorscaleValidator("colorscale", "imshow") layout["coloraxis1"] = dict( colorscale=colorscale_validator.validate_coerce( args["color_continuous_scale"] ), cmid=color_continuous_midpoint, cmin=zmin, cmax=zmax, ) if labels["color"]: layout["coloraxis1"]["colorbar"] = dict(title_text=labels["color"]) # For 2D+RGB data, use Image trace elif ( img.ndim >= 3 and (img.shape[-1] in [3, 4] or slice_dimensions and binary_string) ) or (img.ndim == 2 and binary_string): rescale_image = True # to check whether image has been modified if zmin is not None and zmax is not None: zmin, zmax = ( _vectorize_zvalue(zmin, mode="min"), _vectorize_zvalue(zmax, mode="max"), ) x0, y0, dx, dy = (None,) * 4 error_msg_xarray = ( "Non-numerical coordinates were passed with xarray `img`, but " "the Image trace cannot handle it. Please use `binary_string=False` " "for 2D data or pass instead the numpy array `img.values` to `px.imshow`." ) if x is not None: x = np.asanyarray(x) if np.issubdtype(x.dtype, np.number): x0 = x[0] dx = x[1] - x[0] else: error_msg = ( error_msg_xarray if img_is_xarray else ( "Only numerical values are accepted for the `x` parameter " "when an Image trace is used." ) ) raise ValueError(error_msg) if y is not None: y = np.asanyarray(y) if np.issubdtype(y.dtype, np.number): y0 = y[0] dy = y[1] - y[0] else: error_msg = ( error_msg_xarray if img_is_xarray else ( "Only numerical values are accepted for the `y` parameter " "when an Image trace is used." ) ) raise ValueError(error_msg) if binary_string: if zmin is None and zmax is None: # no rescaling, faster img_rescaled = img rescale_image = False elif img.ndim == 2 + slice_dimensions: # single-channel image img_rescaled = rescale_intensity( img, in_range=(zmin[0], zmax[0]), out_range=np.uint8 ) else: img_rescaled = np.stack( [ rescale_intensity( img[..., ch], in_range=(zmin[ch], zmax[ch]), out_range=np.uint8, ) for ch in range(img.shape[-1]) ], axis=-1, ) img_str = [ image_array_to_data_uri( img_rescaled[index_tup], backend=binary_backend, compression=binary_compression_level, ext=binary_format, ) for index_tup in itertools.product(*iterables) ] traces = [ go.Image(source=img_str_slice, name=str(i), x0=x0, y0=y0, dx=dx, dy=dy) for i, img_str_slice in enumerate(img_str) ] else: colormodel = "rgb" if img.shape[-1] == 3 else "rgba256" traces = [ go.Image( z=img[index_tup], zmin=zmin, zmax=zmax, colormodel=colormodel, x0=x0, y0=y0, dx=dx, dy=dy, ) for index_tup in itertools.product(*iterables) ] layout = {} if origin == "lower" or (dy is not None and dy < 0): layout["yaxis"] = dict(autorange=True) if dx is not None and dx < 0: layout["xaxis"] = dict(autorange="reversed") else: raise ValueError( "px.imshow only accepts 2D single-channel, RGB or RGBA images. " "An image of shape %s was provided. " "Alternatively, 3- or 4-D single or multichannel datasets can be " "visualized using the `facet_col` or/and `animation_frame` arguments." % str(img.shape) ) # Now build figure col_labels = [] if facet_col is not None: slice_label = ( "facet_col" if labels.get("facet_col") is None else labels["facet_col"] ) col_labels = ["%s=%d" % (slice_label, i) for i in facet_slices] fig = init_figure(args, "xy", [], nrows, ncols, col_labels, []) for attr_name in ["height", "width"]: if args[attr_name]: layout[attr_name] = args[attr_name] if args["title"]: layout["title_text"] = args["title"] elif args["template"].layout.margin.t is None: layout["margin"] = {"t": 60} frame_list = [] for index, trace in enumerate(traces): if (facet_col and index < nrows * ncols) or index == 0: fig.add_trace(trace, row=nrows - index // ncols, col=index % ncols + 1) if animation_frame is not None: for i, index in zip(range(nslices_animation), animation_slices): frame_list.append( dict( data=traces[nslices_facet * i : nslices_facet * (i + 1)], layout=layout, name=str(index), ) ) if animation_frame: fig.frames = frame_list fig.update_layout(layout) # Hover name, z or color if binary_string and rescale_image and not np.all(img == img_rescaled): # we rescaled the image, hence z is not displayed in hover since it does # not correspond to img values hovertemplate = "%s: %%{x}<br>%s: %%{y}<extra></extra>" % ( labels["x"] or "x", labels["y"] or "y", ) else: if trace["type"] == "heatmap": hover_name = "%{z}" elif img.ndim == 2: hover_name = "%{z[0]}" elif img.ndim == 3 and img.shape[-1] == 3: hover_name = "[%{z[0]}, %{z[1]}, %{z[2]}]" else: hover_name = "%{z}" hovertemplate = "%s: %%{x}<br>%s: %%{y}<br>%s: %s<extra></extra>" % ( labels["x"] or "x", labels["y"] or "y", labels["color"] or "color", hover_name, ) fig.update_traces(hovertemplate=hovertemplate) if labels["x"]: fig.update_xaxes(title_text=labels["x"], row=1) if labels["y"]: fig.update_yaxes(title_text=labels["y"], col=1) configure_animation_controls(args, go.Image, fig) fig.update_layout(template=args["template"], overwrite=True) return fig
# Now we'll be building a new classifier that takes in our initial predictions as a pre-train model base_predictions_train = pd.DataFrame({ 'RandomForest': rf_oof_train.ravel(), 'ExtraTrees': et_oof_train.ravel(), 'AdaBoost': ada_oof_train.ravel(), 'GradientBoost': gb_oof_train.ravel() }) base_predictions_train.head() # In[ ]: # correlation heatmap of second-level training data = [ go.Heatmap(x=base_predictions_train.columns.values, y=base_predictions_train.columns.values, colorscale='Viridis', showscale=True, reversescale=True, z=base_predictions_train.astype(float).corr().values) ] py.iplot(data, filename='labeled-headmap') # In[ ]: x_train = np.concatenate( (et_oof_train, rf_oof_train, ada_oof_train, gb_oof_train, svc_oof_train), axis=1) x_test = np.concatenate( (et_oof_test, rf_oof_test, ada_oof_test, gb_oof_test, svc_oof_test), axis=1) # In[ ]:
def plot_decision_boundary_and_metrics( model, x_train, y_train, x_test, y_test, metrics ): d = x_train.shape[1] x_min, x_max = x_train[:, 0].min() - 1, x_train[:, 0].max() + 1 y_min, y_max = x_train[:, 1].min() - 1, x_train[:, 1].max() + 1 h = 0.02 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) y_ = np.arange(y_min, y_max, h) model_input = [(xx.ravel() ** p, yy.ravel() ** p) for p in range(1, d // 2 + 1)] aux = [] for c in model_input: aux.append(c[0]) aux.append(c[1]) Z = model.predict(np.concatenate([v.reshape(-1, 1) for v in aux], axis=1)) Z = Z.reshape(xx.shape) fig = make_subplots( rows=2, cols=2, specs=[[{"colspan": 2}, None], [{"type": "indicator"}, {"type": "indicator"}]], subplot_titles=("Decision Boundary", None, None), row_heights=[0.7, 0.30], ) heatmap = go.Heatmap( x=xx[0], y=y_, z=Z, colorscale=["tomato", "rgb(27,158,119)"], showscale=False, ) train_data = go.Scatter( x=x_train[:, 0], y=x_train[:, 1], name="train data", mode="markers", showlegend=True, marker=dict( size=10, color=y_train, colorscale=["tomato", "green"], line=dict(color="black", width=2), ), ) test_data = go.Scatter( x=x_test[:, 0], y=x_test[:, 1], name="test data", mode="markers", showlegend=True, marker_symbol="cross", visible="legendonly", marker=dict( size=10, color=y_test, colorscale=["tomato", "green"], line=dict(color="black", width=2), ), ) fig.add_trace(heatmap, row=1, col=1,).add_trace(train_data).add_trace( test_data ).update_xaxes(range=[x_min, x_max], title="x1").update_yaxes( range=[y_min, y_max], title="x2" ) fig.add_trace( go.Indicator( mode="gauge+number+delta", value=metrics["test_accuracy"], title={"text": f"Accuracy (test)"}, domain={"x": [0, 1], "y": [0, 1]}, gauge={"axis": {"range": [0, 1]}}, delta={"reference": metrics["train_accuracy"]}, ), row=2, col=1, ) fig.add_trace( go.Indicator( mode="gauge+number+delta", value=metrics["test_f1"], title={"text": f"F1 score (test)"}, domain={"x": [0, 1], "y": [0, 1]}, gauge={"axis": {"range": [0, 1]}}, delta={"reference": metrics["train_f1"]}, ), row=2, col=2, ) fig.update_layout( height=700, ) return fig
y='value', color='query', hover_name="query").for_each_trace( lambda t: t.update(name=t.name.split("=")[1])) fig.update_traces(mode='markers+lines') fig.update_layout(legend_traceorder="reversed") st.plotly_chart(fig) if selection == 'Карта корреляций': cov_corr_t = pd.read_excel('data/corr_true_data.xlsx') cov_pivot = pd.pivot_table(cov_corr_t, values='corr', index=['A'], columns=['B']) fig = go.Figure(data=go.Heatmap(z=cov_pivot, x=cov_pivot.columns, y=cov_pivot.columns, hoverongaps=False, colorscale='deep')) fig.show() if selection == 'Поиск взаимосвязей': st.header('Поиск взаимосвязей. Методика расчета') st.markdown( """Для поиска связей в данных были подготовленны датасеты помесячных данных релевантной поисковой статистики и статистики COVID-19<br> Далее, был проведен поиск корреляций Пирсона между всеми наборами данных данного датасета<br> И к полученным результатам применена поправка Бенджамини-Хохберга на множественную проверку гипотез, для повышения надежности результатов. <br> Подтвержденные значения корреляций показаны ниже. """, unsafe_allow_html=True) df_cov_true = pd.read_excel('data/corr_true_data.xlsx') st.write(df_cov_true) st.markdown("""corr - значение корреляции пирсона между A и B<br>
def update_figure(selected_config, selected_has, selected_im, selected_nant, selected_zoom, selected_declination, selected_latitude, selected_weighting): ctx = dash.callback_context # print 'Trig : ', ctx.triggered # print 'States : ', ctx.states # print 'Inputs : ', ctx.inputs #Trig : [{'prop_id': u'config-dropdown.value', 'value': u'RandomConfig'}] thischanged = None if ctx.triggered[0]['prop_id'].count('config-dropdown'): thischanged = 'config' if ctx.triggered[0]['prop_id'].count('source-dropdown'): thischanged = 'source' if ctx.triggered[0]['prop_id'].count('timerange-slider'): thischanged = 'timerange' if ctx.triggered[0]['prop_id'].count('zoom-slider'): thischanged = 'antzoom' if ctx.triggered[0]['prop_id'].count('nant-dropdown'): thischanged = 'nant' if ctx.triggered[0]['prop_id'].count('declination-picker'): thischanged = 'declination' if ctx.triggered[0]['prop_id'].count('latitude-picker'): thischanged = 'latitude' if ctx.triggered[0]['prop_id'].count('weighting-picker'): thischanged = 'weighting' #print 'This changed : ', thischanged tim1 = time.time() if thischanged == 'config' or thischanged == 'antzoom' or thischanged == 'nant': changeseed = True if thischanged == 'antzoom' or thischanged == 'nant': changeseed = False if eval(selected_nant) == 2: selected_config = 'RandomConfig' print("Using random locations for 2 antennas") tel.calcAntList(configtype=selected_config, zoom=2**selected_zoom, changeseed=changeseed, nant=eval(selected_nant)) # if thischanged=='antzoom': # tel.scaleAntList(zoom=selected_zoom) antlist = tel.getAntList() antlocsX = antlist['EastLoc'] antlocsY = antlist['NorthLoc'] tim2 = time.time() # print antlocsX, antlocsY ### Prepare contents of antenna layout plot traces1 = [] traces1.append( go.Scatter( x=antlocsX, y=antlocsY, text=selected_config, mode='markers', opacity=0.7, marker={ 'size': 15, 'line': { 'width': 0.5, 'color': 'white' } }, )) tim3 = time.time() ### Prepare contents of observed image. ### Slider callback too if thischanged == 'config' or thischanged == 'timerange' or thischanged == 'antzoom' or thischanged == 'nant' or thischanged == 'declination' or thischanged == 'latitude' or thischanged == 'weighting': tel.makeUVcov(has=selected_has, dec=selected_declination, obslatitude=selected_latitude, weighting=selected_weighting) tim4 = time.time() traces3 = [] traces3.append(go.Heatmap(z=tel.getUVcov())) ## sky callback if thischanged == 'source': tel.setsky(imtype=selected_im) tim5 = time.time() ## Sim tel.makeImage() tim6 = time.time() traces2 = [] traces2.append(go.Heatmap(z=tel.getImage())) tim7 = time.time() # print 'Ant read time : ', tim2-tim1 # print 'Ant plot time : ', tim3-tim2 # print 'UVcov time : ', tim4-tim3 # print 'Source time : ', tim5-tim4 # print 'Observe time :', tim6-tim5 # print 'Make raster : ', tim7-tim6 # print 'Total time in callback : ', tim7-tim1, ' sec' ### Return list of outputs, sync'd with specification above dispsize = 400 return [{ 'data': traces1, 'layout': go.Layout(xaxis={ 'title': 'X Position (m)', 'range': [-1000, 1000] }, yaxis={ 'title': 'Y Position (m)', 'range': [-1000, 1000] }, title="ARRAY CONFIGURATION", width=dispsize, height=dispsize, autosize=True) }, { 'data': traces3, 'layout': go.Layout(xaxis={'title': 'Spatial frequency : U (pixels)'}, yaxis={'title': 'Spatial frequency : V (pixels)'}, title="SPATIAL FREQUENCY COVERAGE", width=dispsize, height=dispsize, autosize=True) }, { 'data': traces2, 'layout': go.Layout(xaxis={'title': 'Right Ascension (pixels)'}, yaxis={'title': 'Declination (pixels)'}, title="OBSERVED IMAGE", width=dispsize, height=dispsize, autosize=True) }]
def plot_covariance_returns_correlation(correlation, title): config = helper.generate_config() graph_path = "graphs/{}.html".format(_sanatize_string(title)) data = [] dendro_top = ff.create_dendrogram(correlation, orientation="bottom") for i in range(len(dendro_top["data"])): dendro_top["data"][i]["yaxis"] = "y2" data.extend(dendro_top["data"]) dendro_left = ff.create_dendrogram(correlation, orientation="right") for i in range(len(dendro_left["data"])): dendro_left["data"][i]["xaxis"] = "x2" data.extend(dendro_left["data"]) heatmap_hover_text = _generate_hover_text( correlation.index, correlation.columns, correlation.values, "Ticker 2", "Ticker 1", "Correlation", ) heatmap_trace = go.Heatmap( x=dendro_top["layout"]["xaxis"]["tickvals"], y=dendro_left["layout"]["yaxis"]["tickvals"], z=correlation.values, zauto=False, zmax=1.0, zmin=-1.0, text=heatmap_hover_text, hoverinfo="text", ) data.append(heatmap_trace) xaxis1_layout = { "showgrid": False, "showline": False, "zeroline": False, "showticklabels": False, "ticks": "", } xaxis2_layout = { "showgrid": False, "zeroline": False, "showticklabels": False } layout = go.Layout(title=title, showlegend=False, width=800, height=800) figure = go.Figure(data=data, layout=layout) figure["layout"]["xaxis"].update({"domain": [0.15, 1]}) figure["layout"]["xaxis"].update(xaxis1_layout) figure["layout"]["yaxis"].update({"domain": [0, 0.85]}) figure["layout"]["yaxis"].update(xaxis1_layout) figure["layout"]["xaxis2"].update({"domain": [0, 0.15]}) figure["layout"]["xaxis2"].update(xaxis2_layout) figure["layout"]["yaxis2"].update({"domain": [0.825, 0.975]}) figure["layout"]["yaxis2"].update(xaxis2_layout) offline_py.plot(figure, config=config, filename=graph_path, auto_open=False) display( HTML( 'The graph for {} is too large. You can view it <a href="{}" target="_blank">here</a>.' .format(title, graph_path)))
def imshow( img, zmin=None, zmax=None, origin=None, labels={}, x=None, y=None, color_continuous_scale=None, color_continuous_midpoint=None, range_color=None, title=None, template=None, width=None, height=None, aspect=None, ): """ Display an image, i.e. data on a 2D regular raster. Parameters ---------- img: array-like image, or xarray The image data. Supported array shapes are - (M, N): an image with scalar data. The data is visualized using a colormap. - (M, N, 3): an image with RGB values. - (M, N, 4): an image with RGBA values, i.e. including transparency. zmin, zmax : scalar or iterable, optional zmin and zmax define the scalar range that the colormap covers. By default, zmin and zmax correspond to the min and max values of the datatype for integer datatypes (ie [0-255] for uint8 images, [0, 65535] for uint16 images, etc.). For a multichannel image of floats, the max of the image is computed and zmax is the smallest power of 256 (1, 255, 65535) greater than this max value, with a 5% tolerance. For a single-channel image, the max of the image is used. Overridden by range_color. origin : str, 'upper' or 'lower' (default 'upper') position of the [0, 0] pixel of the image array, in the upper left or lower left corner. The convention 'upper' is typically used for matrices and images. labels : dict with str keys and str values (default `{}`) Sets names used in the figure for axis titles (keys ``x`` and ``y``), colorbar title and hoverlabel (key ``color``). The values should correspond to the desired label to be displayed. If ``img`` is an xarray, dimension names are used for axis titles, and long name for the colorbar title (unless overridden in ``labels``). Possible keys are: x, y, and color. x, y: list-like, optional x and y are used to label the axes of single-channel heatmap visualizations and their lengths must match the lengths of the second and first dimensions of the img argument. They are auto-populated if the input is an xarray. color_continuous_scale : str or list of str colormap used to map scalar data to colors (for a 2D image). This parameter is not used for RGB or RGBA images. If a string is provided, it should be the name of a known color scale, and if a list is provided, it should be a list of CSS- compatible colors. color_continuous_midpoint : number If set, computes the bounds of the continuous color scale to have the desired midpoint. Overridden by range_color or zmin and zmax. range_color : list of two numbers If provided, overrides auto-scaling on the continuous color scale, including overriding `color_continuous_midpoint`. Also overrides zmin and zmax. Used only for single-channel images. title : str The figure title. template : str or dict or plotly.graph_objects.layout.Template instance The figure template name or definition. width : number The figure width in pixels. height: number The figure height in pixels. aspect: 'equal', 'auto', or None - 'equal': Ensures an aspect ratio of 1 or pixels (square pixels) - 'auto': The axes is kept fixed and the aspect ratio of pixels is adjusted so that the data fit in the axes. In general, this will result in non-square pixels. - if None, 'equal' is used for numpy arrays and 'auto' for xarrays (which have typically heterogeneous coordinates) Returns ------- fig : graph_objects.Figure containing the displayed image See also -------- plotly.graph_objects.Image : image trace plotly.graph_objects.Heatmap : heatmap trace Notes ----- In order to update and customize the returned figure, use `go.Figure.update_traces` or `go.Figure.update_layout`. If an xarray is passed, dimensions names and coordinates are used for axes labels and ticks. """ args = locals() apply_default_cascade(args) labels = labels.copy() if xarray_imported and isinstance(img, xarray.DataArray): y_label, x_label = img.dims[0], img.dims[1] # np.datetime64 is not handled correctly by go.Heatmap for ax in [x_label, y_label]: if np.issubdtype(img.coords[ax].dtype, np.datetime64): img.coords[ax] = img.coords[ax].astype(str) if x is None: x = img.coords[x_label] if y is None: y = img.coords[y_label] if aspect is None: aspect = "auto" if labels.get("x", None) is None: labels["x"] = x_label if labels.get("y", None) is None: labels["y"] = y_label if labels.get("color", None) is None: labels["color"] = xarray.plot.utils.label_from_attrs(img) labels["color"] = labels["color"].replace("\n", "<br>") else: if labels.get("x", None) is None: labels["x"] = "" if labels.get("y", None) is None: labels["y"] = "" if labels.get("color", None) is None: labels["color"] = "" if aspect is None: aspect = "equal" img = np.asanyarray(img) # Cast bools to uint8 (also one byte) if img.dtype == np.bool: img = 255 * img.astype(np.uint8) # For 2d data, use Heatmap trace if img.ndim == 2: if y is not None and img.shape[0] != len(y): raise ValueError( "The length of the y vector must match the length of the first " + "dimension of the img matrix.") if x is not None and img.shape[1] != len(x): raise ValueError( "The length of the x vector must match the length of the second " + "dimension of the img matrix.") trace = go.Heatmap(x=x, y=y, z=img, coloraxis="coloraxis1") autorange = True if origin == "lower" else "reversed" layout = dict(yaxis=dict(autorange=autorange)) if aspect == "equal": layout["xaxis"] = dict(scaleanchor="y", constrain="domain") layout["yaxis"]["constrain"] = "domain" colorscale_validator = ColorscaleValidator("colorscale", "imshow") if zmin is not None and zmax is None: zmax = img.max() if zmax is not None and zmin is None: zmin = img.min() range_color = range_color or [zmin, zmax] layout["coloraxis1"] = dict( colorscale=colorscale_validator.validate_coerce( args["color_continuous_scale"]), cmid=color_continuous_midpoint, cmin=range_color[0], cmax=range_color[1], ) if labels["color"]: layout["coloraxis1"]["colorbar"] = dict(title_text=labels["color"]) # For 2D+RGB data, use Image trace elif img.ndim == 3 and img.shape[-1] in [3, 4]: if zmax is None and img.dtype is not np.uint8: zmax = _infer_zmax_from_type(img) zmin, zmax = _vectorize_zvalue(zmin), _vectorize_zvalue(zmax) trace = go.Image(z=img, zmin=zmin, zmax=zmax) layout = {} if origin == "lower": layout["yaxis"] = dict(autorange=True) else: raise ValueError( "px.imshow only accepts 2D single-channel, RGB or RGBA images. " "An image of shape %s was provided" % str(img.shape)) layout_patch = dict() for attr_name in ["height", "width"]: if args[attr_name]: layout_patch[attr_name] = args[attr_name] if args["title"]: layout_patch["title_text"] = args["title"] elif args["template"].layout.margin.t is None: layout_patch["margin"] = {"t": 60} fig = go.Figure(data=trace, layout=layout) fig.update_layout(layout_patch) fig.update_traces( hovertemplate="%s: %%{x}<br>%s: %%{y}<br>%s: %%{z}<extra></extra>" % ( labels["x"] or "x", labels["y"] or "y", labels["color"] or "color", )) if labels["x"]: fig.update_xaxes(title_text=labels["x"]) if labels["y"]: fig.update_yaxes(title_text=labels["y"]) fig.update_layout(template=args["template"], overwrite=True) return fig
def imshow( img, zmin=None, zmax=None, origin=None, labels={}, x=None, y=None, color_continuous_scale=None, color_continuous_midpoint=None, range_color=None, title=None, template=None, width=None, height=None, aspect=None, contrast_rescaling=None, binary_string=None, binary_backend="auto", binary_compression_level=4, binary_format="png", ): """ Display an image, i.e. data on a 2D regular raster. Parameters ---------- img: array-like image, or xarray The image data. Supported array shapes are - (M, N): an image with scalar data. The data is visualized using a colormap. - (M, N, 3): an image with RGB values. - (M, N, 4): an image with RGBA values, i.e. including transparency. zmin, zmax : scalar or iterable, optional zmin and zmax define the scalar range that the colormap covers. By default, zmin and zmax correspond to the min and max values of the datatype for integer datatypes (ie [0-255] for uint8 images, [0, 65535] for uint16 images, etc.). For a multichannel image of floats, the max of the image is computed and zmax is the smallest power of 256 (1, 255, 65535) greater than this max value, with a 5% tolerance. For a single-channel image, the max of the image is used. Overridden by range_color. origin : str, 'upper' or 'lower' (default 'upper') position of the [0, 0] pixel of the image array, in the upper left or lower left corner. The convention 'upper' is typically used for matrices and images. labels : dict with str keys and str values (default `{}`) Sets names used in the figure for axis titles (keys ``x`` and ``y``), colorbar title and hoverlabel (key ``color``). The values should correspond to the desired label to be displayed. If ``img`` is an xarray, dimension names are used for axis titles, and long name for the colorbar title (unless overridden in ``labels``). Possible keys are: x, y, and color. x, y: list-like, optional x and y are used to label the axes of single-channel heatmap visualizations and their lengths must match the lengths of the second and first dimensions of the img argument. They are auto-populated if the input is an xarray. color_continuous_scale : str or list of str colormap used to map scalar data to colors (for a 2D image). This parameter is not used for RGB or RGBA images. If a string is provided, it should be the name of a known color scale, and if a list is provided, it should be a list of CSS- compatible colors. color_continuous_midpoint : number If set, computes the bounds of the continuous color scale to have the desired midpoint. Overridden by range_color or zmin and zmax. range_color : list of two numbers If provided, overrides auto-scaling on the continuous color scale, including overriding `color_continuous_midpoint`. Also overrides zmin and zmax. Used only for single-channel images. title : str The figure title. template : str or dict or plotly.graph_objects.layout.Template instance The figure template name or definition. width : number The figure width in pixels. height: number The figure height in pixels. aspect: 'equal', 'auto', or None - 'equal': Ensures an aspect ratio of 1 or pixels (square pixels) - 'auto': The axes is kept fixed and the aspect ratio of pixels is adjusted so that the data fit in the axes. In general, this will result in non-square pixels. - if None, 'equal' is used for numpy arrays and 'auto' for xarrays (which have typically heterogeneous coordinates) contrast_rescaling: 'minmax', 'infer', or None how to determine data values corresponding to the bounds of the color range, when zmin or zmax are not passed. If `minmax`, the min and max values of the image are used. If `infer`, a heuristic based on the image data type is used. binary_string: bool, default None if True, the image data are first rescaled and encoded as uint8 and then passed to plotly.js as a b64 PNG string. If False, data are passed unchanged as a numerical array. Setting to True may lead to performance gains, at the cost of a loss of precision depending on the original data type. If None, use_binary_string is set to True for multichannel (eg) RGB arrays, and to False for single-channel (2D) arrays. 2D arrays are represented as grayscale and with no colorbar if use_binary_string is True. binary_backend: str, 'auto' (default), 'pil' or 'pypng' Third-party package for the transformation of numpy arrays to png b64 strings. If 'auto', Pillow is used if installed, otherwise pypng. binary_compression_level: int, between 0 and 9 (default 4) png compression level to be passed to the backend when transforming an array to a png b64 string. Increasing `binary_compression` decreases the size of the png string, but the compression step takes more time. For most images it is not worth using levels greater than 5, but it's possible to test `len(fig.data[0].source)` and to time the execution of `imshow` to tune the level of compression. 0 means no compression (not recommended). binary_format: str, 'png' (default) or 'jpg' compression format used to generate b64 string. 'png' is recommended since it uses lossless compression, but 'jpg' (lossy) compression can result if smaller binary strings for natural images. Returns ------- fig : graph_objects.Figure containing the displayed image See also -------- plotly.graph_objects.Image : image trace plotly.graph_objects.Heatmap : heatmap trace Notes ----- In order to update and customize the returned figure, use `go.Figure.update_traces` or `go.Figure.update_layout`. If an xarray is passed, dimensions names and coordinates are used for axes labels and ticks. """ args = locals() apply_default_cascade(args) labels = labels.copy() # ----- Define x and y, set labels if img is an xarray ------------------- if xarray_imported and isinstance(img, xarray.DataArray): if binary_string: raise ValueError( "It is not possible to use binary image strings for xarrays." "Please pass your data as a numpy array instead using" "`img.values`" ) y_label, x_label = img.dims[0], img.dims[1] # np.datetime64 is not handled correctly by go.Heatmap for ax in [x_label, y_label]: if np.issubdtype(img.coords[ax].dtype, np.datetime64): img.coords[ax] = img.coords[ax].astype(str) if x is None: x = img.coords[x_label] if y is None: y = img.coords[y_label] if aspect is None: aspect = "auto" if labels.get("x", None) is None: labels["x"] = x_label if labels.get("y", None) is None: labels["y"] = y_label if labels.get("color", None) is None: labels["color"] = xarray.plot.utils.label_from_attrs(img) labels["color"] = labels["color"].replace("\n", "<br>") else: if hasattr(img, "columns") and hasattr(img.columns, "__len__"): if x is None: x = img.columns if labels.get("x", None) is None and hasattr(img.columns, "name"): labels["x"] = img.columns.name or "" if hasattr(img, "index") and hasattr(img.index, "__len__"): if y is None: y = img.index if labels.get("y", None) is None and hasattr(img.index, "name"): labels["y"] = img.index.name or "" if labels.get("x", None) is None: labels["x"] = "" if labels.get("y", None) is None: labels["y"] = "" if labels.get("color", None) is None: labels["color"] = "" if aspect is None: aspect = "equal" # --- Set the value of binary_string (forbidden for pandas) if isinstance(img, pd.DataFrame): if binary_string: raise ValueError("Binary strings cannot be used with pandas arrays") is_dataframe = True else: is_dataframe = False # --------------- Starting from here img is always a numpy array -------- img = np.asanyarray(img) # Default behaviour of binary_string: True for RGB images, False for 2D if binary_string is None: binary_string = img.ndim >= 3 and not is_dataframe # Cast bools to uint8 (also one byte) if img.dtype == np.bool: img = 255 * img.astype(np.uint8) if range_color is not None: zmin = range_color[0] zmax = range_color[1] # -------- Contrast rescaling: either minmax or infer ------------------ if contrast_rescaling is None: contrast_rescaling = "minmax" if img.ndim == 2 else "infer" # We try to set zmin and zmax only if necessary, because traces have good defaults if contrast_rescaling == "minmax": # When using binary_string and minmax we need to set zmin and zmax to rescale the image if (zmin is not None or binary_string) and zmax is None: zmax = img.max() if (zmax is not None or binary_string) and zmin is None: zmin = img.min() else: # For uint8 data and infer we let zmin and zmax to be None if passed as None if zmax is None and img.dtype != np.uint8: zmax = _infer_zmax_from_type(img) if zmin is None and zmax is not None: zmin = 0 # For 2d data, use Heatmap trace, unless binary_string is True if img.ndim == 2 and not binary_string: if y is not None and img.shape[0] != len(y): raise ValueError( "The length of the y vector must match the length of the first " + "dimension of the img matrix." ) if x is not None and img.shape[1] != len(x): raise ValueError( "The length of the x vector must match the length of the second " + "dimension of the img matrix." ) trace = go.Heatmap(x=x, y=y, z=img, coloraxis="coloraxis1") autorange = True if origin == "lower" else "reversed" layout = dict(yaxis=dict(autorange=autorange)) if aspect == "equal": layout["xaxis"] = dict(scaleanchor="y", constrain="domain") layout["yaxis"]["constrain"] = "domain" colorscale_validator = ColorscaleValidator("colorscale", "imshow") layout["coloraxis1"] = dict( colorscale=colorscale_validator.validate_coerce( args["color_continuous_scale"] ), cmid=color_continuous_midpoint, cmin=zmin, cmax=zmax, ) if labels["color"]: layout["coloraxis1"]["colorbar"] = dict(title_text=labels["color"]) # For 2D+RGB data, use Image trace elif img.ndim == 3 and img.shape[-1] in [3, 4] or (img.ndim == 2 and binary_string): rescale_image = True # to check whether image has been modified if zmin is not None and zmax is not None: zmin, zmax = ( _vectorize_zvalue(zmin, mode="min"), _vectorize_zvalue(zmax, mode="max"), ) if binary_string: if zmin is None and zmax is None: # no rescaling, faster img_rescaled = img rescale_image = False elif img.ndim == 2: img_rescaled = rescale_intensity( img, in_range=(zmin[0], zmax[0]), out_range=np.uint8 ) else: img_rescaled = np.dstack( [ rescale_intensity( img[..., ch], in_range=(zmin[ch], zmax[ch]), out_range=np.uint8, ) for ch in range(img.shape[-1]) ] ) img_str = _array_to_b64str( img_rescaled, backend=binary_backend, compression=binary_compression_level, ext=binary_format, ) trace = go.Image(source=img_str) else: colormodel = "rgb" if img.shape[-1] == 3 else "rgba256" trace = go.Image(z=img, zmin=zmin, zmax=zmax, colormodel=colormodel) layout = {} if origin == "lower": layout["yaxis"] = dict(autorange=True) else: raise ValueError( "px.imshow only accepts 2D single-channel, RGB or RGBA images. " "An image of shape %s was provided" % str(img.shape) ) layout_patch = dict() for attr_name in ["height", "width"]: if args[attr_name]: layout_patch[attr_name] = args[attr_name] if args["title"]: layout_patch["title_text"] = args["title"] elif args["template"].layout.margin.t is None: layout_patch["margin"] = {"t": 60} fig = go.Figure(data=trace, layout=layout) fig.update_layout(layout_patch) # Hover name, z or color if binary_string and rescale_image and not np.all(img == img_rescaled): # we rescaled the image, hence z is not displayed in hover since it does # not correspond to img values hovertemplate = "%s: %%{x}<br>%s: %%{y}<extra></extra>" % ( labels["x"] or "x", labels["y"] or "y", ) else: if trace["type"] == "heatmap": hover_name = "%{z}" elif img.ndim == 2: hover_name = "%{z[0]}" elif img.ndim == 3 and img.shape[-1] == 3: hover_name = "[%{z[0]}, %{z[1]}, %{z[2]}]" else: hover_name = "%{z}" hovertemplate = "%s: %%{x}<br>%s: %%{y}<br>%s: %s<extra></extra>" % ( labels["x"] or "x", labels["y"] or "y", labels["color"] or "color", hover_name, ) fig.update_traces(hovertemplate=hovertemplate) if labels["x"]: fig.update_xaxes(title_text=labels["x"]) if labels["y"]: fig.update_yaxes(title_text=labels["y"]) fig.update_layout(template=args["template"], overwrite=True) return fig
def plot_nf_heatmap(plot, input_data): """Generate the plot(s) with algorithm: plot_nf_heatmap specified in the specification file. :param plot: Plot to generate. :param input_data: Data to process. :type plot: pandas.Series :type input_data: InputData """ regex_cn = re.compile(r'^(\d*)R(\d*)C$') regex_test_name = re.compile(r'^.*-(\d+ch|\d+pl)-' r'(\d+mif|\d+vh)-' r'(\d+vm\d+t|\d+dcr\d+t|\d+dcr\d+c).*$') vals = dict() # Transform the data logging.info(f" Creating the data set for the {plot.get(u'type', u'')} " f"{plot.get(u'title', u'')}.") data = input_data.filter_data(plot, continue_on_error=True) if data is None or data.empty: logging.error(u"No data.") return for job in data: for build in job: for test in build: for tag in test[u"tags"]: groups = re.search(regex_cn, tag) if groups: chain = str(groups.group(1)) node = str(groups.group(2)) break else: continue groups = re.search(regex_test_name, test[u"name"]) if groups and len(groups.groups()) == 3: hover_name = (f"{str(groups.group(1))}-" f"{str(groups.group(2))}-" f"{str(groups.group(3))}") else: hover_name = u"" if vals.get(chain, None) is None: vals[chain] = dict() if vals[chain].get(node, None) is None: vals[chain][node] = dict(name=hover_name, vals=list(), nr=None, mean=None, stdev=None) try: if plot[u"include-tests"] == u"MRR": result = test[u"result"][u"receive-rate"] elif plot[u"include-tests"] == u"PDR": result = test[u"throughput"][u"PDR"][u"LOWER"] elif plot[u"include-tests"] == u"NDR": result = test[u"throughput"][u"NDR"][u"LOWER"] else: result = None except TypeError: result = None if result: vals[chain][node][u"vals"].append(result) if not vals: logging.error(u"No data.") return txt_chains = list() txt_nodes = list() for key_c in vals: txt_chains.append(key_c) for key_n in vals[key_c].keys(): txt_nodes.append(key_n) if vals[key_c][key_n][u"vals"]: vals[key_c][key_n][u"nr"] = len(vals[key_c][key_n][u"vals"]) vals[key_c][key_n][u"mean"] = \ round(mean(vals[key_c][key_n][u"vals"]) / 1000000, 1) vals[key_c][key_n][u"stdev"] = \ round(stdev(vals[key_c][key_n][u"vals"]) / 1000000, 1) txt_nodes = list(set(txt_nodes)) def sort_by_int(value): """Makes possible to sort a list of strings which represent integers. :param value: Integer as a string. :type value: str :returns: Integer representation of input parameter 'value'. :rtype: int """ return int(value) txt_chains = sorted(txt_chains, key=sort_by_int) txt_nodes = sorted(txt_nodes, key=sort_by_int) chains = [i + 1 for i in range(len(txt_chains))] nodes = [i + 1 for i in range(len(txt_nodes))] data = [list() for _ in range(len(chains))] for chain in chains: for node in nodes: try: val = vals[txt_chains[chain - 1]][txt_nodes[node - 1]][u"mean"] except (KeyError, IndexError): val = None data[chain - 1].append(val) # Color scales: my_green = [[0.0, u"rgb(235, 249, 242)"], [1.0, u"rgb(45, 134, 89)"]] my_blue = [[0.0, u"rgb(236, 242, 248)"], [1.0, u"rgb(57, 115, 172)"]] my_grey = [[0.0, u"rgb(230, 230, 230)"], [1.0, u"rgb(102, 102, 102)"]] hovertext = list() annotations = list() text = (u"Test: {name}<br>" u"Runs: {nr}<br>" u"Thput: {val}<br>" u"StDev: {stdev}") for chain, _ in enumerate(txt_chains): hover_line = list() for node, _ in enumerate(txt_nodes): if data[chain][node] is not None: annotations.append( dict(x=node + 1, y=chain + 1, xref=u"x", yref=u"y", xanchor=u"center", yanchor=u"middle", text=str(data[chain][node]), font=dict(size=14, ), align=u"center", showarrow=False)) hover_line.append( text.format( name=vals[txt_chains[chain]][txt_nodes[node]][u"name"], nr=vals[txt_chains[chain]][txt_nodes[node]][u"nr"], val=data[chain][node], stdev=vals[txt_chains[chain]][ txt_nodes[node]][u"stdev"])) hovertext.append(hover_line) traces = [ plgo.Heatmap(x=nodes, y=chains, z=data, colorbar=dict( title=plot.get(u"z-axis", u""), titleside=u"right", titlefont=dict(size=16), tickfont=dict(size=16, ), tickformat=u".1f", yanchor=u"bottom", y=-0.02, len=0.925, ), showscale=True, colorscale=my_green, text=hovertext, hoverinfo=u"text") ] for idx, item in enumerate(txt_nodes): # X-axis, numbers: annotations.append( dict(x=idx + 1, y=0.05, xref=u"x", yref=u"y", xanchor=u"center", yanchor=u"top", text=item, font=dict(size=16, ), align=u"center", showarrow=False)) for idx, item in enumerate(txt_chains): # Y-axis, numbers: annotations.append( dict(x=0.35, y=idx + 1, xref=u"x", yref=u"y", xanchor=u"right", yanchor=u"middle", text=item, font=dict(size=16, ), align=u"center", showarrow=False)) # X-axis, title: annotations.append( dict(x=0.55, y=-0.15, xref=u"paper", yref=u"y", xanchor=u"center", yanchor=u"bottom", text=plot.get(u"x-axis", u""), font=dict(size=16, ), align=u"center", showarrow=False)) # Y-axis, title: annotations.append( dict(x=-0.1, y=0.5, xref=u"x", yref=u"paper", xanchor=u"center", yanchor=u"middle", text=plot.get(u"y-axis", u""), font=dict(size=16, ), align=u"center", textangle=270, showarrow=False)) updatemenus = list([ dict(x=1.0, y=0.0, xanchor=u"right", yanchor=u"bottom", direction=u"up", buttons=list([ dict(args=[{ u"colorscale": [ my_green, ], u"reversescale": False }], label=u"Green", method=u"update"), dict(args=[{ u"colorscale": [ my_blue, ], u"reversescale": False }], label=u"Blue", method=u"update"), dict(args=[{ u"colorscale": [ my_grey, ], u"reversescale": False }], label=u"Grey", method=u"update") ])) ]) try: layout = deepcopy(plot[u"layout"]) except KeyError as err: logging.error(f"Finished with error: No layout defined\n{repr(err)}") return layout[u"annotations"] = annotations layout[u'updatemenus'] = updatemenus try: # Create plot plpl = plgo.Figure(data=traces, layout=layout) # Export Plot logging.info(f" Writing file {plot[u'output-file']}.html") ploff.plot(plpl, show_link=False, auto_open=False, filename=f"{plot[u'output-file']}.html") except PlotlyError as err: logging.error(f" Finished with error: {repr(err)}".replace( u"\n", u" ")) return
def plotly_heatmap(self): """plot_heatmap, but with plotly output""" fig = go.Figure() cols = self.plots_per_row if self.numplots > self.plots_per_row else self.numplots rows = np.ceil(self.numplots / float(cols)).astype(int) fig['layout'].update(title=self.plot_title) domainWidth = .9 / cols domainHeight = .9 / rows bufferHeight = 0.0 if rows > 1: bufferHeight = 0.1 / (rows - 1) else: domainHeight = 1.0 bufferWidth = 0.0 if cols > 1: bufferWidth = 0.1 / (cols - 1) else: domainWidth = 1.0 data = [] annos = [] zmin = np.inf zmax = -np.inf for i in range(self.numplots): row = rows - i / self.plots_per_row - 1 col = i % self.plots_per_row if self.per_group: title = self.hm.matrix.group_labels[i] else: title = self.hm.matrix.sample_labels[i] base = row * (domainHeight + bufferHeight) domain = [base, base + domainHeight] titleY = base + domainHeight xanchor = 'x{}'.format(i + 1) yanchor = 'y{}'.format(i + 1) visible = False if col == 0: visible = True fig['layout']['yaxis{}'.format(i + 1)] = {'domain': domain, 'anchor': xanchor, 'visible': visible} base = col * (domainWidth + bufferWidth) domain = [base, base + domainWidth] titleX = base + 0.5 * domainWidth fig['layout']['xaxis{}'.format(i + 1)] = {'domain': domain, 'anchor': yanchor} annos.append({'yanchor': 'bottom', 'xref': 'paper', 'xanchor': 'center', 'yref': 'paper', 'text': title, 'y': titleY, 'x': titleX, 'font': {'size': 16}, 'showarrow': False}) mat = [] labels = [] for j in range(self.numlines): if self.per_group: row, col = i, j else: row, col = j, i sub_matrix = self.hm.matrix.get_matrix(row, col) if self.per_group: label = sub_matrix['sample'] else: label = sub_matrix['group'] labels.append(label) mat.append(np.ma.__getattribute__(self.averagetype)(sub_matrix['matrix'], axis=0)) if np.min(mat[-1]) < zmin: zmin = np.min(mat[-1]) if np.max(mat[-1]) > zmax: zmax = np.max(mat[-1]) totalWidth = len(mat[-1]) trace = go.Heatmap(name=title, z=mat, x=range(totalWidth + 1), y=labels, xaxis=xanchor, yaxis=yanchor) data.append(trace) # Add ticks xticks, xtickslabel = self.getTicks(i) if np.ceil(max(xticks)) != float(totalWidth): tickscale = float(totalWidth) / max(xticks) xticks_use = [x * tickscale for x in xticks] else: xticks_use = xticks xticks_use = [np.ceil(x) for x in xticks_use] fig['layout']['xaxis{}'.format(i + 1)].update(tickmode='array', tickvals=xticks_use, ticktext=xtickslabel, tickangle=self.label_rotation) # Adjust color scale limits for i, trace in enumerate(data): zminUse = zmin zmaxUse = zmax if self.y_min[i % len(self.y_min)] is not None: zminUse = self.y_min[i % len(self.y_min)] if self.y_max[i % len(self.y_max)] is not None: zmaxUse = self.y_max[i % len(self.y_max)] trace.update(zmin=zminUse, zmax=zmaxUse) fig['data'] = data fig['layout']['annotations'] = annos py.plot(fig, filename=self.out_file_name, auto_open=False)
m2.append(males2[0][i]) m3.append(males3[0][i]) f1.append(females1[0][i]) f2.append(females2[0][i]) f3.append(females3[0][i]) for j in range(0, 11): cor[i][j] = cor1[i][j] + cor2[i][j] + cor3[i][j] candidates = [ 'NDA', 'MLP', 'EM', 'BH', 'NA', 'PP', 'JC', 'JL', 'JLM', 'FA', 'FF' ] trace0 = go.Heatmap(z=cor, x=candidates, y=candidates, colorbar=dict(x=0.47, y=0.81, len=0.4), colorscale='Viridis', name='All VTs') trace1 = go.Heatmap(z=cor1, x=candidates, y=candidates, colorbar=dict(y=0.81, len=0.4), name='VT1') trace2 = go.Heatmap(z=cor2, x=candidates, y=candidates, colorbar=dict(x=0.47, y=0.19, len=0.4), name='VT2') trace3 = go.Heatmap(z=cor3, x=candidates, y=candidates,
plty.iplot(fig, filename='bar-direct-labels') # with kfold base_predictions_train = pd.DataFrame({ 'RandomeForest': rf_oof_train.ravel(), 'ExtraTrees': et_oof_train.ravel(), 'AdaBoost': ada_oof_train.ravel(), 'GradientBoost': gb_oof_train.ravel(), }) base_predictions_train.head() data = [ go.Heatmap( z=base_predictions_train.astype(float).corr().values, x=base_predictions_train.columns.values, y=base_predictions_train.columns.values, colorscale='Portland', showscale=True, # reverscale=True ) ] plty.iplot(data, filename='labelled-heatmap') x_train = np.concatenate((et_oof_train, rf_oof_train, ada_oof_train, gb_oof_train, svc_oof_train), axis=1) x_test = np.concatenate((et_oof_test, rf_oof_test, ada_oof_test, gb_oof_test, svc_oof_test), axis=1) gbm = xgb.XGBClassifier( learning_rate=0.02, n_estimators=2000, max_depth=4, min_child_weight=2,
H.set_dict_options(options) H #il est nécessaire d'installer 'plotly' si ce n'est pas déjà fait # Connexion à l'API avec un compte existant import plotly plotly.tools.set_credentials_file(username='******', api_key='P7zRkAFgDxfcFdcGwzTP') # Chargement des librairies import plotly.plotly as py import plotly.graph_objs as go # Création de la 'heatmap' heatmap = go.Heatmap(z=acteurs_large.as_matrix(), x=acteurs_large.columns, y=acteurs_large.index, colorscale='Jet') data = [heatmap] py.iplot(data, filename='ATMO-heatmap') # Définition des accès user = '******' password = '******' machine = '172.20.152.200' mySQLengine = create_engine("mysql://%s:%s@%s/?charset=utf8" % (user, password, machine)) # Ici, on execute directement du SQL: "USE XXX" mySQLengine.execute("USE `BDD_Alexandre`;") # Ecriture des données dataframe -> mySQL # Après avoir supprimé (si existante) la table 'people'
# df = pd.DataFrame(data=evaluated_response_data, # columns=['apple', 'google', 'amazon', 'facebook', 'microsoft']) # df.head() doc = collection.find_one() heat_map_df = pd.DataFrame(data=[doc for doc in collection.find()]) heat_map_df.drop(columns=['_id'], inplace=True) heat_map_df.set_index('METRIC NAME', inplace=True) heat_map_df.head() response_df = pd.Dataframe() app = dash.Dash() app.layout = html.Div([ dcc.Graph(id='heap_map', figure={ 'data': [ go.Heatmap(z=heat_map_df.values.tolist(), x=heat_map_df.columns.tolist(), y=heat_map_df.index.tolist()) ], 'layout': go.Layout(title='Heap Map of Company Response') }) ]) if __name__ == '__main__': PORT = 8000 ADDRESS = '127.0.0.1' app.run_server(port=PORT, host=ADDRESS)
import plotly.offline as pyo import plotly.graph_objs as go import pandas as pd df = pd.read_csv('dados/2010YumaAZ.csv') data = [go.Heatmap(x=df['DAY'],y=df['LST_TIME'], z=df['T_HR_AVG'].values.tolist())] layout = go.Layout(title='Yuma') fig = go.Figure(data=data, layout=layout) pyo.plot(fig, filename='HTML/heatmaps_2.html')
#API = raw_input("Enter your plotly API key: ") #py.sign_in(username, API) with Dataset("tmin.nc", "r") as tmin: tempMinJ = tmin.variables["tmin"][0] tempMinA = tmin.variables["tmin"][91] tempMinY = tmin.variables["tmin"][182] tempMinO = tmin.variables["tmin"][274] #min temps at certain days of year #of course I would loop this through every day of the year if I wasn't just doing four for now... #plot the heatmaps for tmin with the same zmin and zmax values data1 = [go.Heatmap( z=tempMinJ, zmin=-7, zmax=27, )] data2 = [go.Heatmap( z=tempMinA, zmin=-7, zmax=27, )] data3 = [go.Heatmap( z=tempMinY, zmin=-7, zmax=27, )]
def plot_pfb(filename, decimation=None, low_pass=None, backend='matplotlib', output_filename=None, start_time=None, end_time=None, auto_open=True, **kwargs): ''' Plot the output of a PFB acquisition as an heatmap. :retrurn Name of the file. ''' final_filename = "" filename = format_filename(filename) parameters = global_parameter() parameters.retrive_prop_from_file(filename) ant = parameters.get_active_rx_param() try: usrp_number = kwargs['usrp_number'] except KeyError: usrp_number = None if len(ant) > 1: print_error("multiple RX devices not yet supported") return if parameters.get(ant[0], 'wave_type')[0] != "NOISE": print_warning( "The file selected does not have the PFB acquisition tag. Errors may occour" ) fft_tones = parameters.get(ant[0], 'fft_tones') rate = parameters.get(ant[0], 'rate') channel_width = rate / fft_tones decimation = parameters.get(ant[0], 'decim') integ_time = fft_tones * max(decimation, 1) / rate rf = parameters.get(ant[0], 'rf') if start_time is not None: start_time *= effective_rate else: start_time = 0 if end_time is not None: end_time *= effective_rate try: front_end = kwargs['front_end'] except KeyError: front_end = None samples, errors = openH5file(filename, ch_list=None, start_sample=start_time, last_sample=end_time, usrp_number=usrp_number, front_end=front_end, verbose=False, error_coord=True) if output_filename is None: output_filename = "PFB_waterfall_" + get_timestamp() y_label = np.arange(len(samples[0]) / fft_tones) / (rate / (fft_tones * max(1, decimation))) x_label = (rf + (np.arange(fft_tones) - fft_tones / 2) * (rate / fft_tones)) / 1e6 title = "PFB acquisition form file %s" % filename subtitle = "Channel width %.2f kHz; Frame integration time: %.2e s" % ( channel_width / 1.e3, integ_time) with warnings.catch_warnings(): # it's very likely to do some division by 0 in the log10 warnings.simplefilter("ignore") z = 20 * np.log10(np.abs(samples[0])) try: z_shaped = np.roll(np.reshape(z, (len(z) / fft_tones, fft_tones)), -fft_tones / 2, axis=1) except ValueError as msg: print_warning("Error while plotting pfb spectra: " + str(msg)) cut = len(z) - len(z) / fft_tones * fft_tones z = z[:-cut] print_debug("Cutting last data (%d samples) to fit" % cut) # z_shaped = np.roll(np.reshape(z,(len(z)/fft_tones,fft_tones)),fft_tones/2,axis = 1) z_shaped = np.roll(np.reshape(z, (len(z) / fft_tones, fft_tones)), -fft_tones / 2, axis=1) # pl.plot(z_shaped.T, alpha = 0.1, color = "k") # pl.show() if backend == 'matplotlib': fig, ax = pl.subplots( nrows=2, ncols=1, ) # sharex=True) try: fig.set_size_inches(kwargs['size'][0], kwargs['size'][1]) except KeyError: fig.set_size_inches(10, 16) ax[0].set_xlabel("Channel [MHz]") ax[0].set_ylabel("Time [s]") ax[0].set_title(title + "\n" + subtitle) ax[0].set_xlim((min(x_label), max(x_label))) imag = ax[0].imshow( z_shaped, aspect='auto', interpolation='nearest', extent=[min(x_label), max(x_label), min(y_label), max(y_label)]) # fig.colorbar(imag)#,ax=ax[0] for zz in z_shaped[::100]: ax[1].plot(x_label, zz, color='k', alpha=0.1) ax[1].set_xlabel("Channel [MHz]") ax[1].set_ylabel("Power [dBm]") ax[1].set_xlim((min(x_label), max(x_label))) # ax[1].set_title("Trace stack") final_filename = output_filename + '.png' fig.savefig(final_filename) pl.close() if backend == 'plotly': data = [ go.Heatmap( z=z_shaped, x=x_label, y=y_label, colorscale='Viridis', ) ] layout = go.Layout(title=title + "<br>" + subtitle, xaxis=dict(title="Channel [MHz]"), yaxis=dict(title="Time [s]")) fig = go.Figure(data=data, layout=layout) final_filename = output_filename + ".html" plotly.offline.plot(fig) plotly.offline.plot(fig, filename=final_filename, auto_open=auto_open) return final_filename
query_command = { 'metadata.timestamp': { '$gt': start_time, '$lt': end_time } } runs = collection.find(query_command) res_stats = list() timestamps = list() for document in runs: res_stats.append(int(document['metadata']['success'])) timestamps.append(document['metadata']['timestamp']) z.append(res_stats) y.append(collection_name) res_heatmap = go.Heatmap(z=z, x=timestamps, y=y, zmin=0, zmax=1, reversescale=True, showscale=False) _res_stats_graph = dcc.Graph(figure=go.Figure(data=[res_heatmap], layout={'title': 'Run Results'}), id='res_heatmap') pass_fail_stats = html.Div([_res_stats_graph])
def plotly_scatter(self, plot_filename, corr_matrix, plot_title='', minXVal=None, maxXVal=None, minYVal=None, maxYVal=None): """Make the scatter plot of a matrix with plotly""" n = self.matrix.shape[1] self.matrix = self.matrix fig = go.Figure() domainWidth = 1. / n annos = [] for i in range(n): x = domainWidth * (i + 1) y = 1 - (domainWidth * i + 0.5 * domainWidth) anno = dict(text=self.labels[i], showarrow=False, xref='paper', yref='paper', x=x, y=y, xanchor='right', yanchor='middle') annos.append(anno) data = [] zMin = np.inf zMax = -np.inf for x in range(n): xanchor = 'x{}'.format(x + 1) base = x * domainWidth domain = [base, base + domainWidth] if x > 0: base = 1 - base fig['layout']['xaxis{}'.format(x + 1)] = dict( domain=domain, range=[minXVal, maxXVal], anchor='free', position=base) for y in range(0, n): yanchor = 'y{}'.format(y + 1) if x == 1: base = 1 - y * domainWidth domain = [base - domainWidth, base] fig['layout']['yaxis{}'.format(y + 1)] = dict( domain=domain, range=[minYVal, maxYVal], side='right', anchor='free', position=1.0) if x > y: vector1 = self.matrix[:, x] vector2 = self.matrix[:, y] Z, xEdges, yEdges = np.histogram2d(vector1, vector2, bins=50) Z = np.log10(Z) if np.min(Z) < zMin: zMin = np.min(Z) if np.max(Z) > zMax: zMax = np.max(Z) name = '{}={:.2f}'.format(self.corr_method, corr_matrix[x, y]) trace = go.Heatmap(z=Z, x=xEdges, y=yEdges, showlegend=False, xaxis=xanchor, yaxis=yanchor, name=name, showscale=False) data.append(trace) # Fix the colorbar bounds for trace in data: trace.update(zmin=zMin, zmax=zMax) data[-1]['colorbar'].update(title="log10(instances per bin)", titleside="right") data[-1].update(showscale=True) fig['data'] = data fig['layout'].update(title=plot_title, showlegend=False, annotations=annos) offline.plot(fig, filename=plot_filename, auto_open=False)
def plot_slot_avail(node_info, sec_since_epoch=None): """Plot "slot" availability. Parameters ---------- node_info : pandas.DataFrame sec_since_epoch : numeric, optional If provided, a date-time stamp will be displayed on the plots indicating the time the information displayed was generated. """ if DEBUG: t0 = time.time() print(">> plot_slot_avail") updated_at = "" if sec_since_epoch is not None: datetime_stamp, _ = make_datetime_stamp(sec_since_epoch, human_readable=True) updated_at = "Updated {}".format(datetime_stamp) slot_cores = np.array( [1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 24, 28, 32, 40]) slot_mem = 2**np.arange(0, 11) cluster_node_info = OrderedDict([ ("ACI CPU", node_info.query('cluster == "aci"')), ( "CyberLAMP CPU-Only", node_info.query('(cluster == "cyberlamp") & (subgroup != "phi")'), ), ( "CyberLAMP Single-GPU", node_info.query('(cluster == "cyberlamp") & (subgroup != "phi")'), ), ]) for cluster_name, cni in cluster_node_info.items(): print(">> Working on {} cluster".format(cluster_name)) # TODO: cyberlamp has different CPUs available on the 1-GPU nodes for # GPU vs. non-GPU jobs. Knowing which cores are "taken" (or if it's # just a count, not particular cores that are dedicatd to job type) is # not possible as the pbsnodes parsing script stands (might need data # from another source about actual jobs that are running on each node # and what they call out). sc = slot_cores[slot_cores <= cni.cores.max()] sm = slot_mem[slot_mem <= cni.mem_tot.max()] if "gpu" in cluster_name.lower(): sg = 1 sc = sc[sc <= 20] sm = sm[sm <= 242] else: sg = 0 slots = compute_open_slots(node_info=cni, slot_cores=sc, slot_mem=sm, slot_gpus=sg) if np.all(slots <= 3): log_slots = slots else: with np.errstate(divide="ignore"): log_slots = np.log10(slots) neginf_mask = np.isinf(log_slots) neginf_fill_val = -np.max(log_slots) / 6 log_slots[neginf_mask] = neginf_fill_val min_slots = slots.min() max_slots = slots.max() lin_tickvals = np.array( [1, 3, 10, 30, 100, 300, 1e3, 3e3, 1e4, 3e4, 1e5, 3e5, 1e6], dtype=int) lin_tickvals = lin_tickvals[(lin_tickvals > min_slots) & (lin_tickvals < max_slots)] if np.all(slots <= 3): lin_tickvals = np.array([0, max_slots], dtype=int) tickvals = lin_tickvals else: if len(lin_tickvals) < 3: order = np.floor(np.log10(min_slots)) lin_tickvals = np.arange( 10**np.floor(np.log10(min_slots)), max_slots + 1, 10**order, dtype=int, ) lin_tickvals = np.array(sorted( set(lin_tickvals.tolist() + [max_slots])), dtype=int) tickvals = np.log10(lin_tickvals) lin_tickvals = sorted(set([0] + lin_tickvals.tolist())) tickvals = [neginf_fill_val] + tickvals.tolist() ticktext = ["{:d}".format(tv) for tv in lin_tickvals] text = np.empty_like(slots, dtype=str).tolist() for (core_i, cores), (mem_i, mem) in product(enumerate(sc), enumerate(sm)): if cores == 1: ct = "core" else: ct = "cores" val = slots[core_i, mem_i] if val == 1: slt = "slot" else: slt = "slots" text[core_i][ mem_i] = "{:d} {:s} & {:d} GiB mem : {:d} {:s}".format( cores, ct, mem, val, slt) # print(f"slots\n{slots}") # print(f"log_slots\n{log_slots}") # print(f"tickvals\n{tickvals}") trace = graph_objs.Heatmap( z=log_slots, x=[str(m) + " GiB" for m in sm], y=[str(sc[0]) + " core"] + [str(c) + " cores" for c in sc[1:]], zsmooth=False, xgap=1, ygap=1, colorscale="Viridis", colorbar=dict( outlinewidth=0, tickvals=tickvals, ticktext=ticktext, ), hoverinfo="text", text=text, ) data = [trace] layout = graph_objs.Layout( title="{} Job Slots Available".format(cluster_name), annotations=graph_objs.Annotations([ graph_objs.Annotation( x=0.5, y=1.07, showarrow=False, text=updated_at, xref="paper", yref="paper", ), ]), xaxis=dict(ticks="", nticks=36), yaxis=dict(ticks=""), ) fig = graph_objs.Figure(data=data, layout=layout) plotly.plot(fig, filename=cluster_name, auto_open=False) if DEBUG: print(">> plot_slot_avail:", time.time() - t0)
def plotlyMatrix(hm, outFilename, yMin=[None], yMax=[None], zMin=[None], zMax=[None], showSummaryPlot=False, cmap=None, colorList=None, colorBarPosition='side', perGroup=False, averageType='median', yAxisLabel='', xAxisLabel='', plotTitle='', showColorbar=False, label_rotation=0.0): label_rotation *= -1.0 if colorBarPosition != 'side': sys.error.write("Warning: It is not currently possible to have multiple colorbars with plotly!\n") nRows = hm.matrix.get_num_groups() nCols = hm.matrix.get_num_samples() if perGroup: nRows, nCols = nCols, nRows profileHeight = 0.0 profileBottomBuffer = 0.0 if showSummaryPlot: profileHeight = 0.2 profileBottomBuffer = 0.05 profileSideBuffer = 0. profileWidth = 1. / nCols if nCols > 1: profileSideBuffer = 0.1 / (nCols - 1) profileWidth = 0.9 / nCols dataSummary = [] annos = [] fig = go.Figure() fig['layout'].update(title=plotTitle) xAxisN = 1 yAxisN = 1 # Summary plots at the top (if appropriate) if showSummaryPlot: yMinLocal = np.inf yMaxLocal = -np.inf for i in range(nCols): xanchor = 'x{}'.format(xAxisN) yanchor = 'y{}'.format(yAxisN) xBase = i * (profileSideBuffer + profileWidth) yBase = 1 - profileHeight xDomain = [xBase, xBase + profileWidth] yDomain = [yBase, 1.0] for j in range(nRows): if perGroup: mat = hm.matrix.get_matrix(i, j) xTicks, xTicksLabels = hm.getTicks(i) label = mat['sample'] else: mat = hm.matrix.get_matrix(j, i) xTicks, xTicksLabels = hm.getTicks(j) label = mat['group'] if j == 0: fig['layout']['xaxis{}'.format(xAxisN)] = dict(domain=xDomain, anchor=yanchor, range=[0, mat['matrix'].shape[1]], tickmode='array', tickvals=xTicks, ticktext=xTicksLabels, tickangle=label_rotation) fig['layout']['yaxis{}'.format(yAxisN)] = dict(anchor=xanchor, domain=yDomain) trace = plotly_single(mat['matrix'], averageType, colorList[j], label)[0] trace.update(xaxis=xanchor, yaxis=yanchor, legendgroup=label) if min(trace['y']) < yMinLocal: yMinLocal = min(trace['y']) if max(trace['y']) > yMaxLocal: yMaxLocal = max(trace['y']) if i == 0: trace.update(showlegend=True) dataSummary.append(trace) # Add the column label if perGroup: title = hm.matrix.group_labels[i] else: title = hm.matrix.sample_labels[i] titleX = xBase + 0.5 * profileWidth annos.append({'yanchor': 'bottom', 'xref': 'paper', 'xanchor': 'center', 'yref': 'paper', 'text': title, 'y': 1.0, 'x': titleX, 'font': {'size': 16}, 'showarrow': False}) xAxisN += 1 yAxisN += 1 # Adjust y-bounds as appropriate: for i in range(1, yAxisN): yMinUse = yMinLocal if yMin[(i - 1) % len(yMin)] is not None: yMinUse = yMin[(i - 1) % len(yMin)] yMaxUse = yMaxLocal if yMax[(i - 1) % len(yMax)] is not None: yMaxUse = yMax[(i - 1) % len(yMax)] fig['layout']['yaxis{}'.format(i)].update(range=[yMinUse, yMaxUse]) fig['layout']['yaxis1'].update(title=yAxisLabel) # Add the heatmap dataHeatmap = [] zMinLocal = np.inf zMaxLocal = -np.inf heatmapWidth = 1. / nCols heatmapSideBuffer = 0.0 if nCols > 1: heatmapWidth = .9 / nCols heatmapSideBuffer = 0.1 / (nCols - 1) heatmapHeight = 1.0 - profileHeight - profileBottomBuffer for i in range(nCols): xanchor = 'x{}'.format(xAxisN) xBase = i * (heatmapSideBuffer + heatmapWidth) # Determine the height of each heatmap, they have no buffer lengths = [0.0] for j in range(nRows): if perGroup: mat = hm.matrix.get_matrix(i, j) else: mat = hm.matrix.get_matrix(j, i) lengths.append(mat['matrix'].shape[0]) fractionalHeights = heatmapHeight * np.cumsum(lengths).astype(float) / np.sum(lengths).astype(float) xDomain = [xBase, xBase + heatmapWidth] fig['layout']['xaxis{}'.format(xAxisN)] = dict(domain=xDomain, anchor='free', position=0.0, range=[0, mat['matrix'].shape[1]], tickmode='array', tickvals=xTicks, ticktext=xTicksLabels, title=xAxisLabel) # Start adding the heatmaps for j in range(nRows): if perGroup: mat = hm.matrix.get_matrix(i, j) label = mat['sample'] start = hm.matrix.group_boundaries[i] end = hm.matrix.group_boundaries[i + 1] else: mat = hm.matrix.get_matrix(j, i) label = mat['group'] start = hm.matrix.group_boundaries[j] end = hm.matrix.group_boundaries[j + 1] regs = hm.matrix.regions[start:end] regs = [x[2] for x in regs] yanchor = 'y{}'.format(yAxisN) yDomain = [heatmapHeight - fractionalHeights[j + 1], heatmapHeight - fractionalHeights[j]] visible = False if i == 0: visible = True fig['layout']['yaxis{}'.format(yAxisN)] = dict(domain=yDomain, anchor=xanchor, visible=visible, title=label, tickmode='array', tickvals=[], ticktext=[]) if np.min(mat['matrix']) < zMinLocal: zMinLocal = np.min(mat['matrix']) if np.max(mat['matrix']) < zMaxLocal: zMaxLocal = np.max(mat['matrix']) trace = go.Heatmap(z=np.flipud(mat['matrix']), y=regs[::-1], xaxis=xanchor, yaxis=yanchor, showlegend=False, name=label, showscale=False) dataHeatmap.append(trace) yAxisN += 1 xAxisN += 1 if showColorbar: dataHeatmap[-1].update(showscale=True) dataHeatmap[-1]['colorbar'].update(len=heatmapHeight, y=0, yanchor='bottom', ypad=0.0) # Adjust z bounds and colorscale for trace in dataHeatmap: zMinUse = zMinLocal zMaxUse = zMaxLocal if zMin[0] is not None: zMinUse = zMin[0] if zMax[0] is not None: zMaxUse = zMax[0] trace.update(zmin=zMinUse, zmax=zMaxUse, colorscale=convertCmap(cmap[0], vmin=zMinUse, vmax=zMaxUse)) dataSummary.extend(dataHeatmap) fig['data'] = dataSummary fig['layout']['annotations'] = annos py.plot(fig, filename=outFilename, auto_open=False)
'Unrecovered': 'sum' }).reset_index() data_bubblechart = [ go.Scatter(x=bubble_df['Recovered'], y=bubble_df['Unrecovered'], text=bubble_df['Country'], mode='markers', marker=dict(size=bubble_df['Confirmed'] / 200, color=bubble_df['Confirmed'] / 200, showscale=True)) ] # Heatmap data_heatmap = [ go.Heatmap(x=df2['Day'], y=df2['WeekofMonth'], z=df2['Recovered'].values.tolist(), colorscale='Jet') ] # Layout app.layout = html.Div(children=[ html.H1(children='Python Dash', style={ 'textAlign': 'center', 'color': '#ef3e18' }), html.Div('Web dashboard for Data Visualization using Python', style={'textAlign': 'center'}), html.Div('Coronavirus COVID-19 Global Cases - 1/22/2020 to 3/17/2020', style={'textAlign': 'center'}), html.Br(),
#Scatter Plot import plotly.express as px fig = px.scatter(drug_info_dataframe.query("year==2018"), x="state", y="count", size="year", color="city", hover_name="year",size_max=20,title='State Wise Drug Death in USA 2018') fig.show() # Heat Map import numpy as np import plotly.graph_objects as go fig = go.Figure(data=go.Heatmap( z=np.array(drug_info_dataframe_2['avg_drug_score']), x=np.array(drug_info_dataframe_2['age_bin']), y=np.array(drug_info_dataframe_2['state']), hoverongaps = False)) fig.show() # Interactive Visulaization import numpy as np import pandas as pd import plotly.graph_objects as go from ipywidgets import widgets year = widgets.IntSlider( value=1, min=2012,
def heatmap(df, colname, plot_title='default', resolution='day', aggfunc='mean', zmin='default', zmax='default', zsmooth=None, reversescale=False, colorscale='RdBu', colorbar_title='Units', height=600, width=1000, plot=True, asFigure=False, layoutupdate=False): '''take df, x, y, z (optional) columns as integers and return a plotly heatmap''' if type(df) == pd.core.series.Series: df = pd.DataFrame(df) x, plt_title = colname_to_int(df, colname) dfpivot = pd.DataFrame(df.loc[:, plt_title]) dfpivot['hour'] = dfpivot.index.hour dfpivot['day'] = dfpivot.index.map(lambda x: x.strftime('%b-%d')) dfpivot['week'] = dfpivot.index.week dfpivot['month'] = dfpivot.index.month dfpivot['dayofyear'] = dfpivot.index.dayofyear dfpivot.columns = [''.join(x) for x in dfpivot.columns] dfpivot = dfpivot.pivot_table( values=dfpivot.columns[0], index='hour', columns=['dayofyear', 'week', 'day', 'month'], aggfunc=aggfunc) trace = go.Heatmap(x=[x[2] for x in dfpivot.columns], y=dfpivot.index.values, z=dfpivot.values, zsmooth=zsmooth, reversescale=reversescale, colorscale=colorscale, colorbar=dict(title=colorbar_title)) if zmin != 'default': if zmax == 'default': raise ValueError( "Error: If entering zmin or zmax, BOTH must be specified.") trace.update(zmin=zmin) trace.update(zmax=zmax) if plot_title == 'default': title = str(plt_title) + " Annual Heatmap" else: title = plot_title layout = dict(title=title, height=height, width=width) data = [trace] fig = dict(data=data, layout=layout) fig['layout'].update({ 'font': { 'family': 'Futura LT BT, monospace', 'size': 14 }, 'title': title, 'titlefont': { 'size': 24 }, 'xaxis': { 'title': 'Day of Year' }, 'yaxis': { 'title': 'Hour of Day' } }) if layoutupdate: layout.update(layoutupdate) if lv(plotly.__version__) >= lv('3.5'): fig['data'][0]['colorbar']['title'].update({'side': 'right'}) else: fig['data'][0]['colorbar'].update({'titleside': 'right'}) if plot: py.iplot(fig, config=config) if asFigure: return fig
} y = [] x = [] sPi = '0' + str(pi) sPi = sPi.replace('.', '') # create plot StartTime = datetime.now() print(StartTime, '\t\tBegin Plotting PI()') for index in range(0, len(sPi)): # Build the plot series # print(sPi[index]) if index == 0: x.append(0) y.append(0) else: x.append(float(xDict[int(sPi[index])]) + x[index - 1]) y.append(float(yDict[int(sPi[index])]) + y[index - 1]) EndTime = datetime.now() print(EndTime, '\t\tPI() Plot Completed') print(EndTime - StartTime, '\t\tTotal Plot Time\r\n') answer = input('Plot? ') answer = answer.lower() if answer.startswith('y'): # Plot the image trace1 = go.Heatmap([(x), (y)]) data = [trace] py.iplot(data)
####### # Heatmap of temperatures for Yuma, Arizona ###### import plotly.offline as pyo import plotly.graph_objs as go import pandas as pd df = pd.read_csv('../data/2010YumaAZ.csv') data = [ go.Heatmap(x=df['DAY'], y=df['LST_TIME'], z=df['T_HR_AVG'].values.tolist(), colorscale='Jet') ] layout = go.Layout(title='Hourly Temperatures, June 1-7, 2010 in<br>\ Yuma, AZ USA') fig = go.Figure(data=data, layout=layout) pyo.plot(fig, filename='Yuma.html')