matplotlib 从两个嵌套字典绘制条形图

mrwjdhj3  于 2023-05-23  发布在  其他
关注(0)|答案(2)|浏览(162)

下面是两个字典:

data1 = {'value-A': {'mean': 10.0, 'count': 100},
         'value-B': {'mean': 2.0, 'count': 150},
         'value-C': {'mean': 6.6, 'count': 220},
         'value-D': {'mean': 11.4, 'count': 200}}

data2 = {'value-A': {'mean': 20.0, 'count': 50},
         'value-B': {'mean': 6.0, 'count': 100},
         'value-C': {'mean': 18.6, 'count': 150},
         'value-D': {'mean': 30.4, 'count': 120}}

我有以下问题:
1.如何绘制单个字典的条形图?
1.如何绘制一个比较条形图的平均值从两个不同的字典像上面?我正在寻找类似下面的快照

hmae6n7t

hmae6n7t1#

您可以使用pyplot.bar绘制条形图。您可以查看documentation以了解更多有关它的细节,但您需要条形图的位置(在本例中,是value_x_mean变量)和条形图的高度(value_x_count)。我建议在字典中重命名你的参数,以使迭代更容易:

data = {
"value-A" : {
    "value-a-mean" : 10.0,
    "value-a-count" : 100 
},
"value-B" : {
    "value-b-mean" : 2.0,
    "value-b-count" : 150 
},
"value-C" : {
    "value-c-mean" : 6.6,
    "value-c-count" : 220 
},
"value-D" : {
    "value-d-mean" : 11.4,
    "value-d-count" : 200 
    }
} 

for key, value in data.items():
    for sub_key, sub_value in value.items():
        # When you do 'value_a_mean'.split('-') you get ['value', 'a', 'mean']
        # We take the last item from this list [-1], so just 'mean' or 'count'
        value[sub_key.split('-')[-1]] = value[sub_key]
        del value[sub_key]

现在data看起来像这样:

{
    "value-A" : {
        "mean" : 10.0,
        "count" : 100 
    },
    "value-B" : {
        "mean" : 2.0,
        "count" : 150 
    },
    "value-C" : {
        "mean" : 6.6,
        "count" : 220 
    },
    "value-D" : {
        "mean" : 11.4,
        "count" : 200 
    }
}

然后您可以执行以下操作:

from matplotlib import pyplot

data = {
    "value-A" : {
        "mean" : 10.0,
        "count" : 100 
    },
    "value-B" : {
        "mean" : 2.0,
        "count" : 150 
    },
    "value-C" : {
        "mean" : 6.6,
        "count" : 220 
    },
    "value-D" : {
        "mean" : 11.4,
        "count" : 200 
    }
} 

means, counts, labels = [], [], []

# Read the dictionary and save values
for key, value in data.items():
    labels.append(key)
    means.append(value['mean'])
    counts.append(value['count'])

# Bar plot
pyplot.bar(means, counts, tick_label=labels)
pyplot.show()

结果:

您可以为任意多个其他字典执行此操作,条形图将一起显示。您可以使用图例来标记它们:

# Random 2nd dictionary
data2 = {
    "value-A" : {
        "mean" : 20.0,
        "count" : 100 
    },
    "value-B" : {
        "mean" : 3.0,
        "count" : 150 
    },
    "value-C" : {
        "mean" : 5.6,
        "count" : 220 
    },
    "value-D" : {
        "mean" : 8.4,
        "count" : 200 
    }
} 

means, counts, labels = [], [], []

for key, value in data.items():
    means.append(value['mean'])
    counts.append(value['count'])

pyplot.bar(means, counts, label="Dataset 1")

means, counts, labels = [], [], []

for key, value in data2.items():
    means.append(value['mean'])
    counts.append(value['count'])

pyplot.bar(means, counts, label="Dataset 2")
pyplot.legend()
pyplot.show()

结果:

kyxcudwk

kyxcudwk2#

  • 给定嵌套字典或一般字典,通常使用pandas更容易,然后使用seabornpandas.DataFrame.plot进行绘图。
  • seabornmatplotlib的高级API
  • pandas.DataFrame.plot使用matplotlib作为默认后端。
  • 数据框的形状和所需的绘图应确定要使用哪个绘图API。
  • 另请参见How to plot a bar plot from a nested dictionary,它直接使用pandas.DataFrame.plot.绘图
import pandas as pd
import seaborn as sns

# create a dataframe with a unique column, data, to identify which dictionary
df = pd.concat([pd.DataFrame(d).assign(data=i) for i, d in enumerate([data1, data2], start=1)], ignore_index=False)

# reset the index to be a column and rename it
df = df.reset_index().rename({'index': 'category'}, axis=1)

# convert the dataframe to a long for to work more easily with seaborn
dfm = df.melt(id_vars=['category', 'data'])

# plot both mean and count
g = sns.catplot(data=dfm, kind='bar', x='variable', y='value', hue='data', col='category')

# plot just one category by selecting the data value for that column and then plotting
data = dfm[dfm['category'].eq('mean')]
g = sns.catplot(data=data, kind='bar', x='variable', y='value', hue='data', col='category')

df

category  value-A  value-B  value-C  value-D  data
0     mean     10.0      2.0      6.6     11.4     1
1    count    100.0    150.0    220.0    200.0     1
2     mean     20.0      6.0     18.6     30.4     2
3    count     50.0    100.0    150.0    120.0     2

dfm

category  data variable  value
0      mean     1  value-A   10.0
1     count     1  value-A  100.0
2      mean     2  value-A   20.0
3     count     2  value-A   50.0
4      mean     1  value-B    2.0
5     count     1  value-B  150.0
6      mean     2  value-B    6.0
7     count     2  value-B  100.0
8      mean     1  value-C    6.6
9     count     1  value-C  220.0
10     mean     2  value-C   18.6
11    count     2  value-C  150.0
12     mean     1  value-D   11.4
13    count     1  value-D  200.0
14     mean     2  value-D   30.4
15    count     2  value-D  120.0

相关问题