如何分隔条形图? [英] How to separate the barchart?

查看:64
本文介绍了如何分隔条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何分隔这些条形图,我希望每4个之间有1个宽度的距离(指的是4种不同的类型),并请参考我从这段代码中得到的输出来找到附加的图像.

How can I separate these bars, I want a distance of 1 width between each 4(referring to the 4 different types) and kindly find the image attached referring to the output that I'm getting from this code.

import numpy as np
import matplotlib.pyplot as plt

N = 10
Google = (10, 15, 32, 29, 13, 35, 2, 20, 27, 29)

ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind, Google, width, color='b')

Voicebase = (2, 16, 19, 30, 22, 30, 33, 4, 14, 18)

rects2 = ax.bar(ind + width, Voicebase, width, color='g')

Watson = (7, 17, 14, 19, 28, 4, 4, 34, 9, 17)
rects3 = ax.bar(ind + width*2, Watson, width, color='y')  

Remeeting = (12, 21, 19, 35, 24, 6, 22, 31, 19, 14)
rects4 = ax.bar(ind + width*3, Remeeting, width, color='r')

# add some text for labels, title and axes ticks
ax.set_ylabel('Char edit distance')
ax.set_xticks(ind + width/2 )
ax.set_xticklabels(('A1', 'A2', 'A3', 'A4', 'A5','B1', 'B2', 'B3', 'B4', 'C1'))

ax.legend((rects1[0], rects2[0], rects3[0], rects4[0]), ('Google', 'Voicebase','Watson', 'Remeeting'))

def autolabel(rects):
    """
    Attach a text label above each bar displaying its height
    """
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
plt.show()

推荐答案

一种可能的方法是增加 ind 的间距,以便每 4 个数字生成一次.这将为您的所有四个酒吧留下空间.

One possible way to do this is to increase the spacing of your ind so that every 4th number is generated. This will leave space for all four of your bars.

import numpy as np
import matplotlib.pyplot as plt

Google = [10, 15, 32, 29, 13, 35, 2, 20, 27, 29]
Voicebase = [2, 16, 19, 30, 22, 30, 33, 4, 14, 18]
Watson = [7, 17, 14, 19, 28, 4, 4, 34, 9, 17]
Remeeting = [12, 21, 19, 35, 24, 6, 22, 31, 19, 14]

ind = np.arange(1,40,4) # the x locations for the groups
width = 0.5       # the width of the bars

fig, ax = plt.subplots()

rects1 = ax.bar(ind, Google, width, color='b')
rects2 = ax.bar(ind + width, Voicebase, width, color='g')
rects3 = ax.bar(ind + width*2, Watson, width, color='y')
rects4 = ax.bar(ind + width*3, Remeeting, width, color='r')

# add some text for labels, title and axes ticks
ax.set_ylabel('Char edit distance')
ax.set_xticks(ind + width/2 )
ax.set_xticklabels(('A1', 'A2', 'A3', 'A4', 'A5','B1', 'B2', 'B3', 'B4', 'C1'))

ax.legend((rects1[0], rects2[0], rects3[0], rects4[0]), ('Google', 'Voicebase','Watson', 'Remeeting'))

def autolabel(rects):
    """
    Attach a text label above each bar displaying its height
    """
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
plt.show()

这篇关于如何分隔条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆