散景区域图无法绘制

时间:2018-02-20 09:03:32

标签: python bokeh

由于某种原因,我无法在散景中绘制区域图表。

以下是用于相同代码的代码..

from bokeh.charts import Area, show, output_file 

Areadict = dict( 
    I = df['IEXT'], 
    Date=df['Month'], 
    O = df['OT'] 
) 

 area = Area(Areadict, x='Date', y=['I','O'], title="Area Chart", 
             legend="top_left", 
               xlabel='time', ylabel='memory') 

 output_file('area.html') 
 show(area)

所有我看到日期轴是否被绘制,但没有我感兴趣的两个areacharts的迹象。 请指教

1 个答案:

答案 0 :(得分:2)

我建议您查看Holoviews,它是构建在Bokeh之上的非常高级的API,并得到Bokeh团队的认可。您可以看到面积图示例in their documentation。基本上看起来像:

# create holoviews objects
dims = dict(kdims='time', vdims='memory')
python = hv.Area(python_array, label='python', **dims)
pypy   = hv.Area(pypy_array,   label='pypy',   **dims)
jython = hv.Area(jython_array, label='jython', **dims)

# plot
overlay.relabel("Area Chart") + hv.Area.stack(overlay).relabel("Stacked Area Chart")

产生的结果

enter image description here

否则,从Bokeh 0.13开始,使用稳定的bokeh.plotting API创建堆积面积图,您将需要自己堆积数据,如this example所示:

import numpy as np
import pandas as pd

from bokeh.plotting import figure, show, output_file
from bokeh.palettes import brewer

N = 20
cats = 10
df = pd.DataFrame(np.random.randint(10, 100, size=(N, cats))).add_prefix('y')

def  stacked(df):
    df_top = df.cumsum(axis=1)
    df_bottom = df_top.shift(axis=1).fillna({'y0': 0})[::-1]
    df_stack = pd.concat([df_bottom, df_top], ignore_index=True)
    return df_stack

areas = stacked(df)
colors = brewer['Spectral'][areas.shape[1]]
x2 = np.hstack((df.index[::-1], df.index))

p = figure(x_range=(0, N-1), y_range=(0, 800))
p.grid.minor_grid_line_color = '#eeeeee'

p.patches([x2] * areas.shape[1], [areas[c].values for c in areas],
          color=colors, alpha=0.8, line_color=None)

show(p)

这将导致

enter image description here