def __init__(self, pixmap, title="", parentItem=None, **kwargs): super().__init__(parentItem, **kwargs) self.setFocusPolicy(Qt.StrongFocus) self._title = None self._size = QSizeF() layout = QGraphicsLinearLayout(Qt.Vertical, self) layout.setSpacing(2) layout.setContentsMargins(5, 5, 5, 5) self.setContentsMargins(0, 0, 0, 0) self.pixmapWidget = GraphicsPixmapWidget(pixmap, self) self.labelWidget = GraphicsTextWidget(title, self) layout.addItem(self.pixmapWidget) layout.addItem(self.labelWidget) layout.addStretch() layout.setAlignment(self.pixmapWidget, Qt.AlignCenter) layout.setAlignment(self.labelWidget, Qt.AlignHCenter | Qt.AlignBottom) self.setLayout(layout) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setTitle(title) self.setTitleWidth(100)
def __init__(self, pixmap, title="", parentItem=None, **kwargs): super().__init__(parentItem, **kwargs) self.setFocusPolicy(Qt.StrongFocus) self._title = None self._size = QSizeF() layout = QGraphicsLinearLayout(Qt.Vertical, self) layout.setSpacing(2) layout.setContentsMargins(5, 5, 5, 5) self.setContentsMargins(0, 0, 0, 0) self.pixmapWidget = GraphicsPixmapWidget(pixmap, self) self.labelWidget = GraphicsTextWidget(title, self) layout.addItem(self.pixmapWidget) layout.addItem(self.labelWidget) layout.addStretch() layout.setAlignment(self.pixmapWidget, Qt.AlignCenter) layout.setAlignment(self.labelWidget, Qt.AlignHCenter | Qt.AlignBottom) self.setLayout(layout) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setTitle(title) self.setTitleWidth(100)
def _draw_histogram(self): if self.distributions.ndim > 1: largest_bin_count = self.distributions.sum(axis=1).max() else: largest_bin_count = self.distributions.max() bar_size = self._plot_width / self.n_bins for distr, bin_colors in zip(self.distributions, self._get_colors()): bin_count = distr.sum() bar_height = bin_count / largest_bin_count * self._plot_height bar_layout = QGraphicsLinearLayout(Qt.Vertical) bar_layout.setSpacing(0) bar_layout.addStretch() self.__layout.addItem(bar_layout) bar = ProportionalBarItem( distribution=distr, colors=bin_colors, height=bar_height, bar_size=bar_size, ) bar_layout.addItem(bar) self.layout()
class ContinuousLegendItem(QGraphicsLinearLayout): """Continuous legend item. Contains a gradient bar with the color ranges, as well as two labels - one on each side of the gradient bar. Parameters ---------- palette : iterable[QColor] values : iterable[float...] The number of values must match the number of colors in passed in the color palette. parent : QGraphicsWidget font : QFont orientation : Qt.Orientation """ def __init__(self, palette, values, parent, font=None, orientation=Qt.Vertical): if orientation == Qt.Vertical: super().__init__(Qt.Horizontal) else: super().__init__(Qt.Vertical) self.__parent = parent self.__palette = palette self.__values = values if isinstance(palette, ContinuousPalette): self.__gradient = ColorStripItem(palette, parent, orientation) else: self.__gradient = LegendGradient(palette, parent, orientation) self.__labels_layout = QGraphicsLinearLayout(orientation) str_vals = self._format_values(values) self.__start_label = LegendItemTitle(str_vals[0], parent, font=font) self.__end_label = LegendItemTitle(str_vals[1], parent, font=font) self.__labels_layout.addItem(self.__start_label) self.__labels_layout.addStretch(1) self.__labels_layout.addItem(self.__end_label) # Gradient should be to the left, then labels on the right if vertical if orientation == Qt.Vertical: self.addItem(self.__gradient) self.addItem(self.__labels_layout) # Gradient should be on the bottom, labels on top if horizontal elif orientation == Qt.Horizontal: self.addItem(self.__labels_layout) self.addItem(self.__gradient) @staticmethod def _format_values(values): """Get the formatted values to output.""" return ['{:.3f}'.format(v) for v in values]
class ContinuousLegendItem(QGraphicsLinearLayout): """Continuous legend item. Contains a gradient bar with the color ranges, as well as two labels - one on each side of the gradient bar. Parameters ---------- palette : iterable[QColor] values : iterable[float...] The number of values must match the number of colors in passed in the color palette. parent : QGraphicsWidget font : QFont orientation : Qt.Orientation """ def __init__(self, palette, values, parent, font=None, orientation=Qt.Vertical): if orientation == Qt.Vertical: super().__init__(Qt.Horizontal) else: super().__init__(Qt.Vertical) self.__parent = parent self.__palette = palette self.__values = values self.__gradient = LegendGradient(palette, parent, orientation) self.__labels_layout = QGraphicsLinearLayout(orientation) str_vals = self._format_values(values) self.__start_label = LegendItemTitle(str_vals[0], parent, font=font) self.__end_label = LegendItemTitle(str_vals[1], parent, font=font) self.__labels_layout.addItem(self.__start_label) self.__labels_layout.addStretch(1) self.__labels_layout.addItem(self.__end_label) # Gradient should be to the left, then labels on the right if vertical if orientation == Qt.Vertical: self.addItem(self.__gradient) self.addItem(self.__labels_layout) # Gradient should be on the bottom, labels on top if horizontal elif orientation == Qt.Horizontal: self.addItem(self.__labels_layout) self.addItem(self.__gradient) @staticmethod def _format_values(values): """Get the formatted values to output.""" return ['{:.3f}'.format(v) for v in values]
def _draw_histogram(self): # In case the data for the variable were all NaNs, then the # distributions will be empty, and we don't need to display any bars if self.x.size == 0: return # In case we have a target var, but the values are all NaNs, then there # is no sense in displaying anything if self.target_var: y_nn = self.y[~np.isnan(self.y)] if y_nn.size == 0: return if self.distributions.ndim > 1: largest_bin_count = self.distributions.sum(axis=1).max() else: largest_bin_count = self.distributions.max() bar_size = self._plot_width / self.n_bins for distr, bin_colors in zip(self.distributions, self._get_colors()): bin_count = distr.sum() bar_height = bin_count / largest_bin_count * self._plot_height bar_layout = QGraphicsLinearLayout(Qt.Vertical) bar_layout.setSpacing(0) bar_layout.addStretch() self.__layout.addItem(bar_layout) bar = ProportionalBarItem( # pylint: disable=blacklisted-name distribution=distr, colors=bin_colors, height=bar_height, bar_size=bar_size, ) bar_layout.addItem(bar) self.layout()
def _draw_histogram(self): # In case the data for the variable were all NaNs, then the # distributions will be empty, and we don't need to display any bars if self.x.size == 0: return # In case we have a target var, but the values are all NaNs, then there # is no sense in displaying anything if self.target_var: y_nn = self.y[~np.isnan(self.y)] if y_nn.size == 0: return if self.distributions.ndim > 1: largest_bin_count = self.distributions.sum(axis=1).max() else: largest_bin_count = self.distributions.max() bar_size = self._plot_width / self.n_bins for distr, bin_colors in zip(self.distributions, self._get_colors()): bin_count = distr.sum() bar_height = bin_count / largest_bin_count * self._plot_height bar_layout = QGraphicsLinearLayout(Qt.Vertical) bar_layout.setSpacing(0) bar_layout.addStretch() self.__layout.addItem(bar_layout) bar = ProportionalBarItem( # pylint: disable=blacklisted-name distribution=distr, colors=bin_colors, height=bar_height, bar_size=bar_size, ) bar_layout.addItem(bar) self.layout()