Plot a Dictionary of Dataframes

Question:

I have a dictionary of dataframes (Di):

Di = {}
groups = [2,3]
for grp in groups:
    df = pd.DataFrame({'A' : (grp*2, grp*3, grp*4),
                       'B' : (grp*4, grp*5, grp*2)})
    Di[grp] = df

For each df in Di, I would like to plot A against B in a single graph.
I tried:

for grp in groups:
    ax1 = Di[grp].plot(x='A', y='B')

But that gave me two graphs:

enter image description here
enter image description here

How do I get them both in the same graph please?

Asked By: R. Cox

||

Answers:

You should print on a same ax:

fig, ax = plt.subplots(figsize=(5,8))

for grp in groups:
    Di[grp].plot(x='A', y='B',ax=ax)
Answered By: Mehdi Golzadeh

An alternative answer that also loops round but that uses matplotlib instead of pandas:

fig, ax1 = plt.subplots(1,1)
for grp in groups:
    ax1.plot(Di[grp]['A'], Di[grp]['B'], label=grp)

enter image description here

Answered By: R. Cox