简体   繁体   中英

How to iterate over multiple arguments (6 dictionaries of data sets) for histogram subplots

I am attempting to plot 7 subplots (there are 7 categories the data is sorted into) with multiple data sets. My function currently works for one set of data and I have attempted to iterate over args*. When I do this it appears that both sets of data are not being added. How can I iterate over each arg/dictionary (data set values) and dump them into each of the 7 plots with different colors?

def plot_histo(*args,num_bins):
    #print(dic_data)
    fig = plt.figure()
    fig, axes = plt.subplots(2, 4, figsize=(14,6))
    axes = axes.ravel()
    for arg in args:
        for i, (key, value) in enumerate(arg.items()):
            #print(i, key, value)
            axes[i].hist(value,num_bins,stacked= True, alpha=0.5)
            axes[i].set(title=key.upper(), xlabel='R-Resistance',ylabel='N')
            #axes[i].legend()
        plt.tight_layout()
        plt.savefig('wafer_probe_results.png')
        plt.show()
p_histo = plot_histo(w21_results,w22_results,num_bins=10)   

Note the args are dictionaries where I only care about the value. The code for a single set of data is as follows.

def plot_histo(dic_data,num_bins):
    #print(dic_data)
    fig = plt.figure()
    fig, axes = plt.subplots(2, 4, figsize=(14,6))
    axes = axes.ravel()
        for i, (key, value) in enumerate(dic_data.items()):
            #print(i, key, value)
            axes[i].hist(value,num_bins,stacked= True, alpha=0.5)
            axes[i].set(title=key.upper(), xlabel='R-Resistance',ylabel='N')
            #axes[i].legend()
        plt.tight_layout()
        plt.savefig('wafer_probe_results.png')
        plt.show()   
p_histo = plot_histo(w21_results,num_bins=10)

Where dic_data is the dictionary:

dic_data ={'CPW': array([4.2, 4.1, 4.3, 4.3, 4.2, 4.3, 4.1, 4.2, 4.2, 4.1, 4.2, 4.2, 4. ,
       4.1, 4.3, 4.1, 4.1, 4.4, 4.1, 4.2, 4.3, 4.1, 4.1, 4.1, 4.2, 4.2,
       4.4, 4.2]),'CPW to Ground': array([33333., 99960., 99900., 33323., 99950., 99900., 99990., 99950.,
       99890., 99990., 99930., 99900., 99990., 99930., 99890., 49980.,
       99940., 99890., 99960., 99930., 99890., 99910., 99910., 99900.,
       99910., 99900., 99890., 99890.]),etc...}

I am really close, any help or tips are appreciated. 子图

Iterate over keys first then dicts so you can extract the related values from each dict. Then use the method shown in this answer to plot multiple histograms. (apologies for errors, I don't have numpy or matplotlib to test this solution)

# relavent imports

d = dict(zip('abc',[[1,1,1],[2,2,2],[3,3,3]]))
e = dict(zip('abc',[[11,11,11],[22,22,22],[33,33,33]]))
datasets = (d,e)
dataset_names = [str(n) for n in range(len(datasets))]
bins = 10

fig = plt.figure()
fig, axes = plt.subplots(2, 4, figsize=(14,6))
axes = axes.ravel()

for index,key in enumerate(d):
    values = [dictionary[key] for dictionary in datasets]
    axes[index].hist(values, bins, label=dataset_names)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM