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.
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.
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.
You can convert a dictionary to Pandas Dataframe using df = pd. DataFrame. from_dict(my_dict) statement.
I'm not sure whether I understand what result you want but:
for k in d:
ax = d[k].plot()
gives:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With