堆积的散景图不运行

时间:2018-11-26 21:21:45

标签: python pandas bokeh

我正在与Bokeh一起练习,但是仍然失败,我从xls文件中获取数据,然后对数据进行过滤并按性别(女性和男性)进行分组。

           Cantidad
Sexo
FEMENINO        123
MASCULINO       352

这是我打印将要工作的列时的输出:

print df = df[['Sexo','Movil Victima','Cantidad']]

          Sexo    Movil Victima   Cantidad
1    MASCULINO          MOTO         1
2     FEMENINO          MOTO         1
3    MASCULINO         A PIE         1
4    MASCULINO          MOTO         1
5     FEMENINO         A PIE         1
6    MASCULINO         A PIE         1
7     FEMENINO         A PIE         1
8     FEMENINO         A PIE         1
9    MASCULINO         A PIE         1
10    FEMENINO         A PIE         1
11    FEMENINO         A PIE         1
12   MASCULINO     BICICLETA         1
13    FEMENINO         A PIE         1

这是我的代码:

import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral3
output_file('stackedBar-Sexo_tipoMovil.html')

df = pd.read_excel(r'C:\Users\cisco\Desktop\ProyectoV\transito\homicidios-accidentes-transito-2018_1.xls')

filter = df['Sexo'].isin(('MASCULINO','FEMENINO'))
df = df[filter]


grouped = df.groupby('Sexo')['Cantidad', 'Movil Victima', 'Profesion'].sum()

print(df)



source = ColumnDataSource(grouped)
countries = source.data['Sexo'].tolist()
p = figure(x_range=countries)

p.vbar_stack(stackers=['Movil Victima'], x='Sexo', source=source, legend = ['Movil Victima'], width=0.5, color=Spectral3)

p.title.text ='Muertes Genero por Tipo Movil'
p.legend.location = 'top_left'

p.xaxis.axis_label = 'Sexo'
p.xgrid.grid_line_color = None  #remove the x grid lines

p.yaxis.axis_label = 'Cantidad'

show(p)

输出显示第23行有错误:

p.vbar_stack(stackers=['Movil Victima'], x='Sexo', source=source, legend = ['Movil Victima'], width=0.5, color=Spectral3)

错误是:

_stack
    raise ValueError("Keyword argument sequences for broadcasting must all be the same lengths. Got lengths: %r" % sorted(list(lengths)))
ValueError: Keyword argument sequences for broadcasting must all be the same lengths. Got lengths: [1, 3].

哪个是错误?我看不到。 谢谢。

0 个答案:

没有答案