Skip to content
Permalink
Browse files

[processing] update mean and std.dev plot

  • Loading branch information
alexbruy committed Feb 15, 2017
1 parent 2abc3f7 commit 19289ccc4ee865c2d7b2df2fc17219cc6d4e42b3
@@ -25,9 +25,8 @@

__revision__ = '$Format:%H$'

import matplotlib.pyplot as plt
import matplotlib.pylab as lab
import numpy as np
import plotly as plt
import plotly.graph_objs as go

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterTable
@@ -43,8 +42,7 @@ class MeanAndStdDevPlot(GeoAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
NAME_FIELD = 'NAME_FIELD'
MEAN_FIELD = 'MEAN_FIELD'
STDDEV_FIELD = 'STDDEV_FIELD'
VALUE_FIELD = 'VALUE_FIELD'

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Mean and standard deviation plot')
@@ -55,33 +53,33 @@ def defineCharacteristics(self):
self.addParameter(ParameterTableField(self.NAME_FIELD,
self.tr('Category name field'), self.INPUT,
ParameterTableField.DATA_TYPE_ANY))
self.addParameter(ParameterTableField(self.MEAN_FIELD,
self.tr('Mean field'), self.INPUT))
self.addParameter(ParameterTableField(self.STDDEV_FIELD,
self.tr('StdDev field'), self.INPUT))
self.addParameter(ParameterTableField(self.VALUE_FIELD,
self.tr('Value field'), self.INPUT))

self.addOutput(OutputHTML(self.OUTPUT, self.tr('Plot')))

def processAlgorithm(self, feedback):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
namefieldname = self.getParameterValue(self.NAME_FIELD)
meanfieldname = self.getParameterValue(self.MEAN_FIELD)
stddevfieldname = self.getParameterValue(self.STDDEV_FIELD)
valuefieldname = self.getParameterValue(self.VALUE_FIELD)

output = self.getOutputValue(self.OUTPUT)

values = vector.values(layer, namefieldname, meanfieldname, stddevfieldname)
plt.close()
ind = np.arange(len(values[namefieldname]))
width = 0.8
plt.bar(ind, values[meanfieldname], width, color='r',
yerr=values[stddevfieldname],
error_kw=dict(ecolor='yellow'),
)

plt.xticks(ind, values[namefieldname], rotation=45)
plotFilename = output + '.png'
lab.savefig(plotFilename)
with open(output, 'w') as f:
f.write('<html><img src="' + plotFilename + '"/></html>')
values = vector.values(layer, namefieldname, valuefieldname)

d = {}
for i in range(len(values[namefieldname])):
v = values[namefieldname][i]
if v not in d:
d[v] = [values[valuefieldname][i]]
else:
d[v].append(values[valuefieldname][i])

data = []
for k, v in d.items():
data.append(go.Box(y=list(v),
boxmean='sd',
name=k
))
plt.offline.plot(data, filename=output)
@@ -263,28 +263,28 @@ def __init__(self):
]

#~ if hasMatplotlib:
#~ from .VectorLayerHistogram import VectorLayerHistogram
#~ from .RasterLayerHistogram import RasterLayerHistogram
#~ from .VectorLayerScatterplot import VectorLayerScatterplot
#~ from .MeanAndStdDevPlot import MeanAndStdDevPlot
#~ from .BarPlot import BarPlot
#~ from .PolarPlot import PolarPlot
#~ from .VectorLayerHistogram import VectorLayerHistogram
#~ from .RasterLayerHistogram import RasterLayerHistogram
#~ from .VectorLayerScatterplot import VectorLayerScatterplot
#~ from .MeanAndStdDevPlot import MeanAndStdDevPlot
#~ from .BarPlot import BarPlot
#~ from .PolarPlot import PolarPlot

#~ self.alglist.extend([
#~ VectorLayerHistogram(), RasterLayerHistogram(),
#~ VectorLayerScatterplot(), MeanAndStdDevPlot(), BarPlot(),
#~ PolarPlot(),
#~ ])
#~ self.alglist.extend([
#~ VectorLayerHistogram(), RasterLayerHistogram(),
#~ VectorLayerScatterplot(), MeanAndStdDevPlot(), BarPlot(),
#~ PolarPlot(),
#~ ])
if hasPlotly:
from .VectorLayerHistogram import VectorLayerHistogram
from .RasterLayerHistogram import RasterLayerHistogram
from .VectorLayerScatterplot import VectorLayerScatterplot
#~ from .MeanAndStdDevPlot import MeanAndStdDevPlot
from .MeanAndStdDevPlot import MeanAndStdDevPlot
from .BarPlot import BarPlot
#~ from .PolarPlot import PolarPlot

self.alglist.extend([VectorLayerHistogram(), RasterLayerHistogram(),
VectorLayerScatterplot(), BarPlot()])
VectorLayerScatterplot(), MeanAndStdDevPlot(), BarPlot()])

self.externalAlgs = [] # to store algs added by 3rd party plugins as scripts

0 comments on commit 19289cc

Please sign in to comment.
You can’t perform that action at this time.