Repository: ManimCommunity/manim
File: manim/mobject/graphing/probability.py (Python)

Code before:

        bar = Rectangle(
            height=bar_h,
            width=bar_w,
            stroke_width=self.bar_stroke_width,
            fill_opacity=self.bar_fill_opacity,
        )

        pos = UP if (value >= 0) else DOWN
        bar.next_to(self.c2p(bar_number + 0.5, 0), pos, buff=0)
        return bar

    def _add_bars(self) -> None:
        self.bars = VGroup()

        for i, value in enumerate(self.values):
            temp_bar = self._create_bar(bar_number=i, value=value)
            self.bars.add(temp_bar)

        self._update_colors()
        self.add_to_back(self.bars)

    def get_bar_labels(
        self,
        color: Color | None = None,
        font_size: float = 24,
        buff: float = MED_SMALL_BUFF,
        label_constructor: VMobject = Tex,
    ):
        """Annotates each bar with its corresponding value. Use ``self.bar_labels`` to access the
        labels after creation.

        Parameters
        ----------
        color
            The color of each label. By default ``None`` and is based on the parent's bar color.
        font_size
            The font size of each label.
        buff
            The distance from each label to its bar. By default 0.4.
        label_constructor
            The Mobject class to construct the labels, by default :class:`~.Tex`.

        Examples
        --------
        .. manim:: GetBarLabelsExample
            :save_last_frame:

            class GetBarLabelsExample(Scene):
                def construct(self):
                    chart = BarChart(values=[10, 9, 8, 7, 6, 5, 4, 3, 2, 1], y_range=[0, 10, 1])

Review comment (bug):
Well, it isn't really changed by this PR but the typing for this constructor is wrong for `label_constructor` it should be `Type[VMobject]` instead of `VMobject` see https://docs.python.org/3/library/typing.html#typing.Type

Code after:

        # distance from between the y-axis and the top of the bar
        bar_h = abs(self.c2p(0, value)[1] - self.c2p(0, 0)[1])
        # width of the bar
        bar_w = self.c2p(self.bar_width, 0)[0] - self.c2p(0, 0)[0]

        bar = Rectangle(
            height=bar_h,
            width=bar_w,
            stroke_width=self.bar_stroke_width,
            fill_opacity=self.bar_fill_opacity,
        )

        pos = UP if (value >= 0) else DOWN
        bar.next_to(self.c2p(bar_number + 0.5, 0), pos, buff=0)
        return bar

    def _add_bars(self) -> None:
        for i, value in enumerate(self.values):
            tmp_bar = self._create_bar(bar_number=i, value=value)
            self.bars.add(tmp_bar)

        self._update_colors()
        self.add_to_back(self.bars)

    def get_bar_labels(
        self,
        color: Color | None = None,
        font_size: float = 24,
        buff: float = MED_SMALL_BUFF,
        label_constructor: type[VMobject] = Tex,
    ):
        """Annotates each bar with its corresponding value. Use ``self.bar_labels`` to access the
        labels after creation.

        Parameters
        ----------
        color
            The color of each label. By default ``None`` and is based on the parent's bar color.
        font_size
            The font size of each label.
        buff
            The distance from each label to its bar. By default 0.4.
        label_constructor
            The Mobject class to construct the labels, by default :class:`~.Tex`.

        Examples
        --------
        .. manim:: GetBarLabelsExample
            :save_last_frame: