How do you take a pandas DataFrame of data (dataset) and produce a multi-column bar chart? You can manipulate the DataFrame with groupby first to aggregate the data. Then you can use matplotlib and seaborn to plot the data.
To do this we’ll work with an example. We have a list of companies that belong to two different industries and two different regions. Here I create a DataFrame manually and call it df. When we group the data, we’ll create a new DataFrame and call it df_2. We’ll use that DataFrame to plot the two bar charts.
plt.title('Sales by Industry and Geographic Region')
8
plt.xlabel('Industry')
9
plt.ylabel('Sales in USD')
10
plt.legend(title='Region',loc='upper left')
11
plt.plot()
12
plt.show()
Once you have the DataFrame (df2) prepared, you can build two types of graphs. Below is the second one. Notice that we’ve switched the legend and the x-axis.