Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting a pandas dataframe row by row

I have the following dataframe:

enter image description here

I want to create pie charts one for each row, the thing is that i am having trouble with the charts order, i want each chart to have a figsize of lets say 5,5 and that every row in my dataframe will be a row of plot in my subplots with the index as title.

tried many combinations and playing with pyploy.subplots but not success. would be glad for some help.

Thanks

like image 226
Shak Avatar asked Mar 08 '23 08:03

Shak


1 Answers

You can either transpose your dataframe and using pandas pie kind for plotting, i.e. df.transpose().plot(kind='pie', subplots=True) or iterate through rows while sub plotting.

An example using subplots:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Recreate a similar dataframe
rows = ['rows {}'.format(i) for i in range(5)]
columns = ['hits', 'misses']
col1 = np.random.random(5)
col2 = 1 - col1
data = zip(col1, col2)

df = pd.DataFrame(data=data, index=rows, columns=columns)

# Plotting

fig = plt.figure(figsize=(15,10))

for i, (name, row) in enumerate(df.iterrows()):
    ax = plt.subplot(2,3, i+1)
    ax.set_title(row.name)
    ax.set_aspect('equal')
    ax.pie(row, labels=row.index)

plt.show()

piecharts

like image 179
Delforge Avatar answered Mar 10 '23 06:03

Delforge