Luis Henrique
Luis Henrique

Reputation: 771

Create histogram with mean and standard deviation for multiples elements

The array below consists of the following structure:

Array:

[[[70, 2.23606797749979], [66, 5.477225575051661], [71, 1.4142135623730951], [75, 4.58257569495584], [68, 0.0]], [[78, 5.196152422706632], [69, 2.0], [69, 2.0], [69, 2.0], [69, 2.0]]]

[[[Average, Standard deviation],[Average, Standard deviation]],[ ... ]]] --> Block

I am trying to generate an output in matplotlib where the graph is a histogram with the following structure:

enter image description here

1 bar with the mean and the next to the side a bar with the standard deviation, following this order until the end of the block, which are composed of 5 means and 5 standard deviations

I tested something like:

x = [[Mean], [Standard Deviation]]
colors = ['red', 'lime']
plt.hist(x, n_bins, density=True, histtype='bar', color=colors, label=colors)

Note: The number of sublist with mean and standard deviation is relative to the size of the date set, the example array is generated by a function that consumes this data

Y Axis should range from 0 to 100, and it will not be often that the value will be repeated

Upvotes: 1

Views: 1430

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150815

I would do:

fig, ax = plt.subplots(figsize=(10,6))
for i in range(2):
    data = a[i]
    x=np.arange(len(data)) + i*6
    # draw means
    ax.bar(x-0.2, data[:,0], color='C0', width=0.4)

    # draw std
    ax.bar(x+0.2, data[:,1], color='C1', width=0.4)

# separation line
ax.axvline(4.75)

# turn off xticks
ax.set_xticks([]);

Output:

enter image description here

Upvotes: 1

Related Questions