【问题标题】:Grouped Bar Plot with Pattern Fill using Python and Matplotlib使用 Python 和 Matplotlib 进行模式填充的分组条形图
【发布时间】:2019-12-27 16:48:25
【问题描述】:

我在以下网站上找到了以下条形图:http://ndaratha.blogspot.com/2015/03/grouped-bar-plot

根据网站,对应如下代码

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()

但是当我尝试运行代码时,我得到以下输出

有谁知道我做错了什么或者我应该怎么做才能获得所需的输出?

【问题讨论】:

  • 似乎只是缺少条形边缘,edgecolor="k"

标签: python matplotlib plot bar-chart


【解决方案1】:

您需要在您的plt.bar() 代码中添加edgecolor = "k",它为条形边缘提供黑色,您可以获得您想要的条形图。

【讨论】:

    【解决方案2】:
    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 2018-11-29
      • 2014-05-11
      相关资源
      最近更新 更多