# Mastering Multiple Column Bar Chart Visualization with Python

## Plotting Multiple Columns on a Bar Chart

Data analysis is a crucial part of any business or scientific pursuit, and being able to effectively visualize data is just as important. A bar chart is a classic and straightforward way to represent data by plotting categorical data against numerical data.

However, it can sometimes be challenging to display multiple columns on a single bar chart. In this article, we will explore how to plot multiple columns on a bar chart using Python’s pandas DataFrame and matplotlib libraries.

## Creating Fake Data

Before diving into plotting multiple columns on a bar chart, let’s first create some fake data to work with.

We can use the pandas DataFrame to create a table of data, similar to a spreadsheet.

For this example, we will create a DataFrame of the total sales made by three different salespeople in three different months.

First, we will import pandas and numpy libraries:

``````import pandas as pd
import numpy as np
``````

Next, we create a dictionary containing the sales data for each salesperson:

``````sales_data = {'Salesperson A': [3500, 4800, 3750],
'Salesperson B': [2900, 4200, 6600],
'Salesperson C': [4200, 3500, 2400]}
``````

Then, we can use pandas.DataFrame function to create the DataFrame:

``````df = pd.DataFrame(sales_data, index=['January', 'February', 'March'])
``````

## Plotting Columns on a Bar Chart

Now that we have our data, we can plot the total sales made by each individual salesperson on a bar chart.

We can use pandas.DataFrame.plot.bar function with the ‘stacked’ parameter set as False to plot the sales data.

The ‘width’ parameter can be used to adjust the width of each bar.

``````ax = df.plot.bar(width=0.8, figsize=(10,6), title='Total Sales by Salesperson and Month', rot=0)
ax.set_xlabel('Month')
ax.set_ylabel('Total Sales')
``````

This code will create a bar chart with three different bars representing the sales data for each salesperson, with three different colors to differentiate them.

## Creating a Stacked Bar Chart

A stacked bar chart allows us to display the total sales made by each salesperson in each month also stacked by month. We can create this chart by setting the ‘stacked’ parameter to True.

``````ax = df.plot.bar(stacked=True, width=0.8, figsize=(10,6), title='Total Sales by Salesperson and Month', rot=0)
ax.set_xlabel('Month')
ax.set_ylabel('Total Sales')
``````

This code creates a stacked bar chart with three bars representing the sales data for each salesperson in each month, with the bars stacked on top of each other.

## Changing Colors of the Bars

We can change the colors of the bars to make them more visually appealing and easier to interpret. We can use the ‘color’ parameter to set the color of each bar.

``````ax = df.plot.bar(stacked=True, width=0.8, figsize=(10,6), title='Total Sales by Salesperson and Month', rot=0, color=['#008fd5', '#fc4f30', '#e5ae38'])
ax.set_xlabel('Month')
ax.set_ylabel('Total Sales')
``````

This code creates a stacked bar chart with three bars representing the sales data for each salesperson in each month. The bars are colored using hex codes.

• Pandas documentation
• Matplotlib documentation
• DataCamp’s Pandas Cheat Sheet
• DataCamp’s Matplotlib Cheat Sheet

## Conclusion:

In conclusion, being able to effectively visualize data is essential in data analysis. Bar charts are a classic and straightforward way to represent data, but it can be challenging to display multiple columns on a single chart.

We have demonstrated how to plot multiple columns on a bar chart and a stacked bar chart using Python’s pandas DataFrame and matplotlib libraries. With this knowledge, you can create more visually appealing and informative charts that can help you gain insights from your data.

This article has covered the process of plotting multiple columns on a bar chart using Python’s pandas DataFrame and matplotlib libraries. We have demonstrated how to create fake data and plot it on a bar chart, how to create a stacked bar chart, and how to adjust the colors of the bars.

Visualizing data is a crucial aspect of data analysis, and knowing how to create informative and visually appealing charts is essential. With these techniques, you can create clear, effective, and aesthetically pleasing charts that can help you interpret your data effectively.