当键不统一时,如何在不使用Pandas的情况下从嵌套字典绘制条形图? [英] How to plot a bar graph from nested dictionary without using Pandas when keys are not uniform?
问题描述
我有一个像这样的嵌套字典:
I have a nested dictionary like this:
{'uvvm': {'0-250ms': 96, '500-750ms': 2}, 'usvv': {'0-250ms': 1},
'unsharedChunksVirtualVolume': {'0-250ms': 21}, 'ubvv': {'0-250ms': 60, '250-500ms': 2, '500-750ms': 3},
'unbvvh': {'0-250ms': 1}, 'ssvv': {'0-250ms': 101, '250-500ms': 1},
'sscc': {'0-250ms': 2}, 'sc': {'500-750ms': 2},
'qvv': {'0-250ms': 200, '500-750ms': 5}, 'qas': {'0-250ms': 4}}
内部键 0-250
, 250-500
, 500-750
等对于所有外部键都不相同.我需要绘制不使用熊猫的条形图,因为我们需要将其作为支持包的一部分进行收集,并且不能在客户计算机中安装 pandas
.请提供一种方法.
The inner keys 0-250
, 250-500
, 500-750
etc., are not uniform for all outer keys. I need to plot a bar graph without using pandas as we need to collect as part of support bundle and cant have pandas
installed in customer machines. Please provide a way.
op = dict(list(dictLatency.items())[i:i+10])
print(op)
inner_keys = ['0-250ms', '250-500ms', '500-750ms', '750-1000ms', '1000-1250ms',
'1250-1500ms', '1500-1750ms', '1750-2000ms', '2000-2250ms']
newDict = a:[[i, b.get(i, 0)] for i in inner_keys] for a, b in op.items()
print(newDict)
labels = op.keys()
此处 newDict
是
{'updateVirtualVolumeMetaData': [['0-250ms', 96], ['250-500ms', 0], ['500-750ms', 2], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]], 'updateStorageProfileForVirtualVolume': [['0-250ms', 1], ['250-500ms', 0], ['500-750ms', 0], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]], 'unsharedChunksVirtualVolume': [['0-250ms', 21], ['250-500ms', 0], ['500-750ms', 0], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]], 'unbindVirtualVolume': [['0-250ms', 60], ['250-500ms', 2], ['500-750ms', 3], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]], 'unbindAllVirtualVolumesFromHost': [['0-250ms', 1], ['250-500ms', 0], ['500-750ms', 0], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]], 'spaceStatsForVirtualVolume': [['0-250ms', 101], ['250-500ms', 1], ['500-750ms', 0], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]], 'setStorageContainerContext': [['0-250ms', 2], ['250-500ms', 0], ['500-750ms', 0], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]], 'setContext': [['0-250ms', 0], ['250-500ms', 0], ['500-750ms', 2], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]], 'queryVirtualVolume': [['0-250ms', 200], ['250-500ms', 0], ['500-750ms', 5], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]], 'queryArrays': [['0-250ms', 4], ['250-500ms', 0], ['500-750ms', 0], ['750-1000ms', 0], ['1000-1250ms', 0], ['1250-1500ms', 0], ['1500-1750ms', 0], ['1750-2000ms', 0], ['2000-2250ms', 0]]}
它在列表 []
中显示值,而不是字典 {}
中的值.我希望将内键和值作为dict而不是列表.
It is showing values in list []
and not as dictionary {}
.
I'd like the inner key and values as dict not as list.
推荐答案
您可以遍历外部词典,然后遍历内部词典绘制堆叠的条形图:
You could loop through the outer dictionary, and then through the inner one to draw a stacked bar graph:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
dictLatency = {'uvvm': {'0-250ms': 96, '500-750ms': 2}, 'usvv': {'0-250ms': 1},
'unsharedChunksVirtualVolume': {'0-250ms': 21}, 'ubvv': {'0-250ms': 60, '250-500ms': 2, '500-750ms': 3},
'unbvvh': {'0-250ms': 1}, 'ssvv': {'0-250ms': 101, '250-500ms': 1},
'sscc': {'0-250ms': 2}, 'sc': {'500-750ms': 2},
'qvv': {'0-250ms': 200, '500-750ms': 5}, 'qas': {'0-250ms': 4}}
inner_keys = ['0-250ms', '250-500ms', '500-750ms', '750-1000ms', '1000-1250ms',
'1250-1500ms', '1500-1750ms', '1750-2000ms', '2000-2250ms']
# colors = plt.cm.Set3.colors
colors = plt.cm.hsv(np.linspace(0, 0.8, len(inner_keys)))
fig, ax = plt.subplots(figsize=(15, 4))
for outerkey, innerdict in dictLatency.items():
bottom = 0
if outerkey == 'unsharedChunksVirtualVolume':
outerkey = 'unshared\nChunks\nVirtual\nVolume'
for innerkey, value in innerdict.items():
ax.bar([outerkey], [value], bottom=bottom, color=colors[inner_keys.index(innerkey)])
bottom += value
ax.tick_params(axis='x', rotation=90)
legend_handles = [Rectangle((0, 0), 0, 0, color=color, label=label) for color, label in zip(colors, inner_keys)]
ax.legend(handles=legend_handles)
plt.tight_layout()
plt.show()
PS:对于单杠,您可以将对 ax.bar
的调用更改为:
PS: For horizontal bars you could change the call to ax.bar
to:
ax.barh(y=outerkey, width=value, left=bottom, color=colors[inner_keys.index(innerkey)])
这篇关于当键不统一时,如何在不使用Pandas的情况下从嵌套字典绘制条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!