def create_trace(settings): # prepare the hover text to display if the additional combobox is empty or not # this setting is necessary to overwrite the standard hovering labels if not settings.additional_hover_text: text = [ settings.properties['x_name'] + ': {}'.format(settings.x[k]) + '<br>{}: {}'.format( settings.properties['y_name'], settings.y[k]) + '<br>{}: {}'.format(settings.properties['z_name'], settings.z[k]) for k in range(len(settings.x)) ] else: text = [ settings.properties['x_name'] + ': {}'.format(settings.x[k]) + '<br>{}: {}'.format( settings.properties['y_name'], settings.y[k]) + '<br>{}: {}'.format(settings.properties['z_name'], settings.z[k]) + '<br>{}'.format(settings.additional_hover_text[k]) for k in range(len(settings.x)) ] return [ graph_objs.Scatterternary( a=settings.x, b=settings.y, c=settings.z, name='{} + {} + {}'.format(settings.properties['x_name'], settings.properties['y_name'], settings.properties['z_name']), hoverinfo='text', text=text, mode='markers', marker=dict( color=settings.data_defined_colors if settings.data_defined_colors else settings.properties['in_color'], colorscale=settings.properties['color_scale'], showscale=settings.properties['show_colorscale_legend'], reversescale=settings.properties['invert_color_scale'], colorbar=dict(len=0.8), size=settings.data_defined_marker_sizes if settings.data_defined_marker_sizes else settings.properties['marker_size'], symbol=settings.properties['marker_symbol'], line=dict(color=settings.data_defined_stroke_colors if settings.data_defined_stroke_colors else settings.properties['out_color'], width=settings.data_defined_stroke_widths if settings.data_defined_stroke_widths else settings.properties['marker_width'])), opacity=settings.properties['opacity']) ]
def buildTrace(self): ''' build the final trace calling the go.xxx plotly method this method here is the one performing the real job From the initial object created (e.g. p = Plot(plot_type, plot_properties, layout_properties)) this methods checks the plot_type and elaborates the plot_properties dictionary passed Console usage: # create the initial object p = Plot(plot_type, plot_properties, layout_properties) # call the method p.buildTrace() Returns the final Plot Trace (final Plot object, AKA go.xxx plot type) ''' if self.plot_type == 'scatter': self.trace = [go.Scatter( x=self.plot_properties['x'], y=self.plot_properties['y'], mode=self.plot_properties['marker'], name=self.plot_properties['name'], ids=self.plot_properties['featureIds'], customdata=self.plot_properties['custom'], text=self.plot_properties['additional_hover_text'], hoverinfo=self.plot_properties['hover_text'], marker=dict( color=self.plot_properties['in_color'], colorscale=self.plot_properties['colorscale_in'], showscale=self.plot_properties['show_colorscale_legend'], reversescale=self.plot_properties['invert_color_scale'], colorbar=dict( len=0.8 ), size=self.plot_properties['marker_size'], symbol=self.plot_properties['marker_symbol'], line=dict( color=self.plot_properties['out_color'], width=self.plot_properties['marker_width'] ) ), line=dict( width=self.plot_properties['marker_width'], dash=self.plot_properties['line_dash'] ), opacity=self.plot_properties['opacity'] )] elif self.plot_type == 'box': # flip the variables according to the box orientation if self.plot_properties['box_orientation'] == 'h': self.plot_properties['x'], self.plot_properties['y'] = self.plot_properties['y'], self.plot_properties['x'] self.trace = [go.Box( x=self.plot_properties['x'], y=self.plot_properties['y'], name=self.plot_properties['name'], customdata=self.plot_properties['custom'], boxmean=self.plot_properties['box_stat'], orientation=self.plot_properties['box_orientation'], boxpoints=self.plot_properties['box_outliers'], fillcolor=self.plot_properties['in_color'], line=dict( color=self.plot_properties['out_color'], width=self.plot_properties['marker_width'] ), opacity=self.plot_properties['opacity'] )] elif self.plot_type == 'bar': if self.plot_properties['box_orientation'] == 'h': self.plot_properties['x'], self.plot_properties['y'] = self.plot_properties['y'], self.plot_properties['x'] self.trace = [go.Bar( x=self.plot_properties['x'], y=self.plot_properties['y'], name=self.plot_properties['name'], ids=self.plot_properties['featureBox'], customdata=self.plot_properties['custom'], orientation=self.plot_properties['box_orientation'], marker=dict( color=self.plot_properties['in_color'], colorscale=self.plot_properties['colorscale_in'], showscale=self.plot_properties['show_colorscale_legend'], reversescale=self.plot_properties['invert_color_scale'], colorbar=dict( len=0.8 ), line=dict( color=self.plot_properties['out_color'], width=self.plot_properties['marker_width'] ) ), opacity=self.plot_properties['opacity'] )] elif self.plot_type == 'histogram': self.trace = [go.Histogram( x=self.plot_properties['x'], y=self.plot_properties['x'], name=self.plot_properties['name'], orientation=self.plot_properties['box_orientation'], nbinsx=self.plot_properties['bins'], nbinsy=self.plot_properties['bins'], marker=dict( color=self.plot_properties['in_color'], line=dict( color=self.plot_properties['out_color'], width=self.plot_properties['marker_width'] ) ), histnorm=self.plot_properties['normalization'], opacity=self.plot_properties['opacity'], cumulative=dict( enabled=self.plot_properties['cumulative'], direction=self.plot_properties['invert_hist'] ) )] elif self.plot_type == 'pie': self.trace = [go.Pie( labels=self.plot_properties['x'], values=self.plot_properties['y'], name=self.plot_properties['custom'][0], )] elif self.plot_type == '2dhistogram': self.trace = [go.Histogram2d( x=self.plot_properties['x'], y=self.plot_properties['y'], colorscale=self.plot_properties['color_scale'] )] elif self.plot_type == 'polar': self.trace = [go.Scatterpolar( r=self.plot_properties['y'], theta=self.plot_properties['x'], mode=self.plot_properties['marker'], name=self.plot_properties['y_name'], marker=dict( color=self.plot_properties['in_color'], size=self.plot_properties['marker_size'], symbol=self.plot_properties['marker_symbol'], line=dict( color=self.plot_properties['out_color'], width=self.plot_properties['marker_width'] ) ), line=dict( color=self.plot_properties['in_color'], width=self.plot_properties['marker_width'], dash=self.plot_properties['line_dash'] ), opacity=self.plot_properties['opacity'], )] elif self.plot_type == 'ternary': # prepare the hover text to display if the additional combobox is empty or not # this setting is necessary to overwrite the standard hovering labels if self.plot_properties['additional_hover_text'] == []: text = [self.plot_properties['x_name'] + ': {}'.format(self.plot_properties['x'][k]) + '<br>{}: {}'.format(self.plot_properties['y_name'], self.plot_properties['y'][k]) + '<br>{}: {}'.format(self.plot_properties['z_name'], self.plot_properties['z'][k]) for k in range(len(self.plot_properties['x']))] else: text = [self.plot_properties['x_name'] + ': {}'.format(self.plot_properties['x'][k]) + '<br>{}: {}'.format(self.plot_properties['y_name'], self.plot_properties['y'][k]) + '<br>{}: {}'.format(self.plot_properties['z_name'], self.plot_properties['z'][k]) + '<br>{}'.format(self.plot_properties['additional_hover_text'][k]) for k in range(len(self.plot_properties['x']))] self.trace = [go.Scatterternary( a=self.plot_properties['x'], b=self.plot_properties['y'], c=self.plot_properties['z'], name=self.plot_properties['x_name'] + ' + ' + self.plot_properties['y_name'] + ' + ' + self.plot_properties['z_name'], hoverinfo='text', text=text, mode='markers', marker=dict( color=self.plot_properties['in_color'], colorscale=self.plot_properties['colorscale_in'], showscale=self.plot_properties['show_colorscale_legend'], reversescale=self.plot_properties['invert_color_scale'], colorbar=dict( len=0.8 ), size=self.plot_properties['marker_size'], symbol=self.plot_properties['marker_symbol'], line=dict( color=self.plot_properties['out_color'], width=self.plot_properties['marker_width'] ) ), opacity=self.plot_properties['opacity'] )] elif self.plot_type == 'contour': self.trace = [go.Contour( z=[self.plot_properties['x'], self.plot_properties['y']], contours=dict( coloring=self.plot_properties['cont_type'], showlines=self.plot_properties['show_lines'] ), colorscale=self.plot_properties['color_scale'], opacity=self.plot_properties['opacity'] )] elif self.plot_type == 'violin': # flip the variables according to the box orientation if self.plot_properties['box_orientation'] == 'h': self.plot_properties['x'], self.plot_properties['y'] = self.plot_properties['y'], self.plot_properties['x'] self.trace = [go.Violin( x=self.plot_properties['x'], y=self.plot_properties['y'], name=self.plot_properties['name'], customdata=self.plot_properties['custom'], orientation=self.plot_properties['box_orientation'], points=self.plot_properties['box_outliers'], fillcolor=self.plot_properties['in_color'], line=dict( color=self.plot_properties['out_color'], width=self.plot_properties['marker_width'] ), opacity=self.plot_properties['opacity'], meanline=dict( visible=self.plot_properties['show_mean_line'] ), side=self.plot_properties['violin_side'] )] return self.trace
def ternary_plot(plot_data): """ Return a ternary phase diagram in a two-dimensional plot. Args: plot_data: plot data from PDPlotter Returns: go.Figure """ go.Scatterternary({ "mode": "markers", "a": list_of_a_comp, "b":..., "c":..., "text":..., "marker": { "symbol": 100, "color":..., "size":..., "line": { "width": 2 }, }, }) go.Scatterternary({ "mode": "lines", "a":..., "b":..., "c":..., "line":... }) go.Layout({ "title": "Ternary Scatter Plot", "ternary": { "sum": 1, "aaxis": { "title": "X", "min": 0.01, "linewidth": 2, "ticks": "outside", }, "baxis": { "title": "W", "min": 0.01, "linewidth": 2, "ticks": "outside", }, "caxis": { "title": "S", "min": 0.01, "linewidth": 2, "ticks": "outside", }, }, "showlegend": False, }) return go.Figure()
def test_get_subplot(self): # Make Figure with subplot types fig = subplots.make_subplots( rows=4, cols=2, specs=[ [{}, {"secondary_y": True}], [{"type": "polar"}, {"type": "ternary"}], [{"type": "scene"}, {"type": "geo"}], [{"type": "domain", "colspan": 2}, None], ], ) fig.add_scatter(y=[2, 1, 3], row=1, col=1) fig.add_scatter(y=[2, 1, 3], row=1, col=2) fig.add_scatter(y=[1, 3, 2], row=1, col=2, secondary_y=True) fig.add_trace(go.Scatterpolar(r=[2, 1, 3], theta=[20, 50, 125]), row=2, col=1) fig.add_traces( [go.Scatterternary(a=[0.2, 0.1, 0.3], b=[0.4, 0.6, 0.5])], rows=[2], cols=[2], ) fig.add_scatter3d( x=[2, 0, 1], y=[0, 1, 0], z=[0, 1, 2], mode="lines", row=3, col=1 ) fig.add_scattergeo(lat=[0, 40], lon=[10, 5], mode="lines", row=3, col=2) fig.add_parcats( dimensions=[ {"values": ["A", "A", "B", "A", "B"]}, {"values": ["a", "a", "a", "b", "b"]}, ], row=4, col=1, ) fig.update_traces(uid=None) fig.update(layout_height=1200) # Check expected = Figure( { "data": [ {"type": "scatter", "xaxis": "x", "y": [2, 1, 3], "yaxis": "y"}, {"type": "scatter", "xaxis": "x2", "y": [2, 1, 3], "yaxis": "y2"}, {"type": "scatter", "xaxis": "x2", "y": [1, 3, 2], "yaxis": "y3"}, { "r": [2, 1, 3], "subplot": "polar", "theta": [20, 50, 125], "type": "scatterpolar", }, { "a": [0.2, 0.1, 0.3], "b": [0.4, 0.6, 0.5], "subplot": "ternary", "type": "scatterternary", }, { "mode": "lines", "scene": "scene", "type": "scatter3d", "x": [2, 0, 1], "y": [0, 1, 0], "z": [0, 1, 2], }, { "geo": "geo", "lat": [0, 40], "lon": [10, 5], "mode": "lines", "type": "scattergeo", }, { "dimensions": [ {"values": ["A", "A", "B", "A", "B"]}, {"values": ["a", "a", "a", "b", "b"]}, ], "domain": {"x": [0.0, 0.9400000000000001], "y": [0.0, 0.19375]}, "type": "parcats", }, ], "layout": { "geo": { "domain": { "x": [0.5700000000000001, 0.9400000000000001], "y": [0.26875, 0.4625], } }, "height": 1200, "polar": {"domain": {"x": [0.0, 0.37], "y": [0.5375, 0.73125]}}, "scene": {"domain": {"x": [0.0, 0.37], "y": [0.26875, 0.4625]}}, "ternary": { "domain": { "x": [0.5700000000000001, 0.9400000000000001], "y": [0.5375, 0.73125], } }, "xaxis": {"anchor": "y", "domain": [0.0, 0.37]}, "xaxis2": { "anchor": "y2", "domain": [0.5700000000000001, 0.9400000000000001], }, "yaxis": {"anchor": "x", "domain": [0.80625, 1.0]}, "yaxis2": {"anchor": "x2", "domain": [0.80625, 1.0]}, "yaxis3": {"anchor": "x2", "overlaying": "y2", "side": "right"}, }, } ) expected.update_traces(uid=None) # Make sure we have expected starting figure self.assertEqual(fig, expected) # (1, 1) subplot = fig.get_subplot(1, 1) self.assertEqual( subplot, SubplotXY(xaxis=fig.layout.xaxis, yaxis=fig.layout.yaxis) ) # (1, 2) Primary subplot = fig.get_subplot(1, 2) self.assertEqual( subplot, SubplotXY(xaxis=fig.layout.xaxis2, yaxis=fig.layout.yaxis2) ) # (1, 2) Primary subplot = fig.get_subplot(1, 2, secondary_y=True) self.assertEqual( subplot, SubplotXY(xaxis=fig.layout.xaxis2, yaxis=fig.layout.yaxis3) ) # (2, 1) subplot = fig.get_subplot(2, 1) self.assertEqual(subplot, fig.layout.polar) # (2, 2) subplot = fig.get_subplot(2, 2) self.assertEqual(subplot, fig.layout.ternary) # (3, 1) subplot = fig.get_subplot(3, 1) self.assertEqual(subplot, fig.layout.scene) # (3, 2) subplot = fig.get_subplot(3, 2) self.assertEqual(subplot, fig.layout.geo) # (4, 1) subplot = fig.get_subplot(4, 1) domain = fig.data[-1].domain self.assertEqual(subplot, SubplotDomain(x=domain.x, y=domain.y))
def get_duval(df_ratio): trace0 = go.Scatterternary(a=[98, 1, 98], b=[0, 0, 2], c=[2, 0, 0], fill="toself", fillcolor="#8dd3c7", line=dict(color="#444"), mode="lines", text='PD Partial Discharge', name='Partial Discharge', hoverinfo='text') trace1 = go.Scatterternary(a=[0, 0, 64, 87], b=[1, 77, 13, 13], c=[0, 23, 23, 0], fill="toself", fillcolor="#ffffb3", line=dict(color="#444"), mode="lines", text='D1 Discharge of Low Energy', name='Thermal Fault T > 700°C', hoverinfo='text') trace2 = go.Scatterternary(a=[0, 0, 31, 47, 64], b=[77, 29, 29, 13, 13], c=[23, 71, 40, 40, 23], fill="toself", fillcolor="#bebada", line=dict(color="#444"), mode="lines", text='D2 Discharge of High Energy', name='Discharge of High Energy', hoverinfo='text') trace3 = go.Scatterternary(a=[0, 0, 35, 46, 96, 87, 47, 31], b=[29, 15, 15, 4, 4, 13, 13, 29], c=[71, 85, 50, 50, 0, 0, 40, 40], fill="toself", fillcolor="#fb8072", line=dict(color="#444"), mode="lines", text='DT Electrical and Thermal Fault', name='Electrical and Thermal Fault', hoverinfo='text') trace4 = go.Scatterternary(a=[76, 80, 98, 98, 96], b=[4, 0, 0, 2, 4], c=[20, 20, 2, 0, 0], fill="toself", fillcolor="#80b1d3", line=dict(color="#444"), mode="lines", text='T1 Thermal Fault < 300°C', name='Thermal Fault < 300°C', hoverinfo='text') trace5 = go.Scatterternary(a=[46, 50, 80, 76], b=[4, 0, 0, 4], c=[50, 50, 20, 20], fill="toself", fillcolor="#fdb462", line=dict(color="#444"), mode="lines", text='T2 Thermal Fault 300 < T < 700°C', name='Thermal Fault 300 < T < 700°C', hoverinfo='text') trace6 = go.Scatterternary(a=[0, 0, 50, 35], b=[15, 0, 0, 15], c=[85, 1, 50, 50], fill="toself", fillcolor="#b3de69", line=dict(color="#444"), mode="lines", text='T3 Thermal Fault > 700°C', name='Thermal Fault > 700°C', hoverinfo='text') f1 = df_ratio.iloc[:, 0].copy() f2 = df_ratio.iloc[:, 1].copy() f3 = df_ratio.iloc[:, 2].copy() text = [ f1[k:k + 1].index[0].strftime("%d-%b-%Y %H:%M") + '<br>CH4: ' + '{}'.format(f1[k]) + '<br>C2H2: ' + '{}'.format(f2[k]) + '<br>C2H4: ' + '{}'.format(f3[k]) for k in range(len(f1)) ] f1 = f1.apply(lambda x: x + 0.01 if x < 0.1 else x) f2 = f2.apply(lambda x: x + 0.01 if x < 0.1 else x) f3 = f3.apply(lambda x: x + 0.01 if x < 0.1 else x) trace7 = go.Scatterternary(a=f1, b=f2, c=f3, hoverinfo='text', text=text, marker=dict(color="red", size=8, symbol=28), mode="markers", name='Data Points') data = [trace0, trace1, trace2, trace3, trace4, trace5, trace6, trace7] layout = go.Layout(autosize=True, showlegend=True, legend=dict(x=0.9, y=1, font=dict(size=8)), ternary=dict( aaxis=dict(linewidth=2, min=0.01, ticks="outside", ticksuffix="%", title="CH4(Methane)", titlefont=dict(color='#0e4886')), baxis=dict(linewidth=2, min=0.01, ticks="outside", ticksuffix="%", title="C2H2(Acetylene)", titlefont=dict(color='#0e4886')), caxis=dict(linewidth=2, min=0.01, ticks="outside", ticksuffix="%", title="C2H4(Ethylene)", titlefont=dict(color='#0e4886')), sum=100), margin=dict(l=0, r=0, t=55, b=35), font=dict(size=10), titlefont=dict(size=14, color='#0e4886')) fig_tri = go.Figure(data=data, layout=layout) return fig_tri
def processAlgorithm(self, parameters, context, feedback): try: import pandas as pd import numpy as np except Exception as e: feedback.reportError( QCoreApplication.translate('Error', '%s' % (e))) feedback.reportError(QCoreApplication.translate('Error', ' ')) feedback.reportError( QCoreApplication.translate( 'Error', 'Error loading modules - please install the necessary dependencies' )) return {} plot = parameters[self.Plot] try: import plotly.graph_objs as go import chart_studio.plotly as py from plotly.subplots import make_subplots except Exception: feedback.reportError( QCoreApplication.translate( 'Error', 'Plotting will be disabled as plotly module did not load - please install the necessary dependencies.' )) plot = False Nodes = self.parameterAsSource(parameters, self.Nodes, context) Branches = self.parameterAsSource(parameters, self.Branches, context) SA = self.parameterAsSource(parameters, self.Sample_Area, context) warnings.simplefilter(action='ignore', category=FutureWarning) feedback.pushInfo( QCoreApplication.translate('TopologyParameters', 'Reading Data')) samples = [] stotal = SA.featureCount() feedback.pushInfo( QCoreApplication.translate('TopologyParameters', '%s Grid Samples' % (stotal))) stotal = 100.0 / stotal for enum, feature in enumerate(SA.getFeatures(QgsFeatureRequest())): if stotal != -1: feedback.setProgress(int(enum * stotal)) samples.append(feature['Sample_No_']) SN = [] CLASS = [] total = Nodes.featureCount() feedback.pushInfo( QCoreApplication.translate('TopologyParameters', '%s Nodes' % (total))) total = 100.0 / total for enum, feature in enumerate(Nodes.getFeatures(QgsFeatureRequest())): if total != -1: feedback.setProgress(int(enum * total)) v = feature['Sample_No_'] if v in samples: SN.append(v) CLASS.append(feature['Class']) df = pd.DataFrame({'Sample No.': SN, 'Class': CLASS}) df['Nodes'] = 0 df = df[['Sample No.', 'Class', 'Nodes']].groupby(['Sample No.', 'Class']).count().unstack(level=1) df.fillna(0, inplace=True) df.columns = df.columns.droplevel() node_columns = ['I', 'X', 'Y', 'E', 'U'] for column in node_columns: if column not in df: df[column] = 0.0 if 'Error' in df: del df['Error'] df['No. Nodes'] = df.X + df.Y + df.I df['No. Branches'] = ((df.X * 4.0) + (df.Y * 3.0) + df.I) / 2.0 df['No. Lines'] = (df.Y + df.I) / 2 df['No. Connections'] = df.X + df.Y df['Connect/L'] = (2.0 * (df.X + df.Y)) / df['No. Lines'] SN = [] B = [] CON = [] LEN = [] total = Branches.featureCount() feedback.pushInfo( QCoreApplication.translate('TopologyParameters', '%s Branches' % (total))) total = 100.0 / total for enum, feature in enumerate( Branches.getFeatures(QgsFeatureRequest())): if total != -1: feedback.setProgress(int(enum * total)) v = feature['Sample_No_'] if v in samples: SN.append(v) B.append(feature['Weight']) CON.append(feature['Connection']) LEN.append(feature.geometry().length()) df2 = pd.DataFrame({ 'Sample No.': SN, 'Branches': B, 'Connection': CON, 'Length': LEN }) df3 = df2[['Sample No.', 'Branches', 'Connection']].groupby(['Sample No.', 'Connection' ]).sum().unstack(level=1) df3.fillna(0.0, inplace=True) df3.columns = df3.columns.droplevel() branch_columns = ['C - C', 'C - I', 'C - U', 'I - I', 'I - U', 'U - U'] delete_columns = [ 'C - Error', 'Error - Error', 'Error - I', 'Error - U' ] for column in branch_columns: if column not in df3: df3[column] = 0.0 for column in delete_columns: if column in df3: del df3[column] df2 = df2[['Sample No.', 'Length', 'Connection']].groupby(['Sample No.', 'Connection' ]).sum().unstack(level=1) df2.fillna(0.0, inplace=True) df2.columns = df2.columns.droplevel() for column in branch_columns: if column not in df2: df2[column] = 0.0 for column in delete_columns: if column in df2: del df2[column] check = SA.fields().indexFromName('Radius') SN = [] CIRC = [] AREA = [] for feature in SA.getFeatures(QgsFeatureRequest()): SN.append(feature['Sample_No_']) if check == -1: CIRC.append(feature.geometry().length()) AREA.append(feature.geometry().area()) else: CIRC.append(feature['Circ']) AREA.append(feature['Area']) df4 = pd.DataFrame({ 'Sample No.': SN, 'Circumference': CIRC, 'Area': AREA }) df4.set_index('Sample No.', inplace=True) df3['Total Trace Length'] = df2['C - C'] + df2['C - I'] + df2[ 'I - I'] + df2['C - U'] + df2['I - U'] + df2['U - U'] df['Average Line Length'] = df3['Total Trace Length'] / df['No. Lines'] df['Average Branch Length'] = df3['Total Trace Length'] / df[ 'No. Branches'] df['Connect/B'] = ((3.0 * df.Y) + (4.0 * df.X)) / df['No. Branches'] df['Branch Freq'] = df['No. Branches'] / df4['Area'] df['Line Freq'] = df['No. Lines'] / df4['Area'] df['NcFreq'] = df['No. Connections'] / df4['Area'] samples = df.index.tolist() df4 = df4.loc[samples] r = df4['Circumference'] / (np.pi * 2.0) a = np.pi * r * r a = df4['Area'] - a df['a'] = np.fabs(a.round(4)) df['1D Intensity'] = 0.0 df.loc[df['a'] == 0.0, '1D Intensity'] = (df['E'] / (2.0 * np.pi * r)) * (np.pi / 2.0) del df['a'] df['2D Intensity'] = df3['Total Trace Length'] / df4['Area'] df['Dimensionless Intensity'] = df['2D Intensity'] * df[ 'Average Branch Length'] df = pd.concat([df4, df, df3], axis=1) df = df[np.isfinite(df['No. Nodes'])] df.replace(np.inf, 0.0, inplace=True) df.replace(np.nan, 0.0, inplace=True) df = df.round(5) fs = QgsFields() fs.append(QgsField('Sample_No_', QVariant.Int)) field_check = SA.fields().indexFromName('Radius') if field_check != -1: fs.append(QgsField('Radius', QVariant.Double)) fs.append(QgsField('Rotation', QVariant.Double)) fs.append(QgsField('Spacing', QVariant.Double)) count = 0 for c in df: fs.append(QgsField(c, QVariant.Double)) count += 1 (writer, dest_id) = self.parameterAsSink(parameters, self.TP, context, fs, QgsWkbTypes.Polygon, Nodes.sourceCrs()) feedback.pushInfo( QCoreApplication.translate('TopologyParametersOutput', 'Creating Output')) fet = QgsFeature() for enum, feature in enumerate(SA.getFeatures(QgsFeatureRequest())): if stotal != -1: feedback.setProgress(int(enum * stotal)) ID = feature['Sample_No_'] fet.setGeometry(feature.geometry()) rows = [ID] if field_check != -1: rows.append(feature['Radius']) rows.append(feature['Rotation']) rows.append(feature['Spacing']) rows.append(feature['Circ']) rows.append(feature['Area']) else: rows.append(feature.geometry().length()) rows.append(feature.geometry().area()) if ID in samples: try: rows.extend(df.loc[ID].tolist()[2:]) fet.setAttributes(rows) writer.addFeature(fet, QgsFeatureSink.FastInsert) except Exception: feedback.reportError( QCoreApplication.translate( 'Error', 'Could not find Sample No %s - skipping' % (ID))) continue else: rows.extend([0] * (count - 2)) fet.setAttributes(rows) writer.addFeature(fet, QgsFeatureSink.FastInsert) if plot: ID = ['Sample No. %s' % (s) for s in samples] p1s = [(0, 0.75, 0.25), (0, 0.66666, 0.33333), (0, 0.562500, 0.437500), (0, 0.429, 0.571), (0, 0.2, 0.8)] p2s = [(0.2, 0.8, 0), (0.273, 0.727, 0), (0.368, 0.632, 0), (0.5, 0.5, 0), (0.692, 0.308, 0)] text = [1.0, 1.2, 1.4, 1.6, 1.8] fig = make_subplots(rows=1, cols=2, specs=[[{ "type": "ternary" }, { "type": "ternary" }]]) fig.add_trace(go.Scatterternary(a=df['I'], b=df['Y'], c=df['X'], mode='markers', name='I + Y + X', text=ID, marker=dict(size=15)), row=1, col=1) for p1, p2, t in zip(p1s, p2s, text): fig.add_trace(go.Scatterternary(a=[p1[1], p2[1]], b=[p1[2], p2[2]], c=[p1[0], p2[0]], name=str(t), text=str(t), marker=dict(size=0, color='gray')), row=1, col=1) branchPlot = fig.add_trace(go.Scatterternary( a=df['I - I'], b=df['C - I'], c=df['C - C'], mode='markers', name='I-I + C-I + C-C', text=ID, marker=dict(size=15)), row=1, col=2) p = [(0, 1, 0), (0.01, 0.81, 0.18), (0.04, 0.64, 0.32), (0.09, 0.49, 0.42), (0.16, 0.36, 0.48), (0.25, 0.25, 0.5), (0.36, 0.16, 0.48), (0.49, 0.09, 0.42), (0.64, 0.04, 0.32), (0.81, 0.01, 0.18), (1, 0, 0)] x, y, z = [], [], [] for i in p: x.append(i[0]) y.append(i[1]) z.append(i[2]) fig.add_trace(go.Scatterternary(a=x, b=z, c=y, name='Trend', marker=dict(size=0, color='gray')), row=1, col=2) layout = { 'ternary': dict(sum=100, aaxis=dict( title='I', ticksuffix='%', ), baxis=dict(title='Y', ticksuffix='%'), caxis=dict(title='X', ticksuffix='%'), domain={ 'row': 0, 'column': 0 }), 'ternary2': dict(sum=100, aaxis=dict( title='I', ticksuffix='%', ), baxis=dict(title='C - I', ticksuffix='%'), caxis=dict(title='C - C', ticksuffix='%'), domain={ 'row': 0, 'column': 1 }) } ngtPath = 'https://raw.githubusercontent.com/BjornNyberg/NetworkGT/master/Images/NetworkGT_Logo1.png' layout['images'] = [ dict(source=ngtPath, xref="paper", yref="paper", x=1.0, y=1.0, sizex=0.2, sizey=0.2, xanchor="right", yanchor="bottom") ] fig.update_layout(layout) try: py.plot(fig, filename='Ternary Diagram', auto_open=True) except Exception: fig.show() self.dest_id = dest_id return {self.TP: dest_id}
def Triang5(date_str): # Duval Triangle 5 trace15 = go.Scatterternary(name='Duval Triangle', uid="a1a8b8", a=[0, 46, 36, 0], b=[1, 54, 54, 90], c=[0, 0, 10, 10], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#3dd3c7", text="O", hoverinfo='text') trace16 = go.Scatterternary(name='Duval Triangle1', uid="c72344", a=[46, 85, 75, 36], b=[54, 15, 15, 54], c=[0, 0, 10, 10], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#8dd3c7", text="S", hoverinfo='text') trace17 = go.Scatterternary(name='Duval Triangle2', uid="3eab74", a=[85, 98, 96, 83], b=[15, 2, 2, 15], c=[0, 0, 2, 2], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#bebada", text="PD", hoverinfo='text') trace18 = go.Scatterternary(name='Duval Triangle4', uid="3eab74", a=[83, 96, 98, 1, 90, 75], b=[15, 2, 2, 0, 0, 15], c=[2, 2, 0, 0, 10, 10], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#fb8877", text="DT", hoverinfo='text') trace19 = go.Scatterternary(name='Duval Triangle5', uid="381ad2", a=[0, 60, 35, 0], b=[90, 30, 30, 65], c=[10, 10, 35, 35], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#40b7d3", text="I", hoverinfo='text') trace20 = go.Scatterternary(name='Duval Triangle5', uid="8cc163", a=[78, 90, 65, 53], b=[12, 0, 0, 12], c=[10, 10, 35, 35], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#90d133", text="T2", hoverinfo='text') trace21 = go.Scatterternary(name='Duval Triangle5', uid="6f33dc", a=[0, 35, 0, 16, 36, 38, 53, 65, 0], b=[65, 30, 30, 14, 14, 12, 12, 0, 0], c=[35, 35, 70, 70, 50, 50, 35, 35, 1], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#40f1d3", text="T3", hoverinfo='text') trace22 = go.Scatterternary(name='Duval Triangle5', uid="6f33dc", a=[0, 60, 78, 38, 36, 16], b=[30, 30, 12, 12, 14, 14], c=[70, 10, 10, 50, 50, 70], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#40f1d3", text="T3", hoverinfo='text') trace23 = go.Scatterternary( name='Duval Triangle5', uid="6f33dc", a=[32, 30, 34, 35], # Pasar vector de datos historicos b=[43, 50, 34, 54], # Pasar vector de datos historicos c=[25, 20, 32, 11], # Pasar vector de datos historicos mode="markers+lines", marker={ "size": 10, "color": "black", "symbol": 4 }) hover_value = [ date_str + '<br>CH4: ' + '{}%'.format(trace23['a'][k]) + '<br>C2H6: ' + '{}%'.format(trace23['b'][k]) + '<br>C2H4: ' + '{}%'.format(trace23['c'][k]) for k in range(len(trace23['a'])) ] trace23['text'] = hover_value trace23['hoverinfo'] = 'text' figure = go.Figure() # Duval Triangle 5 figure.add_trace(trace15) figure.add_trace(trace16) figure.add_trace(trace17) figure.add_trace(trace18) figure.add_trace(trace19) figure.add_trace(trace20) figure.add_trace(trace21) figure.add_trace(trace22) figure.add_trace(trace23) # Layout layoutT = go.Layout( ternary={ "sum": 100, "aaxis": { "min": 0.01, "ticks": "outside", "title": "CH4", # "(Methane)", "linewidth": 2, "ticksuffix": "%", "color": '#e6e6e6' }, "baxis": { "min": 0.01, "ticks": "outside", "title": "C2H6", # "(Acetylene)", "linewidth": 2, "ticksuffix": "%", "color": '#e6e6e6' }, "caxis": { "min": 0.01, "ticks": "outside", "title": "C2H4", # "(Ethylene)", "linewidth": 2, "ticksuffix": "%", "color": '#e6e6e6' } }, showlegend=False, plot_bgcolor='#282828', paper_bgcolor='#222222', margin=dict(l=50, r=50, t=50, b=50)) figure['layout'].update(layoutT) figure.update_xaxes(tickfont=dict(color='#e6e6e6')) figure.update_yaxes(tickfont=dict(color='#e6e6e6')) for annotation in figure['layout']['annotations']: annotation['font'] = dict(color='#e6e6e6') return dict(msgTriang5=T('Triangle 5'), figTriang5=figure.to_json())
def fielData1(transformer, date_str): trace1 = go.Scatterternary(name='Duval Triangle', uid="a1a8b8", a=[98, 1, 98], b=[0, 0, 2], c=[2, 0, 0], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#3dd3c7", text="PD", hoverinfo='text') trace2 = go.Scatterternary(name='Duval Triangle1', uid="c72344", a=[0, 0, 64, 87], b=[1, 77, 13, 13], c=[0, 23, 23, 0], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#8dd3c7", text="D1", hoverinfo='text') trace3 = go.Scatterternary(name='Duval Triangle2', uid="3eab74", a=[0, 0, 31, 47, 64], b=[77, 29, 29, 13, 13], c=[23, 71, 40, 40, 23], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#bebada", text="D2", hoverinfo='text') trace4 = go.Scatterternary(name='Duval Triangle4', uid="3eab74", a=[1, 0, 35, 46, 96, 87, 47, 31], b=[29, 15, 15, 4, 4, 13, 13, 29], c=[71, 85, 50, 50, 0, 0, 40, 40], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#fb8072", text="DT", hoverinfo='text') trace5 = go.Scatterternary(name='Duval Triangle5', uid="381ad2", a=[76, 80, 98, 98, 96], b=[4, 0, 0, 2, 4], c=[20, 20, 2, 0, 0], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#80b1d3", text="T1", hoverinfo='text') trace6 = go.Scatterternary(name='Duval Triangle5', uid="8cc163", a=[46, 50, 80, 76], b=[4, 0, 0, 4], c=[50, 50, 20, 20], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#90d133", text="T2", hoverinfo='text') trace7 = go.Scatterternary(name='Duval Triangle5', uid="6f33dc", a=[0, 0, 50, 35], b=[15, 0, 0, 15], c=[85, 1, 50, 50], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#40f1d3", text="T3", hoverinfo='text') trace8 = go.Scatterternary( name='Duval Triangle5', uid="6f33dc", a=[32, 30, 34, 35], # Pasar vector de datos historicos b=[43, 50, 34, 54], # Pasar vector de datos historicos c=[25, 20, 32, 11], # Pasar vector de datos historicos mode="markers+lines", marker={ "size": 10, "color": "black", "symbol": 4 }) hover_value = [ date_str + '<br>CH4: ' + '{}%'.format(trace8['a'][k]) + '<br>C2H2: ' + '{}%'.format(trace8['b'][k]) + '<br>C2H4: ' + '{}%'.format(trace8['c'][k]) for k in range(len(trace8['a'])) ] trace8['text'] = hover_value trace8['hoverinfo'] = 'text' figure = go.Figure() # Duval Triangle 1 figure.add_trace(trace1) figure.add_trace(trace2) figure.add_trace(trace3) figure.add_trace(trace4) figure.add_trace(trace5) figure.add_trace(trace6) figure.add_trace(trace7) figure.add_trace(trace8) layoutT = go.Layout( ternary={ "sum": 100, "aaxis": { "min": 0.01, "ticks": "outside", "title": "CH4", # "(Methane)", "linewidth": 2, "ticksuffix": "%", "color": '#e6e6e6' }, "baxis": { "min": 0.01, "ticks": "outside", "title": "C2H2", # "(Acetylene)", "linewidth": 2, "ticksuffix": "%", "color": '#e6e6e6' }, "caxis": { "min": 0.01, "ticks": "outside", "title": "C2H4", # "(Ethylene)", "linewidth": 2, "ticksuffix": "%", "color": '#e6e6e6' } }, showlegend=False, plot_bgcolor='#282828', paper_bgcolor='#222222', margin=dict(l=50, r=50, t=40, b=40)) figure['layout'].update(layoutT) figure.update_xaxes(tickfont=dict(color='#e6e6e6')) figure.update_yaxes(tickfont=dict(color='#e6e6e6')) for annotation in figure['layout']['annotations']: annotation['font'] = dict(color='#e6e6e6') return dict(message=T('Transformer analysis'), transformer=transformer, figure=figure.to_json())
def Triang4(date_str): # Duval Triangle 4 trace9 = go.Scatterternary(name='Duval Triangle', uid="a1a8b8", a=[0, 9, 9, 0], b=[1, 91, 30, 30], c=[0, 0, 61, 70], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#3dd3c7", text="O", hoverinfo='text') trace10 = go.Scatterternary(name='Duval Triangle1', uid="c72344", a=[9, 55, 9], b=[91, 45, 45], c=[0, 0, 46], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#8dd3c7", text="I", hoverinfo='text') trace11 = go.Scatterternary( name='Duval Triangle2', uid="3eab74", a=[55, 1, 98, 97, 84, 85, 64, 40, 15, 15, 9, 9], b=[45, 0, 0, 1, 1, 0, 0, 24, 24, 30, 30, 45], c=[0, 0, 2, 2, 15, 15, 36, 36, 61, 55, 61, 46], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#bebada", text="S", hoverinfo='text') trace12 = go.Scatterternary(name='Duval Triangle4', uid="3eab74", a=[98, 97, 84, 85], b=[0, 1, 1, 0], c=[2, 2, 15, 15], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#fb8072", text="PD", hoverinfo='text') trace13 = go.Scatterternary(name='Duval Triangle5', uid="381ad2", a=[0, 0, 15, 15, 40, 64], b=[0, 30, 30, 24, 24, 0], c=[1, 70, 55, 61, 36, 36], fill="toself", line={"color": "#444"}, mode="lines", fillcolor="#80b1d3", text="C", hoverinfo='text') trace14 = go.Scatterternary( name='Duval Triangle5', uid="6f33dc", a=[32, 30, 34, 35], # Pasar vector de datos historicos b=[43, 50, 34, 54], # Pasar vector de datos historicos c=[25, 20, 32, 11], # Pasar vector de datos historicos mode="markers+lines", marker={ "size": 10, "color": "black", "symbol": 4 }) hover_value = [ date_str + '<br>H2: ' + '{}%'.format(trace14['a'][k]) + '<br>C2H6: ' + '{}%'.format(trace14['b'][k]) + '<br>CH4: ' + '{}%'.format(trace14['c'][k]) for k in range(len(trace14['a'])) ] trace14['text'] = hover_value trace14['hoverinfo'] = 'text' # Duval Triangle 4 figure = go.Figure() figure.add_trace(trace9) figure.add_trace(trace10) figure.add_trace(trace11) figure.add_trace(trace12) figure.add_trace(trace13) figure.add_trace(trace14) # Layout layoutT = go.Layout( ternary={ "sum": 100, "aaxis": { "min": 0.01, "ticks": "outside", "title": "H2", # "(Methane)", "linewidth": 2, "ticksuffix": "%", "color": '#e6e6e6' }, "baxis": { "min": 0.01, "ticks": "outside", "title": "C2H6", # "(Acetylene)", "linewidth": 2, "ticksuffix": "%", "color": '#e6e6e6' }, "caxis": { "min": 0.01, "ticks": "outside", "title": "CH4", # "(Ethylene)", "linewidth": 2, "ticksuffix": "%", "color": '#e6e6e6' } }, showlegend=False, plot_bgcolor='#282828', paper_bgcolor='#222222', margin=dict(l=50, r=50, t=50, b=50)) figure['layout'].update(layoutT) figure.update_xaxes(tickfont=dict(color='#e6e6e6')) figure.update_yaxes(tickfont=dict(color='#e6e6e6')) for annotation in figure['layout']['annotations']: annotation['font'] = dict(color='#e6e6e6') return dict(msgTriang4=T('Triangle 4'), figTriang4=figure.to_json())