python-3.x - 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)
解决方案
我想你想要的是这样的:
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)
然后,您可以对数据框中的其他国家/地区应用相同的方法。data
inColumnDataSource
应该包含带有键和向量值的字典,而不是 pandas DataFrames
。
结果:
推荐阅读
- html - 如何使用 html 在 x 轴上的表格中重复图像?
- c++ - std::vector 和 std::array 之间的区别
- .net - Ms Graph 读取消息给出 ErrorAccesDenied
- ios - 如何使用原始 iPhone SE 尺寸的 SwiftUI 画布
- arrays - Reactjs 删除数组的特定索引
- powershell - 收到乱序的 PowerShell 事件
- sql - ORA-02291: SQL Developer 中的完整性约束错误消息
- sql - 按状态分组的社区计数
- wpf - 我需要
.config 文件以在 WPF 中正常运行 - winapi - Windows 10 API 例程的汇编程序调用约定