Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a dictionary of DataFrames

I have a dictionary of DataFrames that I want to use for graphing. This is an example of the data:

import pandas as pd
import numpy as np

np.random.seed(5)
first = pd.DataFrame(columns=range(1,4),data=np.random.rand(3,3), index=range(1,4))
second = pd.DataFrame(columns=range(1,4),data=np.random.rand(3,3)+0.2, index=range(1,4))
third = pd.DataFrame(columns=range(1,4),data=np.random.rand(3,3)+0.05, index=range(1,4))
fourth= pd.DataFrame(columns=range(1,4),data=np.random.rand(3,3)-0.2, index=range(1,4))

d={'frame_1':first,'frame_2':second,'frame_3':third,'frame_4':fourth}

#This is what it looks like
d
    {'frame_1':           1         2         3
 1  0.221993  0.870732  0.206719
 2  0.918611  0.488411  0.611744
 3  0.765908  0.518418  0.296801, 'frame_2':           1         2         3
 1  0.387721  0.280741  0.938440
 2  0.641309  0.358310  1.079937
 3  0.474086  0.614235  0.496080, 'frame_3':           1         2         3
 1  0.678788  0.629838  0.649929
 2  0.315819  0.334686  0.303588
 3  0.377564  0.194164  0.215613, 'frame_4':           1         2         3
 1  0.763931  0.760227 -0.011585
 2 -0.175693  0.004556  0.499844
 3  0.579515 -0.177067  0.377663}

Ultimately, I want 9 separate graphs which plot the evolution of each element combination in the DataFrame (11,12,13,..) with 1,2,3,4 (relating to 'frame_1', 'frame_2', etc.) as the x-axis. To be clear, this means that the first graph (relating to 11) should contain the values 0.221993, 0.387721, 0.678788 and 0.763931.

Normally I always explain what I've tried to solve the problem myself. However, in this case I haven't got a clue about a possible solution..

Note: the actual dataset are 10x10 DataFrames and the dictionary contains 75 such DataFrames so I would prefer as much automation as possible.

like image 299
Pilik Avatar asked Oct 16 '15 09:10

Pilik


People also ask

Can Matplotlib plot dictionary?

Plotting a Matplotlib bar chart using a dictionary plots the value in each key-value pair as a bar with the associated key displayed as the label.

Can you store Dataframes in dictionary?

A pandas DataFrame can be converted into a Python dictionary using the DataFrame instance method to_dict(). The output can be specified of various orientations using the parameter orient. In dictionary orientation, for each column of the DataFrame the column value is listed against the row label in a dictionary.

How do you create a dictionary DataFrame?

You can convert a dictionary to Pandas Dataframe using df = pd. DataFrame. from_dict(my_dict) statement.


1 Answers

I'm not sure whether I understand what result you want but:

for k in d:
    ax = d[k].plot()

gives:

enter image description here enter image description here enter image description here

like image 136
R. Cox Avatar answered Sep 28 '22 07:09

R. Cox