📄 owscatterplotgraph.py
字号:
for key in self.legendCurveKeys: # remove old curve keys
self.removeCurve(key)
self.legendCurveKeys = []
for val in legendKeys.values(): # add new curve keys
for i in range(len(val[1])):
k = self.addCurve(val[0][i], val[1][i], val[1][i], val[2][i], symbol = val[3][i], enableLegend = 1)
self.legendCurveKeys.append(k)
self.oldLegendKeys = legendKeys
# ##############################################################
# draw color scale for continuous coloring attribute
if colorIndex != -1 and showColorLegend and self.rawdata.domain[colorIndex].varType == orange.VarTypes.Continuous:
x0 = xmax + xVar*1.0/100.0; x1 = x0 + xVar*2.5/100.0
count = 200
height = yVar / float(count)
xs = [x0, x1, x1, x0]
for i in range(count):
y = yVarMin + i*yVar/float(count)
col = self.contPalette[i/float(count)]
curve = PolygonCurve(self, QPen(col), QBrush(col))
newCurveKey = self.insertCurve(curve)
self.setCurveData(newCurveKey, xs, [y,y, y+height, y+height])
# add markers for min and max value of color attribute
(colorVarMin, colorVarMax) = self.attrValues[colorAttr]
self.addMarker("%s = %%.%df" % (colorAttr, self.rawdata.domain[colorAttr].numberOfDecimals) % (colorVarMin), x0 - xVar*1./100.0, yVarMin + yVar*0.04, Qt.AlignLeft)
self.addMarker("%s = %%.%df" % (colorAttr, self.rawdata.domain[colorAttr].numberOfDecimals) % (colorVarMax), x0 - xVar*1./100.0, yVarMin + yVar*0.96, Qt.AlignLeft)
# ##############################################################
# ###### SHOW CLUSTER LINES ##################################
# ##############################################################
def showClusterLines(self, xAttr, yAttr, width = 1):
classIndices = getVariableValueIndices(self.rawdata, self.attributeNameIndex[self.rawdata.domain.classVar.name])
shortData = self.rawdata.select([self.rawdata.domain[xAttr], self.rawdata.domain[yAttr], self.rawdata.domain.classVar])
shortData = orange.Preprocessor_dropMissing(shortData)
(closure, enlargedClosure, classValue) = self.clusterClosure
(xVarMin, xVarMax) = self.attrValues[xAttr]
(yVarMin, yVarMax) = self.attrValues[yAttr]
xVar = xVarMax - xVarMin
yVar = yVarMax - yVarMin
if type(closure) == dict:
for key in closure.keys():
clusterLines = closure[key]
color = self.discPalette[classIndices[self.rawdata.domain.classVar[classValue[key]].value]]
for (p1, p2) in clusterLines:
self.addCurve("", color, color, 1, QwtCurve.Lines, QwtSymbol.None, xData = [float(shortData[p1][0]), float(shortData[p2][0])], yData = [float(shortData[p1][1]), float(shortData[p2][1])], lineWidth = width)
else:
colorIndex = self.discPalette[classIndices[self.rawdata.domain.classVar[classValue].value]]
for (p1, p2) in closure:
self.addCurve("", color, color, 1, QwtCurve.Lines, QwtSymbol.None, xData = [float(shortData[p1][0]), float(shortData[p2][0])], yData = [float(shortData[p1][1]), float(shortData[p2][1])], lineWidth = width)
def addTip(self, x, y, attrIndices = None, dataindex = None, text = None):
if self.tooltipKind == DONT_SHOW_TOOLTIPS: return
if text == None:
if self.tooltipKind == VISIBLE_ATTRIBUTES: text = self.getExampleTooltipText(self.rawdata, self.rawdata[dataindex], attrIndices)
elif self.tooltipKind == ALL_ATTRIBUTES: text = self.getExampleTooltipText(self.rawdata, self.rawdata[dataindex], range(len(self.attributeNames)))
self.tips.addToolTip(x, y, text)
# override the default buildTooltip function defined in OWGraph
def buildTooltip(self, exampleIndex):
if self.tooltipKind == VISIBLE_ATTRIBUTES: text = self.getExampleTooltipText(self.rawdata, self.rawdata[exampleIndex], self.shownAttributeIndices)
elif self.tooltipKind == ALL_ATTRIBUTES: text = self.getExampleTooltipText(self.rawdata, self.rawdata[exampleIndex], range(len(self.rawdata.domain)))
return text
# ##############################################################
# send 2 example tables. in first is the data that is inside selected rects (polygons), in the second is unselected data
def getSelectionsAsExampleTables(self, attrList):
[xAttr, yAttr] = attrList
#if not self.rawdata: return (None, None, None)
if not self.rawdata: return (None, None)
if not self.selectionCurveKeyList: return (None, self.rawdata) # if no selections exist
selIndices, unselIndices = self.getSelectionsAsIndices(attrList)
selected = self.rawdata.selectref(selIndices)
unselected = self.rawdata.selectref(unselIndices)
if len(selected) == 0: selected = None
if len(unselected) == 0: unselected = None
return (selected, unselected)
def getSelectionsAsIndices(self, attrList, validData = None):
[xAttr, yAttr] = attrList
if not self.rawdata: return [], []
attrIndices = [self.attributeNameIndex[attr] for attr in attrList]
if not validData: validData = self.getValidList(attrIndices)
(xArray, yArray) = self.getXYPositions(xAttr, yAttr)
return self.getSelectedPoints(xArray, yArray, validData)
# add tooltips for pie charts
def addTooltips(self):
for (text, i, j) in self.tooltipData:
x_1 = self.transform(QwtPlot.xBottom, i-0.5); x_2 = self.transform(QwtPlot.xBottom, i+0.5)
y_1 = self.transform(QwtPlot.yLeft, j+0.5); y_2 = self.transform(QwtPlot.yLeft, j-0.5)
rect = QRect(x_1, y_1, x_2-x_1, y_2-y_1)
self.toolRects.append(rect)
QToolTip.add(self, rect, text)
def removeTooltips(self):
for rect in self.toolRects: QToolTip.remove(self, rect)
self.toolRects = []
def onMouseReleased(self, e):
OWGraph.onMouseReleased(self, e)
self.updateLayout()
def computePotentials(self):
import orangeom
rx = self.transform(QwtPlot.xBottom, self.xmax) - self.transform(QwtPlot.xBottom, self.xmin)
ry = self.transform(QwtPlot.yLeft, self.ymin) - self.transform(QwtPlot.yLeft, self.ymax)
rx -= rx % self.squareGranularity
ry -= ry % self.squareGranularity
ox = self.transform(QwtPlot.xBottom, 0) - self.transform(QwtPlot.xBottom, self.xmin)
oy = self.transform(QwtPlot.yLeft, self.ymin) - self.transform(QwtPlot.yLeft, 0)
if not getattr(self, "potentialsBmp", None) or getattr(self, "potentialContext", None) != (rx, ry, self.shownXAttribute, self.shownYAttribute, self.squareGranularity, self.jitterSize, self.jitterContinuous, self.spaceBetweenCells):
if self.potentialsClassifier.classVar.varType == orange.VarTypes.Continuous:
imagebmp = orangeom.potentialsBitmap(self.potentialsClassifier, rx, ry, ox, oy, self.squareGranularity, 1) # the last argument is self.trueScaleFactor (in LinProjGraph...)
palette = [qRgb(255.*i/255., 255.*i/255., 255-(255.*i/255.)) for i in range(255)] + [qRgb(255, 255, 255)]
else:
imagebmp, nShades = orangeom.potentialsBitmap(self.potentialsClassifier, rx, ry, ox, oy, self.squareGranularity, 1., self.spaceBetweenCells) # the last argument is self.trueScaleFactor (in LinProjGraph...)
colors = defaultRGBColors
palette = []
sortedClasses = getVariableValuesSorted(self.potentialsClassifier, self.potentialsClassifier.domain.classVar.name)
for cls in self.potentialsClassifier.classVar.values:
color = colors[sortedClasses.index(cls)]
towhite = [255-c for c in color]
for s in range(nShades):
si = 1-float(s)/nShades
palette.append(qRgb(*tuple([color[i]+towhite[i]*si for i in (0, 1, 2)])))
palette.extend([qRgb(255, 255, 255) for i in range(256-len(palette))])
image = QImage(imagebmp, (rx + 3) & ~3, ry, 8, palette, 256, QImage.LittleEndian)
self.potentialsBmp = QPixmap()
self.potentialsBmp.convertFromImage(image)
self.potentialContext = (rx, ry, self.shownXAttribute, self.shownYAttribute, self.squareGranularity, self.jitterSize, self.jitterContinuous, self.spaceBetweenCells)
def drawCanvasItems(self, painter, rect, map, pfilter):
if self.showProbabilities and getattr(self, "potentialsClassifier", None):
self.computePotentials()
painter.drawPixmap(QPoint(self.transform(QwtPlot.xBottom, self.xmin), self.transform(QwtPlot.yLeft, self.ymax)), self.potentialsBmp)
OWGraph.drawCanvasItems(self, painter, rect, map, pfilter)
class QwtPlotCurvePieChart(QwtPlotCurve):
def __init__(self, parent = None, text = None):
QwtPlotCurve.__init__(self, parent, text)
self.color = Qt.black
self.penColor = Qt.black
self.parent = parent
def draw(self, p, xMap, yMap, f, t):
# save ex settings
back = p.backgroundMode()
pen = p.pen()
brush = p.brush()
colors = self.parent.discPalette
p.setBackgroundMode(Qt.OpaqueMode)
#p.setBackgroundColor(self.color)
for i in range(self.dataSize()-1):
p.setBrush(QBrush(colors[i]))
p.setPen(QPen(colors[i]))
factor = self.percentOfTotalData * self.percentOfTotalData
px1 = xMap.transform(self.x(0)-0.1 - 0.5*factor)
py1 = yMap.transform(self.x(1)-0.1 - 0.5*factor)
px2 = xMap.transform(self.x(0)+0.1 + 0.5*factor)
py2 = yMap.transform(self.x(1)+0.1 + 0.5*factor)
p.drawPie(px1, py1, px2-px1, py2-py1, self.y(i)*16*360, (self.y(i+1)-self.y(i))*16*360)
# restore ex settings
p.setBackgroundMode(back)
p.setPen(pen)
p.setBrush(brush)
if __name__== "__main__":
#Draw a simple graph
a = QApplication(sys.argv)
c = OWScatterPlotGraph(None)
a.setMainWidget(c)
c.show()
a.exec_loop()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -