1

グラフの種類に応じて異なる色を取得できるように、matplotlib の積み上げ棒グラフの色を交互に変更したいと考えています。定期的に入れ替わらない以外は2種類持っています。そのため、色を選択する前にタイプを確認する必要があります。

問題は、それが条件付きであることです。私は配列でタイプを提供しますが、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 はすべて、同じ図にプロットする必要があるデータ リストであり、上記のリストの各要素の型の別のリストがあります。タイプリストによって提供されるタイプに応じてグラフの色を変更し、同時にすべてのバーを1つのプロットに保持できるようにする方法を知りたいだけです。

4

1 に答える 1

5

それがリストだとあなたが言うとき、私は混乱します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)
于 2010-08-05T15:19:24.407 に答える