0

I am Plotting a multiple bar chart but unable to understand how to create a gap between the xticks Here is the code

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

dict = {'Asian': [2.607142857142857,
  9.84047619047619,
  40.102380952380955,
  47.45476190476191],
 'Black': [12.523809523809524,
  32.60476190476191,
  43.74523809523809,
  11.126190476190477],
 'Hispanic': [11.166666666666666,
  30.085714285714282,
  45.55238095238095,
  13.185714285714285],
 'White': [3.830952380952381, 14.083333333333334, 45.76190476190476, 36.3]}

# making a DataFrame here
df = pd.DataFrame(dict)

fig,ax = plt.subplots(figsize=(14,8))

# Set the position of bar on x-axis
# xLabels = np.arange(len(df.index))
n = 4
r = np.arange(n)

# set the bar width
width = 0.25

# PLOTTING THE BARS
plt.bar(r, df['Level 1 %'],color='g', width=width, label="Level1 %")

plt.bar(r+width, df['Level 2 %'], color='b', width=width, label="Level2 %")

plt.bar(r+2*width, df['Level 3 %'], color='y', width=width, label="Level3 %")

plt.bar(r+3*width, df['Level 4 %'], color='r', width=width, label = 'Level4 %')

# Adding ticks
plt.legend()
plt.xlabel('Cateogries')
plt.ylabel('Avg percent of students')
plt.xticks(r+3*width/2, ['Asians', 'Black','Hispanic','Whites'])

I know this is a silly question but I can't really think how do I make it right. The chart does not have space between bars. Pls help me with this. following is the image. The chart image here

otaku
  • 48
  • 7

0 Answers0