Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
Pandas 如何使用嵌套类别在bokeh中绘制堆叠vbar_Pandas_Bokeh - Fatal编程技术网

Pandas 如何使用嵌套类别在bokeh中绘制堆叠vbar

Pandas 如何使用嵌套类别在bokeh中绘制堆叠vbar,pandas,bokeh,Pandas,Bokeh,我试图用嵌套类别(x轴)绘制vbar_堆叠(y轴)。我使用了Pandas的多索引并将其传递给ColumnDataSource,但不知道如何在vbar_堆栈中指定“x”。下面是我正在编写的代码 提前谢谢 import pandas as pd from bokeh.models import ColumnDataSource, FactorRange from bokeh.io import show, output_file import numpy as np from bokeh.plott

我试图用嵌套类别(x轴)绘制vbar_堆叠(y轴)。我使用了Pandas的多索引并将其传递给ColumnDataSource,但不知道如何在vbar_堆栈中指定“x”。下面是我正在编写的代码

提前谢谢

import pandas as pd
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.io import show, output_file
import numpy as np
from bokeh.plotting import figure, curdoc, output_file, show

output_file("stacked.html")


second_x = ['A', 'B', 'C', 'D']
first_x = ['one','two']
stacks = ["q", "w", "e"]

iterable = [first_x, second_x]
index = pd.MultiIndex.from_product(iterable, names=['first', 'second'])


df = pd.DataFrame(np.random.randn(len(stacks),len(first_x)*len(second_x)), index = stacks, columns= index)
source = ColumnDataSource(df)

h_index = df.columns.tolist()

p=figure(x_range=FactorRange(*h_index))
p.vbar_stack(x=WHAT SHOULD I PUT?, width=0.9, stackers=['q','w','e'], source = source)

Bokeh文档中有一些很好的示例,可以轻松创建这些图表。我用一个来创建这个例子

from bokeh.core.properties import value
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.io import show, output_file
from bokeh.plotting import figure, output_file, show

output_file("stacked.html")

second_x = ['A', 'B', 'C', 'D']
first_x = ['one','two']
q = [1, 2, 3, 4, 5, 6, 7, 8]
w = [8, 7, 6, 5, 4, 3, 2, 1]
e = [2, 4, 6, 8, 7, 5, 3, 1]

stacks = ['q', 'w', 'e']

factors = []
for first in first_x:
    for second in second_x:
        factors.append((first, second))

source = ColumnDataSource(data=dict(
    x=factors,
    q=q,
    w=w,
    e=e
))

p=figure(x_range=FactorRange(*factors), title='Chart')
p.yaxis.axis_label = "Axis Title"
p.xgrid.grid_line_color = None
p.vbar_stack(stacks, x='x', width=0.9, color=['blue', 'orange', 'gray'], source = source, legend=[value(x) for x in stacks])
show(p)

下面是一个您想要的解决方案:我刚刚修改了您的随机数据帧

import pandas as pd
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.core.properties import value
from bokeh.io import show, output_file
import numpy as np
from bokeh.plotting import figure, curdoc, output_file, show

output_file("stacked.html")

second_x = ['A', 'B', 'C', 'D']
first_x = ['one', 'two']
stacks = ["q", "w", "e"]

iterable = [first_x, second_x]
index = pd.MultiIndex.from_product(iterable, names=['first', 'second'])

#new randomn generator to avoid negative numbers
df = pd.DataFrame(np.random.randint(5, size=(len(stacks), len(first_x) * len(second_x))), index=stacks, columns=index)
print(df)

# this creates the double axis
factors = [(f_x, s_x) for f_x in first_x for s_x in second_x]

source = ColumnDataSource(data=dict(
    x = factors,
    q = df.loc['q'].tolist(),
    w = df.loc['w'].tolist(),
    e = df.loc['e'].tolist()
))

h_index = df.columns.tolist()

p = figure(x_range=FactorRange(*h_index), title='Chart')

p.vbar_stack(stacks, x='x', width = 0.9,  color=["blue", "red", "grey"], source = source,
             legend=[value(x) for x in stacks])

p.yaxis.axis_label = "Axis Title"
p.xgrid.grid_line_color = None
p.legend.location = "top_center"
p.legend.orientation = "horizontal"
show(p)
输出df:

first  one          two         
second   A  B  C  D   A  B  C  D
q        2  0  3  4   2  4  1  2
w        4  3  4  1   1  3  3  1
e        3  3  4  4   4  0  0  3
图形结果:


如果有帮助,请验证答案