0

I have a nested dictionary like given below.

    {'a1-b1': {'label': 'ab1',
  'x_axis': array([ 1,  6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81,
         86]),
  'y_axis_1': array([1.315, 1.29 , 1.29 , 1.295, 1.32 , 1.35 , 1.4  , 1.46 , 1.54 ,
         1.64 , 1.765, 1.93 , 2.135, 2.4  , 2.755, 3.24 , 3.955, 4.74 ]),
  'y_axis_2': array([0.18912254, 0.22628277, 0.25497109, 0.27911141, 0.30370315,
         0.32626577, 0.3506969 , 0.37465609, 0.40049521, 0.42780508,
         0.45713762, 0.49116357, 0.52798013, 0.56970844, 0.61884742,
         0.67655377, 0.74901601, 0.78038465])},  
 'a1-b2': {'label': 'ab2',
  'x_axis': array([ 1,  6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81,
         86]),
  'y_axis_1': array([1.315, 1.29 , 1.29 , 1.295, 1.32 , 1.35 , 1.4  , 1.46 , 1.54 ,
         1.64 , 1.765, 1.93 , 2.135, 2.4  , 2.755, 3.24 , 3.95 , 4.455]),
  'y_axis_2': array([0.21220464, 0.25390022, 0.2860899 , 0.31317651, 0.34076963,
         0.36608599, 0.39349889, 0.42038227, 0.44937501, 0.480018  ,
         0.51293053, 0.55110929, 0.59241926, 0.63924044, 0.69437676,
         0.75912608, 0.83936971, 0.82298074])}}

I would like to use matplotlib to plot sub-plots using both sub-dictionaries. For a1-b1 and a1-b2 I like to plot y_axis_1 against x_axis_1 and would like to use corresponding labels using label. The titles of x and y axis should also be corresponding keys, i.e. y_axis_1 and x_axis_1. Likewise, I would prefer to do a second plot which plots y_axis_2 against x_axis with corresponding label and titles. I am trying to access dictionary items using following, but I am stuck to proceed further. Thanks for your feedback in advance!

for i,outer_dict_key in enumerate(nested_dict):
    for key, value in nested_dict[outer_dict_key].items():
        keys = key
        values = value

2 Answers 2

1

Does this solve your problem?

for i in range(1, 3):
    fig, ax = plt.subplots()
    for j, outer_dict_key in enumerate(nested_dict):
        ax.plot(nested_dict[outer_dict_key]["x_axis"],
                nested_dict[outer_dict_key]["y_axis_"+str(i)],
                label=nested_dict[outer_dict_key]["label"])
    ax.legend(loc="upper left")
    ax.set_xlabel("x_axis")
    ax.set_ylabel("y_axis_"+str(i))
    plt.show()

The output is two plots (one for "y_axis_1" and the other for "y_axis_2"), with two lines in each ("ab1" and "ab2").

Sign up to request clarification or add additional context in comments.

Comments

1

If you call nested_dict your dictionary, you can do:

import matplotlib.pyplot as plt

for info in nested_dict.values():
    keys  = list(info.keys())
    vals  = list(info.values())
    plt.plot(vals[1], vals[2], label = keys[2])
    plt.plot(vals[1], vals[2], label = keys[3])
    plt.xlabel(keys[1])
    plt.title(vals[0])
    plt.legend()
    plt.show()

Should result: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.