条形图matplotlib基于8行数组,每行5个值 [英] Bar chart matplotlib based on array of 8 rows with 5 values each

查看:48
本文介绍了条形图matplotlib基于8行数组,每行5个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用此表格的数组:

  data = [[19,14,6,6,36,3],[12,12,1,32,1],[18,25,0,33,0],[13,19,0,32,5],[12,14,0,33,0],[16、14、7、30、0],[11,18,5,31,2],[17、11、3、46、7] 

我想将其绘制为条形图.x轴上将有8个点,每个点有5条,其高度对应于数组每一行中的5个值.超级会感谢您的帮助!

解决方案

使用

 将numpy导入为np导入matplotlib.pyplot作为plt数据= np.array([[19,14,6,6,36,3],[12,12,1,32,1],[18,25,0,33,0],[13,19,0,32,5],[12,14,0,33,0],[16、14、7、30、0],[11,18,5,31,2],[17,11,3,46,7]])x = np.arange(data.shape [0])dx =(np.arange(data.shape [1])-data.shape [1]/2.)/(data.shape [1] +2.)d = 1//(data.shape [1] +2.)无花果,ax = plt.subplots()对于范围内的i(data.shape [1]):ax.bar(x + dx [i],data [:,i],width = d,label ="label {}".format(i))plt.legend(framealpha = 1).draggable()plt.show() 

堆积的酒吧

或者您也可以将条形图堆叠在一起,以使条形图的底部从上一个条形图的顶部开始.

 将numpy导入为np导入matplotlib.pyplot作为plt数据= np.array([[19,14,6,6,36,3],[12,12,1,32,1],[18,25,0,33,0],[13,19,0,32,5],[12,14,0,33,0],[16、14、7、30、0],[11,18,5,31,2],[17,11,3,46,7]])x = np.arange(data.shape [0])无花果,ax = plt.subplots()对于范围内的i(data.shape [1]):bottom = np.sum(data [:,0:i],axis = 1)ax.bar(x,data [:,i],bottom = bottom,label ="label {}".format(i))plt.legend(framealpha = 1).draggable()plt.show() 

I have an array off this form:

data = [[19, 14, 6, 36, 3],
        [12, 12, 1, 32, 1],
        [18, 25, 0, 33, 0],
        [13, 19, 0, 32, 5],
        [12, 14, 0, 33, 0],
        [16, 14, 7, 30, 0],
        [11, 18, 5, 31, 2],
        [17, 11, 3, 46, 7]]

I want to plot it as a bar chart. There would be 8 points on the x-axis, each having 5 bars, with heights corresponding to the 5 values in each row of the array. Would super appreciate any help!

解决方案

There are two obtions using plt.bar.

Single, adjacent bars

You can either plot the bars next to each other, in a grouped fashion, where you need to determine the bars' positions from the number of columns in the array.

import numpy as np
import matplotlib.pyplot as plt

data = np.array([[19, 14, 6, 36, 3],
                 [12, 12, 1, 32, 1],
                 [18, 25, 0, 33, 0],
                 [13, 19, 0, 32, 5],
                 [12, 14, 0, 33, 0],
                 [16, 14, 7, 30, 0],
                 [11, 18, 5, 31, 2],
                 [17, 11, 3, 46, 7]])
x = np.arange(data.shape[0])
dx = (np.arange(data.shape[1])-data.shape[1]/2.)/(data.shape[1]+2.)
d = 1./(data.shape[1]+2.)


fig, ax=plt.subplots()
for i in range(data.shape[1]):
    ax.bar(x+dx[i],data[:,i], width=d, label="label {}".format(i))

plt.legend(framealpha=1).draggable()
plt.show()

Stacked bars

Or you can stack the bars on top of each other, such that the bottom of the bar starts at the top of the previous one.

import numpy as np
import matplotlib.pyplot as plt

data = np.array([[19, 14, 6, 36, 3],
                 [12, 12, 1, 32, 1],
                 [18, 25, 0, 33, 0],
                 [13, 19, 0, 32, 5],
                 [12, 14, 0, 33, 0],
                 [16, 14, 7, 30, 0],
                 [11, 18, 5, 31, 2],
                 [17, 11, 3, 46, 7]])
x = np.arange(data.shape[0])

fig, ax=plt.subplots()
for i in range(data.shape[1]):
    bottom=np.sum(data[:,0:i], axis=1)  
    ax.bar(x,data[:,i], bottom=bottom, label="label {}".format(i))

plt.legend(framealpha=1).draggable()
plt.show()

这篇关于条形图matplotlib基于8行数组,每行5个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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