2

私はこのプロットを持っています:

data = {'User 1': [1,2,3], 'User 2': [5, 8, 10], 'User 3': [80, 75, 100], 'User 4': [65, 80, 45]}
characteristics = [('commits', 'r'), ('intended h', 'g'), ('actual h', 'b')]

n_bars = (len(characteristics)+1)*len(data)

fig = plt.figure(figsize=(5,5))
ax = fig.add_axes([0.15, 0.15, 0.65, 0.7])
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.set_yticklabels(data.keys())
ax.set_yticks(np.arange(len(characteristics)/2, n_bars, len(data)))

pers_id = 0
bar_id = 0

for name, data in data.iteritems():
    for char_id, characteristic in enumerate(characteristics):
        ax.barh(bar_id, data[char_id], facecolor=characteristic[1])
        bar_id = bar_id + 1
    ax.barh(bar_id, 100, facecolor='white', linewidth=0)
    bar_id += 1
    pers_id += 1

plt.savefig('perf.png')
plt.show()

ここに画像の説明を入力してください

私がまだ必要としている唯一の大きな機能は、右上に凡例を追加することです。ラベルはで示されcharacteristics[i][0]、色はからcharacteristics[i][0]です。

これをどのように機能させるのですか?

4

2 に答える 2

2

pandasには、そのようなデータを処理およびプロットするためのデータ構造とメソッドがいくつかあります。

In [89]: import pandas as pd

In [90]: df = pd.DataFrame(data).T

In [91]: df.columns = [c[0] for c in characteristics]

In [92]: df
Out[92]: 
        commits  intended h  actual h
User 1        1           2         3
User 2        5           8        10
User 3       80          75       100
User 4       65          80        45

In [93]: colors = [c[1] for c in characteristics]

In [94]: df.sort_index(ascending=False).plot(kind='barh', color=colors)

pandas_barh

于 2012-12-02T22:14:50.917 に答える
1
import matplotlib.pyplot as plt
import numpy as np
import collections

data = {'User 1': [1,2,3], 'User 2': [5, 8, 10], 'User 3': [80, 75, 100],
        'User 4': [65, 80, 45]}
characteristics = [('commits', 'r'), ('intended h', 'g'), ('actual h', 'b')]

n_bars = (len(characteristics)+1)*len(data)

fig = plt.figure(figsize=(5,5))
ax = fig.add_axes([0.15, 0.15, 0.65, 0.7])
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.set_yticklabels(data.keys())
ax.set_yticks(np.arange(len(characteristics)/2, n_bars, len(data)))

pers_id = 0
bar_id = 0

artists = collections.deque(maxlen = len(characteristics))
labels = collections.deque(maxlen = len(characteristics))
for name, data in data.iteritems():
    for char_id, characteristic in enumerate(characteristics):
        artist, = ax.barh(bar_id, data[char_id], facecolor=characteristic[1])
        artists.append(artist)
        labels.append(characteristic[0])
        bar_id = bar_id + 1
    ax.barh(bar_id, 100, facecolor='white', linewidth=0)
    bar_id += 1
    pers_id += 1

plt.legend(artists, labels, loc = 'best')
# plt.savefig('perf.png')
plt.show()

収量 ここに画像の説明を入力してください

于 2012-12-02T21:21:20.903 に答える