[英]Grouped Bar Plot with Pattern Fill using Python and Matplotlib
I found the following barplot on the following website: http://ndaratha.blogspot.com/2015/03/grouped-bar-plot我在以下网站上找到了以下条形图: http ://ndaratha.blogspot.com/2015/03/grouped-bar-plot
According to the website, it corresponds to the following code根据官网,对应如下代码
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()
But when I try running the code, I get the following output但是当我尝试运行代码时,我得到以下输出
Does anyone know what I'm doing wrong or what I should do to get the desired output?有谁知道我做错了什么或者我应该怎么做才能获得所需的输出?
您需要在plt.bar()
代码中添加edgecolor = "k"
为条形边缘提供黑色,您可以获得所需的条形图。
When you add edgecolor = "k", code is as follows,
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],edgecolor='black')
plt.bar([p + width for p in pos], blue_data, width,
alpha=0.5,
color='w',
hatch='o',
label=labels[1],edgecolor='black')
plt.bar([p + width*2 for p in pos], red_data, width,
alpha=0.5,
color='k',
hatch='',
label=labels[2],edgecolor='black')
plt.bar([p + width*3 for p in pos], black_data, width,
alpha=0.5,
color='w',hatch='*',
label=labels[3],edgecolor='black')
# 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.