create plot with different color bars for different categories in histogram

Question:

I am trying to plot a histogram that looks like this, with different colors for every 10 values. enter image description here

However, I’m unsure about the bins in histogram and was able to only get a plot as follows.

y=[500, 477, 455, 434, 415, 396, 378, 361, 344, 328, 314, 299, 286, 273, 260, 248, 237, 226, 216, 206, 197, 188, 179, 171, 163, 156, 149, 142, 135, 129, 123, 118, 112, 107, 102, 98, 93, 89, 85, 81, 77, 74, 70, 67, 64, 61, 58, 56, 53, 51, 48, 46, 44, 42, 40, 38, 36, 35, 33, 32, 30, 29, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 15, 14, 13, 13, 12, 12, 11, 11, 10, 10, 9, 9, 8, 8, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5]
x = list(range(100))
plt.hist(y,x)

And the histogram looks like this
enter image description here

Any help would be very much appreciated. TIA

Answers:

I think you should be using plt.bar() with x and y as per your requirement, not histogram. That would give you the bars the way you want. Also, you can choose color of each bar using set_color(your_color). The updated code is below. Do make sure you have sufficient colors in the list so that you can display different colors. Hope this is what you are looking for…

y=[500, 477, 455, 434, 415, 396, 378, 361, 344, 328, 314, 299, 286, 273, 260, 248, 237, 226, 216, 206, 197, 188, 179, 171, 163, 156, 149, 142, 135, 129, 123, 118, 112, 107, 102, 98, 93, 89, 85, 81, 77, 74, 70, 67, 64, 61, 58, 56, 53, 51, 48, 46, 44, 42, 40, 38, 36, 35, 33, 32, 30, 29, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 15, 14, 13, 13, 12, 12, 11, 11, 10, 10, 9, 9, 8, 8, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5]
x = list(range(100))

## The list of colors you want to display (10 here as you have 100 entries)
colors = ['b', 'r', 'g', 'k', 'c', 'm', 'y', 'tab:orange', 'tab:brown', 'tab:gray']

## Plot bars
bars=plt.bar(y,x)

## For each bar, set color... i/10 will give same color to 10 bars
for i, item in enumerate(bars):
    item.set_color(colors[int(i/10)])

Output plot

enter image description here

Answered By: Redox

plt.hist(y, x) makes a histogram of the y values, using x to determine the bins. So, 99 bars are created. First there are 5 bars with height 0, as there are no y values below 5. Then there is a bar with height 4 as there are 4 y values with value 5, then another bar with height 4 corresponding to 6.

However, as your y values seem to correspond to the counts of each x, you can directly create a bar plot.

You can create a bar plot. plt.bar has a convenient parameter color where you can provide a color for each of the bars. The tick labels can be located using the mean x-positions of the bars.

import matplotlib.pyplot as plt

y = [500, 477, 455, 434, 415, 396, 378, 361, 344, 328, 314, 299, 286, 273, 260, 248, 237, 226, 216, 206, 197, 188, 179, 171, 163, 156, 149, 142, 135, 129, 123, 118, 112, 107, 102, 98, 93, 89, 85, 81, 77, 74, 70, 67, 64, 61, 58, 56, 53, 51, 48, 46, 44, 42, 40, 38, 36, 35, 33, 32, 30, 29, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 15, 14, 13, 13, 12, 12, 11, 11, 10, 10, 9, 9, 8, 8, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5]
x = list(range(100))

colors = plt.cm.tab10.colors

plt.figure(figsize=(20, 5))
plt.bar(x, y, color=[c for c in colors for _ in range(10)])

# set ticks at the mean positions of the bars
ticks = [sum([xi for xi in x[i:i + 10]]) / 10 for i in range(0, 100, 10)]
labels = [f'task{i}' for i in range(1, 11)]
plt.xticks(ticks, labels)

plt.margins(x=0.003) # less empty space at the left and right of the bars
plt.show()

colored histogram

Answered By: JohanC
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.