
In this post, you will learn how to create a Bar Chart in Matplotlib.
plt.bar() –
Read a dataset to work with.
import pandas as pd
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv"
df = pd.read_csv(url)
df.head()

Let’s calculate the total sales by Method of Payment.
pay_sale = df.groupby('Method of Payment')['Net Sales'].sum().reset_index()

Now, let’s create the bar chart.
plt.figure(figsize=(10,8))
plt.bar(pay_sale['Method of Payment'], pay_sale['Net Sales'])
plt.xlabel('Method of Payment', fontsize=14)
plt.ylabel('Total Sales', fontsize=14)
plt.title('Total Sales By Payment Method', fontsize=16)
plt.show()

You can also customize the color of the bars using the color parameter.
bar_colors = ['red','blue','crimson','seagreen','gold']
plt.figure(figsize=(10,8))
plt.bar(pay_sale['Method of Payment'], pay_sale['Net Sales'], color=bar_colors)
plt.xlabel('Method of Payment', fontsize=14)
plt.ylabel('Total Sales', fontsize=14)
plt.title('Total Sales By Payment Method', fontsize=16)
plt.show()

And to customize the width of the bars, use the width parameter.
bar_colors = ['red','blue','crimson','seagreen','gold']
plt.figure(figsize=(10,8))
plt.bar(pay_sale['Method of Payment'], pay_sale['Net Sales'],
width=0.5, color=bar_colors)
plt.xlabel('Method of Payment', fontsize=14)
plt.ylabel('Total Sales', fontsize=14)
plt.title('Total Sales By Payment Method', fontsize=16)
plt.show()
