0

I have the following data where A and B belong to two different categories; additionally within A and B, A1 (or B1) belong to subcategories of A and B. In other words, A1 is in one subcategory and A2 and A3 in another. Similarly for B.

labels= ['A1', 'A2', 'A3', 'B1', 'B2', 'B3']
values= [2, 4, 3, 2.5, 3.5, 4]

I would like to do a bar plot where A and B categories as well as sub categories are separated (preferably by different colors). My attempt is:

import matplotlib.pyplot as plt
import numpy as np

index = np.arange(len(labels))

plt.bar(index, values)
plt.ylabel('Values', fontsize=14)
plt.xticks(index, labels, fontsize=14)
plt.show()

enter image description here

How can I customize the spacing/coloring between the bars?

A.E
  • 997
  • 1
  • 16
  • 33
  • Are you groups always in size of 3? A1, A2, A3, B1, B2, B3, C1, C2, C3 and so on? – Sheldore Jun 16 '19 at 17:23
  • *bar plot where A and B categories as well as sub categories are separated (preferably by different colors).*... Do you want to have 6 colors? each category and each subcategory in different color? Then what is the purpose of coloring? – Sheldore Jun 16 '19 at 17:24
  • *How can I customize the spacing/coloring between the bars?*.... What kind of spacing do you want? Please include these things in your question to make it more clear to the readers. – Sheldore Jun 16 '19 at 17:28
  • They are not always in size of 3 but in this case they are. In the graph above I want to have spacing (d) between A3 and B1. Also spacing (d/3) between A1- A2 and B1-B2. If coloring is not possible I want to have another label for A and B categories. Basically, below A1, A2, A3 there will be a label A and similarly for B. – A.E Jun 16 '19 at 17:39
  • Have a look at similar problems [here](https://stackoverflow.com/questions/34885526/creating-bar-chart-with-different-groups-in-different-colors-in-python) and [here](https://stackoverflow.com/questions/47796264/function-to-create-grouped-bar-plot) and [here](https://stackoverflow.com/questions/51502360/python-plot-grouped-bar-graph) and [here](https://stackoverflow.com/questions/43554521/add-data-label-to-grouped-bar-chart-in-matplotlib). You first need to figure out a way to group them by category. Plotting them is a secondary task – Sheldore Jun 16 '19 at 17:46
  • Thanks again for your response but I am going to accept the response by @ZarakiKenpachi – A.E Jun 16 '19 at 22:41

1 Answers1

2

You can set indexing and colors manually:

import matplotlib.pyplot as plt
import numpy as np

labels= ['A1', 'A2', 'A3', 'B1', 'B2', 'B3']
values= [2, 4, 3, 2.5, 3.5, 4]

# set indexing manually
index = np.array([0,1,2,4,5,6])
# plot bars with colors
bar_list = plt.bar(index, values, width=0.5, color=('r', 'r', 'r', 'b','b','b'))

plt.ylabel('Values', fontsize=14)
plt.xticks(index, labels, fontsize=14)
plt.show()

Output:

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38
  • Thank you very much; this is exactly what I want. Is it also possible to decrease the spacing between A2-A3 and B2-B3? Can you also tell me how I can put secondary x label below A1 (M category), A2-3(N category), B1(M category), B2-B3(N category)? – A.E Jun 16 '19 at 18:16
  • @A.E to mnmipulate distances between bars change values inside index array like : np.array([0,1,1.7,4,5,5.7]) – Zaraki Kenpachi Jun 16 '19 at 18:22