Home > Net >  Key Error Trying to plot multiple bar charts from dataframe
Key Error Trying to plot multiple bar charts from dataframe

Time:12-22

Reason why I am loading the df from the .csv is because another file creates the csv and then this file will access it (maybe this is an issue? not sure)

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('MAIN_DATAFRAME.csv')

def plot_graph_1(MAIN_DATAFRAME):
        df1 = MAIN_DATAFRAME.loc[['Bots']]
        df1 = df1.transpose()
        df2 = MAIN_DATAFRAME.loc[['Speed']]
        df2 = df2.transpose()
        df3 = MAIN_DATAFRAME.loc[['Weight']]
        df3 = df3.transpose()
        df4 = MAIN_DATAFRAME.loc[['Chargers']]
        df4 = df4.transpose()


        ax = df1.plot(kind='bar')
        df2.plot(ax=ax, kind='bar')
        df3.plot(ax=ax,kind='bar')
        df4.plot(ax=ax, kind='bar')
        ax.bar(ax, df1)
        plt.show()


plot_graph_1(df)

So I would like to have this Dataframe be plotted and ideally the bar charts will share axis and be different collors so that they can be distinguised when stacked on each other.

btw here is the dataframe:

Run 1 Run 2 Run 3 Run 4 Run 5 Run 6 Run 7 Run 8 Run 9 Run 10
Bots 5 6 7 8 9 10 11 12 13 14
Speed 1791 2359 2996 3593 4105 4551 4631 4656 4672 4674
Weight 612 733 810 888 978 1059 1079 1085 1090 1092
Chargers 10 10 10 10 10 10 10 10 10 10

I tried changing how I access the dataframe values. I also tried changing brackets from: df2 = MAIN_DATAFRAME.loc[['Speed']] to df2 = MAIN_DATAFRAME.loc['Speed'] and still get a key error.

CodePudding user response:

These are two separate approaches with different result:

First approach:

import pandas as pd
import matplotlib.pyplot as plt


df = pd.read_csv('MAIN_DATAFRAME.csv')

# Create a figure with 2 rows and 2 columns of subplots
fig, axs = plt.subplots(nrows=2, ncols=2)

# Extract the data for each dataframe
df1 = df[['Bots']]
df2 = df[['Speed']]
df3 = df[['Weight']]
df4 = df[['Chargers']]

# Plot the first dataframe in the first subplot
axs[0, 0].bar(df1.index, df1['Bots'], color='red')
axs[0, 0].legend(['Bots'])

# Plot the second dataframe in the second subplot
axs[0, 1].bar(df2.index, df2['Speed'], color='blue')
axs[0, 1].legend(['Speed'])

# Plot the third dataframe in the third subplot
axs[1, 0].bar(df3.index, df3['Weight'], color='green')
axs[1, 0].legend(['Weight'])

# Plot the fourth dataframe in the fourth subplot
axs[1, 1].bar(df4.index, df4['Chargers'], color='yellow')
axs[1, 1].legend(['Chargers'])

plt.show()

Output:

enter image description here

Second approach:

import pandas as pd
import matplotlib.pyplot as plt

# Load the data from the CSV file
df = pd.read_csv('MAIN_DATAFRAME.csv')

# Extract the data for each column
bots = df['Bots']
speed = df['Speed']
weight = df['Weight']
chargers = df['Chargers']

# Create a figure and axis
fig, ax = plt.subplots()

# Plot the first column as a stacked bar chart
ax.bar(df.index, bots, color='red', bottom=speed weight chargers)
ax.bar(df.index, speed, color='blue', bottom=weight chargers)
ax.bar(df.index, weight, color='green', bottom=chargers)
ax.bar(df.index, chargers, color='yellow')

# Add a legend
ax.legend(['Bots', 'Speed', 'Weight', 'Chargers'])

plt.show()

Output:

enter image description here

  • Related