Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多维数据和绘图图例_Python_Matplotlib - Fatal编程技术网

Python 多维数据和绘图图例

Python 多维数据和绘图图例,python,matplotlib,Python,Matplotlib,我有这样一个情节: 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(fi

我有这样一个情节:

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]

我怎样才能让它工作

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()
屈服 有一些数据结构和方法来处理和绘制此类数据:

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)

显然,图例必须位于数据绘制矩形之外。。。如何实现这一点?据我所知,matplotlib总是在“数据绘制矩形”内绘制图例。如果它覆盖了您的一些数据,您可以增加
xlim
,因此图例位于最大值的右侧。@Flavius:为什么这很明显@unutbu:我认为您可以使用
leg=ax.legend(艺术家、标签、loc=2、bbox\u to\u锚=(1.05、1.0));fig.savefig('perf.png',bbox_extra_artists=(leg,),bbox_inches='tight')
来完成它。@DSM:谢谢您提供的信息!它确实会将图例移到轴之外。
fig.savefig
命令为我引发了一个错误,但这可能是因为我的matplotlib版本不够新。