Home > Net >  Python matplotlib.pyplot
Python matplotlib.pyplot

Time:04-12

My labels on this bar graph are off by one and I can't for the life of me figure out how to correct it. I have tried adding another series to the index but that then creats an error. Would this have something to do with the width of the bars? I'm lost.

import matplotlib.pyplot as plt
import numpy as np


labels = ['cluster1','cluster2','cluster3','cluster4','cluster5','cluster6','cluster7']
Mean = df_mean['net_sale']
Count = df_p_count
Sum = df_p_sum

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, Count, width, label='% of Sales')
rects2 = ax.bar(x   width/2, Sum, width, label='% of Revenue')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Percent')
ax.set_title('Discount Promotion Analysis')
ax.set_xticks(x, labels)
#ax.set_xticklabels(labels)
ax.set_xticklabels(['cluster1','cluster2','cluster3','cluster4','cluster5','cluster6','cluster7'])
ax.legend()



fig.tight_layout()

plt.show()

enter image description here

  • Related