私は大きなcsvファイルを持っています。このファイルは読み込まれ、定義済みの行ごとにCounter
が返されます。例として:matplotlibを使用しているPython Stacked Barchartからのカウンタ
counter = [Counter({(0, 1): 9, (1, 2): 8}), Counter({(1, 2): 99, (0, 1): 99}), Counter({(1, 2): 256, (0, 1): 189}), Counter({(1, 5): 473, (0, 1): 301})]
これは私が使用するスクリプトです。
import matplotlib.pyplot as plt
import numpy
from collections import Counter
counter = [Counter({(0, 1): 9, (1, 2): 8}), Counter({(1, 2): 99, (0, 1): 99}), Counter({(1, 2): 256, (0, 1): 189}), Counter({(1, 5): 473, (0, 1): 301})]
fig = plt.figure()
ax1 = fig.add_subplot(111)
N = len(counter)
ind = numpy.arange(N)
j = 0
while j in range(0, len(counter)):
a, i = 0, 0
frequencies = counter[j].values()
names = counter[j].keys()
while i in range(0, len(frequencies)):
if i == 0:
ax1.bar(ind, frequencies[i], label=names[i], width=0.25)
a = frequencies[i]
else:
ax1.bar(ind, frequencies[i], label=names[i], width=0.25, bottom=a)
a += frequencies[i]
i += 1
j += 1
labels = ["%s to %s" % (200, 202)]
ax1.set_xticks(numpy.arange(N))
ax1.set_xticklabels(labels)
ax1.set_ylabel("Frequency")
ax1.set_xlabel("Material Contact")
ax1.legend()
plt. show()
しかし、それはエラーメッセージとして返されます:私は信じて
ValueError: incompatible sizes: argument 'height' must be length 4 or scalar
はind
配列に関連しています。
これを解決するために、if文でind
をind[j]
に変更しました。しかし、最終的には多くの色のバーがたくさんあります。色はそれぞれのビンには関係ありません。
ax1.bar(ind[j], frequencies[i], label=names[i], width=0.25)
更新: が可能な解決策は、カウンターから配列を構築することができます。しかし、これは最初に実装されたカウンターの概念に反します。