Demo entry 1460863

Grouped Bar Plots with Pattern Fill

   

Submitted by anonymous on Mar 04, 2015 at 06:53
Language: Python. Code size: 1.7 kB.

import matplotlib.pyplot as plt

# Input data; groupwise
green_data = [16, 23, 22, 21, 13, 11, 18, 15]
blue_data =  [ 3,  3,  0,  0,  5,  5,  3,  3]
red_data =   [ 6,  6,  6,  0,  0,  0,  0,  0]
black_data = [25, 32, 28, 21, 18, 16, 21, 18]

labels = ['XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII']

# Setting the positions and width for the bars
pos = list(range(len(green_data))) 
width = 0.15 # the width of a bar
    
# Plotting the bars
fig, ax = plt.subplots(figsize=(10,6))

bar1=plt.bar(pos, green_data, width,
                 alpha=0.5,
                 color='w',
                 hatch='x', # this one defines the fill pattern
                 label=labels[0])

plt.bar([p + width for p in pos], blue_data, width,
                 alpha=0.5,
                 color='w',
                 hatch='o',
                 label=labels[1])
    
plt.bar([p + width*2 for p in pos], red_data, width,
                 alpha=0.5,
                 color='k',
                 hatch='',
                 label=labels[2])

plt.bar([p + width*3 for p in pos], black_data, width,
                 alpha=0.5,
                 color='w',hatch='*',
                 label=labels[3])


# Setting axis labels and ticks
ax.set_ylabel('Number of Switching')
ax.set_xlabel('Strategy')
ax.set_title('Grouped bar plot')
ax.set_xticks([p + 1.5 * width for p in pos])
ax.set_xticklabels(labels)

# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*5)
plt.ylim([0, max(green_data + blue_data + red_data) * 1.5])

# Adding the legend and showing the plot
plt.legend(['OLTC', 'SVC', 'SC', 'OLTC+SC+SVC'], loc='upper right')
plt.grid()
plt.show()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).