比较来自2个嵌套词典的数据并产生箱形图

时间:2020-09-10 10:38:15

标签: python dictionary matplotlib nested boxplot

我正在尝试使用matplotlib和嵌套字典中的数据生成箱形图。下面是有关字典结构的粗略概述。

m_data = {scenario:{variable:{'model_name':value, ''model_name':value ...}

一个问题是,我想查看两种不同方案(方案1 [VAR1]-方案2 [VAR2])之间模型输出的变化,然后将这种差异绘制在箱形图中。

我设法做到了,但是,我希望能够用模型名称标记异常值。我当前的方法将键与值分开,因此异常数据点不再具有与之关联的名称。

    #BOXPLOT
#set up blank lists
future_rain = []
past_rain = []
future_temp = []
past_temp = []

#single out the values for each model from the nested dictioaries
for key,val in m_data[FUTURE_SCENARIO][VAR1].items():
    future_rain.append(val)
for key,val in m_data[FUTURE_SCENARIO][VAR2].items():
    future_temp.append(val)
for key,val in m_data['historical'][VAR1].items():
   past_rain.append(val)
for key,val in m_data['historical'][VAR2].items():
    past_temp.append(val)

#blanks for final data
bx_plt_rain = []
bx_plt_temp = []

#allow for the subtration of two lists
zip_object = zip(future_temp, past_temp)
for future_temp_i, past_temp_i in zip_object:
    bx_plt_temp.append(future_temp_i - past_temp_i)

zip_object = zip(future_rain, past_rain)
for future_rain_i, past_rain_i in zip_object:
    bx_plt_rain.append(future_rain_i - past_rain_i)

#colour ouliers red

c = 'red'

outlier_col = {'flierprops': dict(color =c, markeredgecolor=c)}

#plot

bp = plt.boxplot(bx_plt_rain, patch_artist=True, showmeans=True, vert= False, meanline=True, **outlier_col)
bp['boxes'][0].set(facecolor = 'lightgrey')
plt.show()

如果有人知道解决方法,我将非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以创建一个函数,该函数通过dict查找异常值并返回键。

def outlier_name(outlier_val, inner_dict):
    for key, value in inner_dict.items():
        if value == outlier_val:
            return key

如果您的数据集很大,这可能会非常密集。