如何在 matplotlib 中交替堆积条形图中的颜色?

发布于 2024-09-13 04:51:40 字数 820 浏览 7 评论 0原文

我想在 matplotlib 中的堆叠条形图中交替颜色......这样我就可以根据图表的类型获得不同的颜色。我有两种类型,只不过它们不定期交替。所以我需要在选择颜色之前检查它们的类型。

问题是它是有条件的。我在数组中提供类型,但无法在 plt.bar(........................) 级别上执行此操作......我认为。

p1 = plt.bar(self.__ind,
                    self.__a,
                    self.__width, 
                    color='#263F6A')

p2 = plt.bar(self.__ind,
                    self.__b,
                    self.__width, 
                    color='#3F9AC9',
                    bottom = self.__arch)

p3 = plt.bar(self.__ind,
                    self.__c,
                    self.__width, 
                    color='#76787A',
                    bottom = self.__a + self.__b)

self.__a 、 self.__b 和 self.__c 都是我需要在同一个图中绘制的数据列表,并且对于上面提到的列表中的每个元素,我都有另一个类型列表。 我只想知道如何才能根据类型列表提供的类型来更改图表的颜色,同时将所有条形保留在一个图中。

I want to alternate colors in a stacked bar graph in matplotlib..so as I can get different colors for the graphs depending on their type. I have two types except that they do not alternate regularly. so I need to check their type before I choose the colors.

The problem is that it is conditional. I provide the type in an array, but there is no way to do it at the level of the plt.bar(.............)...well I think.

p1 = plt.bar(self.__ind,
                    self.__a,
                    self.__width, 
                    color='#263F6A')

p2 = plt.bar(self.__ind,
                    self.__b,
                    self.__width, 
                    color='#3F9AC9',
                    bottom = self.__arch)

p3 = plt.bar(self.__ind,
                    self.__c,
                    self.__width, 
                    color='#76787A',
                    bottom = self.__a + self.__b)

self.__a and self.__b and self.__c are all data lists that I need to plot in the same figure and I have another list of types for each element of the mentioned lists above.
I only want to know how could I make it possible to change the colors of the graphs depending on the type provided by the types list and at the same time keeping all the bars in one plot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2024-09-20 04:51:40

当你说 self.__a 是一个列表时,我很困惑 - 当我尝试绘制列表时:

In [19]: plt.bar(1,[1,2,3], 0.1, color='#ffcc00')

我得到了

AssertionError: incompatible sizes: argument 'height' must be length 1 or scalar

但是,你可以做的是在循环中绘制你的值:

# Setup code here...

indices = [1,2,3,4]
heights = [1.2, 2.2, 3.3, 4.4]
widths = [0.1, 0.1, 0.2, 1]
types = ['spam', 'rabbit', 'spam', 'grail']


for index, height, width, type in zip(indices, heights, widths, types):
    if type == 'spam':
        plt.bar(index, height, width, color='#263F6A')
    elif type == 'rabbit':
        plt.bar(index, height, width, color='#3F9AC9', bottom = self.__arch)
    elif type == 'grail':
        plt.bar(index, height, width, color='#76787a', bottom = 3)

I'm confused when you say that self.__a is a list - when I try plotting a list:

In [19]: plt.bar(1,[1,2,3], 0.1, color='#ffcc00')

I get

AssertionError: incompatible sizes: argument 'height' must be length 1 or scalar

However, what you can do is plot your values in a loop:

# Setup code here...

indices = [1,2,3,4]
heights = [1.2, 2.2, 3.3, 4.4]
widths = [0.1, 0.1, 0.2, 1]
types = ['spam', 'rabbit', 'spam', 'grail']


for index, height, width, type in zip(indices, heights, widths, types):
    if type == 'spam':
        plt.bar(index, height, width, color='#263F6A')
    elif type == 'rabbit':
        plt.bar(index, height, width, color='#3F9AC9', bottom = self.__arch)
    elif type == 'grail':
        plt.bar(index, height, width, color='#76787a', bottom = 3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文