Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a data like this
Region    East  North  South  West
city                              
Albany    10.0    8.0    2.0   3.0
Concord    9.0   10.0    5.0  16.0
Santa Fe   1.0    7.0    5.0   8.0
Trenton    6.0    0.0    6.0   6.0


I have this value in temp1 data frame and I want to generate clustered bar chart.

What I have tried:

print(temp1)

plt.bar(Region,city)
plt.show()


But the chart is not showing correct Way clustered is not showing.

So what to write in a code to generate clustered chart in python using matplot lib
Posted
Updated 6-Apr-23 23:57pm

1 solution

Take a look at clustered chart in python using matplot lib - Google Search[^] for some suggestions.

[edit]

The first link in that Google search has the following (modified) code:
Python
# data from https://allisonhorst.github.io/palmerpenguins/

import matplotlib.pyplot as plt
import numpy as np

region = ("East", "North", "South", "West")
city = {
    'Albany': (10.0, 8.0, 2.0, 3.0),
    'Concord': (9.0, 10.0, 5.0, 16.0),
    'Santa Fe': (1.0, 7.0, 5.0, 8.0),
    'Trenton': (6.0, 0.0, 6.0, 6.0),
}

x = np.arange(len(region))  # the label locations
width = 0.25  # the width of the bars
multiplier = 0

fig, ax = plt.subplots(layout='constrained')

for attribute, measurement in city.items():
    offset = width * multiplier
    rects = ax.bar(x + offset, measurement, width, label=attribute)
    ax.bar_label(rects, padding=3)
    multiplier += 1

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('City')
ax.set_title('Region')
ax.set_xticks(x + width, region)
ax.legend(loc='upper left', ncols=3)
ax.set_ylim(0, 25)

plt.show()

That produces a nice coloured plot.

[/edit]
 
Share this answer
 
v2
Comments
An@mik@ 7-Apr-23 6:02am    
I did this but I am not getting the things I want. If you can submit the code it will help to me
Richard MacCutchan 7-Apr-23 7:43am    
I have no idea what code you want, and this site does not provide code to order. You need to go and study some of those links to see what possible solutions are available to get the sort of plot that you are looking for.
Richard MacCutchan 7-Apr-23 8:28am    
See my update above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900