glitchb3rt
glitchb3rt

Reputation: 23

Two-bar plotting from dictionary

I am not very experienced with python3 as I am still learning so I apology in advance if what I ask is pretty obvious. So I have two files (.xlsx and .csv) with some data listed in different DataFrames.

exp=pd.read_excel('filepath/something.xlsx', keep_default_na=False)
sim=pd.read_csv('filepath/something.csv', keep_default_na=False)

Which look like these:

print(exp.dataframe_from_exp1)

0     2.2
1     1.0
2     2.0
3     2.7
4     3.5
5     1.3
6     2.2
7     3.0
8     2.1
9     1.7
10    2.5
Name: dataframe_from_exp1, dtype: float64

print(sim[[dataframe_from_sim1]])
        dataframe_from_sim1
0                   2.1
1                   1.3
2                   2.4
3                   2.9
4                   2.4
5                   2.8
6                   2.8
7                   3.6
8                   2.2
9                   2.7
10                  2.5

Then I arrange them in a dictionary by using their names:

comparison = {}
comparison['dataframe_from_sim1'] = 'dataframe_from_exp1' 
comparison['dataframe_from_sim2'] = 'dataframe_from_exp2'  

And so on. Then, what I want is to plot each dataframe_from_sim with its corresponding dataframe_from_exp in a double-bar plot so I can compare the values from each variable in each of the 11 cases given and do it inside a for loop. Something like this:

N=11
x = np.arange(N)
width = 0.35
for key, value in comparison.items():
    fig, ax = plt.subplots()
    bar1=ax.bar(x, key, width, color='r')
    bar2=ax.bar(x + width, value, width, color='b')
    plt.show()

But of course, this doesn't work. So if you could help me I would really appreciate it!

Upvotes: 1

Views: 714

Answers (1)

Martin Evans
Martin Evans

Reputation: 46759

If you are trying to see these values side by side, the following will work:

import matplotlib.pyplot as plt    
import pandas as pd

fig = plt.figure()

exp = pd.read_excel('filepath/something.xlsx', keep_default_na=False)
sim = pd.read_csv('filepath/something.csv', keep_default_na=False)    

exp.plot.bar(color='red', ax=fig.gca(), position=0, width=0.3)
sim.plot.bar(color='blue', ax=fig.gca(), position=1, width=0.3)

plt.show()

Giving you:

side by side bar plots

This could be extended to work on multiple pairs of files as follows:

import matplotlib.pyplot as plt    
import pandas as pd
import glob

for xlsx_filename in glob.glob('*.xlsx'):
    name, ext = os.path.splitext(xlsx_filename)
    csv_filename = '{}.csv'.format(name)

    if os.path.exists(csv_filename):
        exp = pd.read_csv(csv_filename, keep_default_na=False)
        sim = pd.read_excel(xlsx_filename, keep_default_na=False)    

        fig = plt.figure(name)
        exp.plot.bar(color='red', ax=fig.gca(), position=0, width=0.3)
        sim.plot.bar(color='blue', ax=fig.gca(), position=1, width=0.3)
        plt.title(name)

plt.show()

This searches for pairs of .xlsx and .csv filenames. Only files with a matching pair are processed.

Upvotes: 1

Related Questions