def setHistogram(self, values=None, bins=None, use_kde=False, histogram=None):
""" Set background histogram (or density estimation, violin plot)
The histogram of bins is calculated from values, optionally as a
Gaussian KDE. If histogram is provided, its values are used directly
and other parameters are ignored.
"""
if (values is None or not len(values)) and histogram is None:
self.setPixmap(None)
return
if histogram is not None:
self._histogram = hist = histogram
else:
if bins is None:
bins = min(100, max(10, len(values) // 20))
if use_kde:
hist = gaussian_kde(values,
None if isinstance(use_kde, bool) else use_kde)(
np.linspace(np.min(values), np.max(values), bins))
else:
hist = np.histogram(values, bins)[0]
self._histogram = hist = hist / hist.max()
HEIGHT = self.rect().height() / 2
OFFSET = HEIGHT * .3
pixmap = QPixmap(QSize(len(hist), 2 * (HEIGHT + OFFSET))) # +1 avoids right/bottom frame border shadow
pixmap.fill(Qt.transparent)
painter = QPainter(pixmap)
painter.setPen(QPen(Qt.darkGray))
for x, value in enumerate(hist):
painter.drawLine(x, HEIGHT * (1 - value) + OFFSET,
x, HEIGHT * (1 + value) + OFFSET)
if self.orientation() != Qt.Horizontal:
pixmap = pixmap.transformed(QTransform().rotate(-90))
self.setPixmap(pixmap)
评论列表
文章目录