簡體   English   中英

散景堆積條形圖

[英]Bokeh stacked bar chart

我想在 jupyter 中使用 pandas 和 bokeh 做一個堆積條形圖。 我有 3 列:['Feedstuff','Month','Price']。 我想在 x 軸上有價格,在 y 軸上有每個飼料的名稱,以及按月堆疊的條形。 我的數據如下所示:

data=[
['feed_wheat','Jan',138.24],
['feed_wheat','Dec',141.84],
['wheat_feed','Jan',106.45], ], 
columns=['Feedstuff','Month','Price']

我的繪圖代碼

output_notebook()

source = ColumnDataSource(T_M_11)
fs=source.data['Feedstuff'].tolist()
mt=source.data['Month'].tolist()
pr=source.data['Price'].tolist()
colors = ["#A5D6F3", "#B7D8EB", "#72A2D0","#252825","#505D66","#71A5D8","#2A5581","#202E26","#3D545F",
         "#20364C","#6E9CD5","#3F64B4"]
data = {
    'Feedstuff' : fs,
     pr : mt
}


p = figure(x_range=fs, plot_height=250, title="Средняя цена за месяцы за 1 единицу корма (Ł/ton), в фунт стерлингов",
           toolbar_location=None,tools="hover", tooltips="$name @fs: @$name")

p.vbar_stack(mt, x='Feedstuff', width=0.9, color=colors,source=data,
             legend_label=mt)

p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

show(p)

這是錯誤的樣子:

> BokehJS 1.4.0 successfully loaded.
>---------------------------------------------------------------------------
>TypeError                                 Traceback (most recent call last)
><ipython-input-361-b751cfb4a157> in <module>
>     9 data = {
>     10     'Feedstuff' : fs,
>---> 11      pr : mt
>     12 }
> 13 
>TypeError: unhashable type: 'list'

不幸的是,我的繪圖代碼不起作用。 請你幫助我好嗎。

使用您的數據 DataFrame,這對我有用...

data_aux=pd.DataFrame(np.array(np.meshgrid(data['Feedstuff'].unique(), data['Month'].unique())).T.reshape(-1, 2), columns=['Feedstuff', 'Month']).merge(data, on=['Feedstuff',  'Month'], how='left').fillna(0)
data2 = {'Feedstuff' : data_aux['Feedstuff'].unique()}
data2.update(data_aux.groupby(['Month'])['Price'].apply(list).to_dict())

p= figure(y_range=fs, x_range=(0, 1000))

p.hbar_stack(mt, y='Feedstuff', height=0.9, color= ["#A5D6F3", "#B7D8EB"], source=ColumnDataSource(data2))
show(p)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM