首页 > 解决方案 > DataFrame 无法使用 Bokeh DataColumnSource 将值分配给轴

问题描述

我不明白为什么我不能将值分配给轴,我指定了源中的每一列。如果有人可以帮助我,我将不胜感激。数据来自http://data.un.org/ (人口增长、生育率、预期寿命和死亡率)一旦我可以将数据分配到轴上,我将在绘图上进行更多工作,因此为什么会有这么多列。

import pandas as pd
from bokeh.io import output_file,show,output_notebook,push_notebook
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource,HoverTool,CategoricalColorMapper
from bokeh.layouts import row,column,gridplot
from bokeh.models.widgets import Tabs,Panel



df = pd.read_csv('populationIndex2.csv', skiprows=1)
df = pd.DataFrame(df)
df.head()
df.columns

 source = ColumnDataSource(data = dict(AF = df[(df['Unnamed: 1'] == 
                          'Africa') & (df['Series'] == 'Life expectancy at 
                          birth for both sexes (years)')],
                          SA = df[(df['Unnamed: 1'] == 'South America') & 
                          (df['Series'] == 'Life expectancy at birth for 
                          both sexes (years)')],
                          NA = df[(df['Unnamed: 1'] == 'Northern America') 
                          & (df['Series'] == 'Life expectancy at birth for 
                          both sexes (years)')],
                          EU = df[(df['Unnamed: 1'] == 'Europe') & 
                          (df['Series'] == 'Life expectancy at birth for 
                          both sexes (years)')],
                          CA = df[(df['Unnamed: 1'] == 'Central America') 
                          & (df['Series'] == 'Life expectancy at birth for 
                          both sexes (years)')],
                          As = df[(df['Unnamed: 1'] == 'Asia') & 
                          (df['Series'] == 'Life expectancy at birth for 
                          both sexes (years)')],
                          Oc = df[(df['Unnamed: 1'] == 'Oceania') & 
                          (df['Series'] == 'Life expectancy at birth for 
                          both sexes (years)')],
                          Cb = df[(df['Unnamed: 1'] == 'Caribbean') & 
                          (df['Series'] == 'Life expectancy at birth for 
                          both sexes (years)')],
                          year = SA.Year))

tools = 'box_select, pan'
source.column_names
output_notebook()

p = figure(plot_height=300, plot_width=500,
          title='Life expectancy by continent',
          x_axis_label='Life expectancy by percent',
          y_axis_label='Years',
          tools=tools)
 #p2 = figure(plot_height=300, plot_with=500,
 #           title='')
 p.circle(x='AF', y='year', source = source, color='Yellow')
 show(p)

标签: python-3.xdataframeplotbokeh

解决方案


我想你想要的是这样的:

import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource

df = pd.read_csv('populationIndex2.csv', skiprows = 1)

for percent in df[(df['Unnamed: 1'] == 'Africa') & (df['Series'] == 'Life expectancy at birth for both sexes (years)')].values:
    print percent
    print percent [4]

source = ColumnDataSource(data = dict(AF = [percent[4] for percent in df[(df['Unnamed: 1'] == 'Africa') & (df['Series'] == 'Life expectancy at birth for both sexes (years)')].values],
                                      year = df[(df['Unnamed: 1'] == 'Northern America') & (df['Series'] == 'Life expectancy at birth for both sexes (years)')].Year.values))

p = figure(plot_height = 300, plot_width = 500,
          title = 'Life expectancy by continent',
          y_axis_label = 'Life expectancy by percent',
          x_axis_label = 'Years',
          tools = 'box_select, pan')
p.circle(x = 'year' , y = 'AF', source = source, color = 'red')

show(p)

然后,您可以对数据框中的其他国家/地区应用相同的方法。datainColumnDataSource应该包含带有键和向量值的字典,而不是 pandas DataFrames

结果:

在此处输入图像描述


推荐阅读