def plotTraces( plotType, label, traces, title, metadata, layoutOptions, suffix =None ): suffix = '' if not suffix else ('-' + suffix) url = plotly.plot( filename=PlotlyUtils.getPlotlyFilename( filename='{}{}'.format(label.replace(' ', '-'), suffix), folder=PLOTLY_FOLDER), figure_or_data=plotlyGraph.Figure( data=plotlyGraph.Data(traces), layout=_getLayout( metadata=metadata, title=title, **layoutOptions)), auto_open=False) print('{}[{}]: {}'.format(plotType, label, PlotlyUtils.toEmbedUrl(url)))
def makePlot(label, tracks): tracks = tracks.copy() xBounds = [ NumericUtils.roundToOrder(tracks.length.min() - 0.05, -1, math.floor), NumericUtils.roundToOrder(tracks.length.max() + 0.05, -1, math.ceil)] yBounds = [ NumericUtils.roundToOrder(tracks.width.min() - 0.05, -1, math.floor), NumericUtils.roundToOrder(tracks.width.max() + 0.05, -1, math.ceil)] fig = plotlyTools.make_subplots( rows=1, cols=2, subplot_titles=('Length vs Width','Aspect Ratios'), print_grid=False) traces = [] for site in tracks.site.unique(): color = PlotConfigs.SITE_SPECS[site]['color'] siteSlice = tracks[tracks.site == site] traces.append(plotlyGraph.Scatter( name=site, mode='markers', xaxis='x1', yaxis='y1', marker=plotlyGraph.Marker(color=color), x=siteSlice.length, y=siteSlice.width)) traces.append(plotlyGraph.Box( name=site, y=siteSlice.length/siteSlice.width, marker=plotlyGraph.Marker(color=color), xaxis='x2', yaxis='y2')) fig['data'] += plotlyGraph.Data(traces) fig['layout'].update( title='%s Length & Width by Tracksite' % label, xaxis1=plotlyGraph.XAxis( title='Length (m)', range=xBounds, autorange=False ), yaxis1=plotlyGraph.YAxis( title='Width (m)', range=yBounds, autorange=False )) url = plotly.plot( filename='A16/%s-Length-Width' % label, figure_or_data=fig, auto_open=False) print('PLOT[%s]:' % label, PlotlyUtils.toEmbedUrl(url))
def makeStackedBars(label, columnName, tracks): traces = [] xStart = tracks[columnName].min() xEnd = tracks[columnName].max() binDelta = 0.01 bins = [xStart] while bins[-1] < xEnd: bins.append(min(xEnd, bins[-1] + binDelta)) for site in tracks.site.unique(): color = PlotConfigs.SITE_SPECS[site]['color'] siteSlice = tracks[tracks.site == site] data = np.histogram(a=siteSlice[columnName].values, bins=bins) traces.append(plotlyGraph.Bar( name=site, x=data[1], y=data[0], marker=plotlyGraph.Marker(color=color) )) data = plotlyGraph.Data(traces) layout = plotlyGraph.Layout( title='%s Distributions by Tracksite' % label, barmode='stack', xaxis=plotlyGraph.XAxis( title='Track %s' % label, range=[xStart - 0.01, xEnd + 0.01], autorange=False ), yaxis=plotlyGraph.YAxis( title='Count', autorange=True)) url = plotly.plot( filename='A16/%s-Stacked-Distributions' % label, figure_or_data=plotlyGraph.Figure(data=data, layout=layout), auto_open=False) print('STACK[%s]:' % label, PlotlyUtils.toEmbedUrl(url))
def plotComparison(df, key, label, binCount): xMin = df[key].min() xMax = df[key].max() count = df.shape[0] test = np.histogram( a=df[key].values, bins=np.linspace(xMin, xMax, 512)) cdf = 100.0*np.cumsum(test[0])/float(count) for i in range(len(cdf)): if cdf[i] >= 98.0: xMax = test[1][i] break areaTraces = [] xValues = np.linspace(xMin, xMax, 64) areaValues = np.zeros(len(xValues) - 1) histTraces = [] bins = np.linspace(xMin, xMax, int(binCount)) index = 0 for sizeClass in PlotConfigs.SIZE_CLASSES: color = sizeClass['color'] dataSlice = df[ (df['width'] >= sizeClass['range'][0]) & (df['width'] < sizeClass['range'][1] )] sliceCountLabel = locale.format( '%d', int(dataSlice.shape[0]), grouping=True) histValues = np.histogram(a=dataSlice[key].values, bins=bins) histTraces.append(plotlyGraph.Bar( name='%s (%s)' % (sizeClass['name'], sliceCountLabel), x=100.0*histValues[1], y=histValues[0], marker=plotlyGraph.Marker(color=color) )) histValues = np.histogram(a=dataSlice[key].values, bins=xValues) values = 100.0*(dataSlice.shape[0] - np.cumsum(histValues[0]))/count areaValues = np.add(areaValues, values) areaTraces.append(plotlyGraph.Scatter( name='%s (%s)' % (sizeClass['name'], sliceCountLabel), x=100.0*histValues[1], y=areaValues, mode='lines', fill='tozeroy' if index < 1 else 'tonexty', line=plotlyGraph.Line( width=1.0, color=color) )) index += 1 countLabel = locale.format('%d', count, grouping=True) data = plotlyGraph.Data(histTraces) layout = plotlyGraph.Layout( title='Distribution of Track %s Deviations (%s Tracks)' % ( label, countLabel), barmode='stack', xaxis=plotlyGraph.XAxis( title='Deviation (%)'), yaxis=plotlyGraph.YAxis( title='Frequency', autorange=True)) url = plotly.plot( filename=PlotlyUtils.getPlotlyFilename( filename='%s' % label.replace(' ', '-'), folder=PLOTLY_FOLDER), figure_or_data=plotlyGraph.Figure(data=data, layout=layout), auto_open=False) print( 'COMPARE|HIST[%s]' % label, 'x:(%s, %s)' % (xMin, xMax), PlotlyUtils.toEmbedUrl(url)) expected = [] for x in xValues: expected.append(100.0*(1.0 - (norm.cdf(x) - norm.cdf(-x)))) areaTraces.append(plotlyGraph.Scatter( name='Normal Threshold', x=100.0*xValues, y=expected, mode='lines', line=plotlyGraph.Line( color='rgba(0, 0, 0, 0.75)', dash='dash', width=1.0) )) data = plotlyGraph.Data(areaTraces) layout = plotlyGraph.Layout( title=('Inverse Cumulative Distribution of Track ' + '%s Deviations (%s Tracks)') % (label, countLabel), xaxis=plotlyGraph.XAxis( title='Deviation (%)', range=[100.0*xMin, 100.0*xMax], autorange=False), yaxis=plotlyGraph.YAxis( title='Cumulative Remainder (%)', range=[0, 100], autorange=False)) url = plotly.plot( filename=PlotlyUtils.getPlotlyFilename( filename='%s-cdf-remainder' % label.replace(' ', '-'), folder=PLOTLY_FOLDER), figure_or_data=plotlyGraph.Figure(data=data, layout=layout), auto_open=False) print( 'COMPARE|AREA[%s]' % label, 'x:(%s, %s)' % (xMin, xMax), PlotlyUtils.toEmbedUrl(url))
def makeHistograms(label, columnName, errorColumnName, tracks): index = 0 histTraces = [] densityTraces = [] xStart = tracks[columnName].min() xEnd = tracks[columnName].max() sites = tracks.site.unique() for site in sites: index += 1 color = PlotConfigs.SITE_SPECS[site]['color'] siteSlice = tracks[tracks.site == site] histTraces.append(plotlyGraph.Histogram( name=site, x=siteSlice[columnName], autobinx=False, xbins=plotlyGraph.XBins( start=xStart, end=xEnd, size=0.01), xaxis='x1', yaxis='y%s' % int(index), marker=plotlyGraph.Marker(color=color) )) distributionValues = [] for i, row in siteSlice.iterrows(): distributionValues.append(NumericUtils.toValueUncertainty( value=row[columnName], uncertainty=row[errorColumnName])) dd = DensityDistribution(values=distributionValues) xValues = dd.getAdaptiveRange(10) yValues = dd.createDistribution(xValues=xValues, scaled=True) densityTraces.append(plotlyGraph.Scatter( name=site, x=xValues, y=yValues, xaxis='x1', yaxis='y%s' % int(index), mode='lines', fill='tozeroy', marker=plotlyGraph.Marker(color=color) )) fig = plotlyTools.make_subplots( rows=len(sites), cols=1, shared_xaxes=True, print_grid=False) fig['data'] += plotlyGraph.Data(histTraces) fig['layout'].update(title='%s Distributions by Tracksite' % label) url = plotly.plot( filename='A16/%s-Distributions' % label.replace(' ', '-'), figure_or_data=fig, auto_open=False) print('HISTOGRAM[%s]:' % label, PlotlyUtils.toEmbedUrl(url)) fig = plotlyTools.make_subplots( rows=len(sites), cols=1, shared_xaxes=True, print_grid=False) fig['data'] += plotlyGraph.Data(densityTraces) fig['layout'].update( title='%s Distributions by Tracksite' % label, xaxis1=plotlyGraph.XAxis( autorange=False, range=[xStart, xEnd])) url = plotly.plot( filename='A16/%s-Kernel-Distributions' % label.replace(' ', '-'), figure_or_data=fig, auto_open=False) print('KERNEL-DENSITY[%s]:' % label, PlotlyUtils.toEmbedUrl(url))