Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
HamidBee
Power Participant
Power Participant

Stacked bar chart to clustered bar chart using a Python script

I am trying to create a clustered bar chart. My python code currently seems to have create a stacked bar chart. I'm relatively new to Python so I'm not too sure why this is. Please see below an image of what I am seeing:

 

Bar Chart Python.png

Below is an example of the clustered bar chart that I am trying to create using a Python script:

 

Bar Chart BI.png

I am including a link to the file below:

https://www.mediafire.com/file/7txd1k0knab2b81/Example_Table.pbix/file

 

In addition you can find the code I used below:

 

import pandas as pd
from matplotlib import pyplot as plt
plt.xlabel('Month')
plt.ylabel('Value')
plt.title('Month vs Value')
plt.bar(dataset['Month'],dataset['Total A 2021'], color='black', label='2021')
plt.bar(dataset['Month'],dataset['Total A 2020'], color='grey', label='2020')
plt.legend()
plt.show()

 

Thanks in advance.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @HamidBee ,

 

To include multiple X values on the same chart, we can reduce the width of the bars and then place the indices one bar’s width further from the y axis.

 

Please try:

import numpy as np

from matplotlib import pyplot as plt

N = 12
ind = np.arange(N) 
width = 0.3
plt.xlabel('Month')
plt.ylabel('Value')
plt.title('Month vs Value')
plt.bar(ind,dataset['Total A 2021'],width,color='black', label='2021')
plt.bar(ind + width,dataset['Total A 2020'], width,color='grey',label='2020')
plt.xticks(ind + width / 2, ('Jan', 'Feb', 'Mar', 'Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'))
plt.legend()
plt.show()

Eyelyn9_0-1641872227989.png

 

Refer to:

https://benalexkeen.com/bar-charts-in-matplotlib/

 

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @HamidBee ,

 

To include multiple X values on the same chart, we can reduce the width of the bars and then place the indices one bar’s width further from the y axis.

 

Please try:

import numpy as np

from matplotlib import pyplot as plt

N = 12
ind = np.arange(N) 
width = 0.3
plt.xlabel('Month')
plt.ylabel('Value')
plt.title('Month vs Value')
plt.bar(ind,dataset['Total A 2021'],width,color='black', label='2021')
plt.bar(ind + width,dataset['Total A 2020'], width,color='grey',label='2020')
plt.xticks(ind + width / 2, ('Jan', 'Feb', 'Mar', 'Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'))
plt.legend()
plt.show()

Eyelyn9_0-1641872227989.png

 

Refer to:

https://benalexkeen.com/bar-charts-in-matplotlib/

 

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.