Skip to content

Commit

Permalink
[processing] update mean and std.dev plot
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Feb 15, 2017
1 parent 2abc3f7 commit 19289cc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
48 changes: 23 additions & 25 deletions python/plugins/processing/algs/qgis/MeanAndStdDevPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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)
26 changes: 13 additions & 13 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 19289cc

Please sign in to comment.