Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从数据帧在Bokeh中创建嵌套条形图_Python_Pandas_Dataframe_Bar Chart_Bokeh - Fatal编程技术网

Python 从数据帧在Bokeh中创建嵌套条形图

Python 从数据帧在Bokeh中创建嵌套条形图,python,pandas,dataframe,bar-chart,bokeh,Python,Pandas,Dataframe,Bar Chart,Bokeh,我有一个现有的数据框架,它是按职位和年份分组的。我想在此基础上创建一个嵌套的Bokeh条形图,但我不知道如何正确地绘制它 数据帧: size fromJobtitle year CEO 2000 236 2001 479 2002 4 Director 2000 42

我有一个现有的数据框架,它是按职位和年份分组的。我想在此基础上创建一个嵌套的Bokeh条形图,但我不知道如何正确地绘制它

数据帧:

                       size
fromJobtitle      year   

CEO               2000   236
                  2001   479
                  2002     4
Director          2000    42
                  2001   609
                  2002   188
Employee          1998    23
                  1999   365
                  2000  2393
                  2001  5806
                  2002   817
In House Lawyer   2000     5
                  2001    54
Manager           1999     8
                  2000   979
                  2001  2173
                  2002   141
Managing Director 1998     2
                  1999    14
                  2000   130
                  2001   199
                  2002    11
President         1999    31
                  2000   202
                  2001   558
                  2002   198
Trader            1999     5
                  2000   336
                  2001   494
                  2002    61
Unknown           1999   591
                  2000  2960
                  2001  3959
                  2002   673
Vice President    1999    49
                  2000  2040
                  2001  3836
                  2002   370

一个示例输出是:

我假设您有一个数据框
df
,其中有三列
fromJobtitle
year
size
。如果有多索引,请重置索引。使用
FactorRange
bokeh
开始,我们需要一个包含两个字符串的元组列表(这很重要,浮动不起作用),如

等等

这可以通过

df['x'] = df[['fromJobtitle', 'year']].apply(lambda x: (x[0],str(x[1])), axis=1)
这是最重要的部分。其余的都是你的
bokeh

来自bokeh.plotting导入显示、图形、输出\u笔记本
从bokeh.models导入FactorRange
输出_笔记本()
p=数字(
x_range=FactorRange(*列表(df[“x”]),
宽度=1400
)
p、 vbar(
x=“x”,
top=“size”,
宽度=0.9,
source=df,
)
表演(p)
这是生成的图形

df['x'] = df[['fromJobtitle', 'year']].apply(lambda x: (x[0],str(x[1])), axis=1)