python multiple bar chart from multiple dataframes
Question:
I have 6 dataframes one for each region. I am extracting the value_counts. I want to plot a multiple bar chart. something like in the image. exept every group of bars are for each region.
each bar for region represents an event_type, y-axis represents value_count.
how can i do this. i am not able to plot muliple events together for all regions
these are the dataframes df_asia, df_africa…
df = pd.read_csv(asia)
df_asia = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(africa)
df_africa = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(europe)
df_europe = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(america)
df_america = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(latin_america)
df_latin_america = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(middle_east)
df_middle_east = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
Answers:
You can use plot.bar
from pandas after concatenate your dataframe:
import matplotlib.pyplot as plt
out = pd.concat([
df_asia.value_counts('EVENT_TYPE').rename_axis('Events').rename('Asia'),
df_africa.value_counts('EVENT_TYPE').rename_axis('Events').rename('Africa'),
df_europe.value_counts('EVENT_TYPE').rename_axis('Events').rename('Europe'),
], axis=1).T
out.plot.bar(rot=0, figsize=(12, 8), xlabel='Countries', ylabel='Cases')
plt.show()
Output
I have 6 dataframes one for each region. I am extracting the value_counts. I want to plot a multiple bar chart. something like in the image. exept every group of bars are for each region.
each bar for region represents an event_type, y-axis represents value_count.
how can i do this. i am not able to plot muliple events together for all regions
these are the dataframes df_asia, df_africa…
df = pd.read_csv(asia)
df_asia = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(africa)
df_africa = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(europe)
df_europe = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(america)
df_america = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(latin_america)
df_latin_america = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
df = pd.read_csv(middle_east)
df_middle_east = df['EVENT_TYPE'].value_counts().rename_axis('events').reset_index(name='counts')
You can use plot.bar
from pandas after concatenate your dataframe:
import matplotlib.pyplot as plt
out = pd.concat([
df_asia.value_counts('EVENT_TYPE').rename_axis('Events').rename('Asia'),
df_africa.value_counts('EVENT_TYPE').rename_axis('Events').rename('Africa'),
df_europe.value_counts('EVENT_TYPE').rename_axis('Events').rename('Europe'),
], axis=1).T
out.plot.bar(rot=0, figsize=(12, 8), xlabel='Countries', ylabel='Cases')
plt.show()
Output