簡體   English   中英

用熊貓和散景構建分類的條形圖

[英]Building a sorted Bar-chart with pandas & bokeh

作為一些基本數據分析的一部分,我試圖使用bokeh以HTML格式顯示一些圖表。 通過以下代碼,我使用了一個具有26列和594行的pandas數據框,創建了一個非常簡單的條形圖。 我的問題是:如何按廢品價值降序排序? 現在,該圖按“材料組”列的字母順序排序。 我幾乎嘗試了所有東西,但無法弄清楚...

import pandas as pd
from bokeh.charts import Bar, output_file, show
from bokeh.charts.attributes import cat
chart1 = Bar(df,values='SCRAP',
             label=cat(columns='MATERIAL GROUP',sort=True),
             plot_width=1250,agg='sum')      
output_file('DMSY_November_2015.html')
show(chart1)

通過創建排序的DataFrame並使用其繪制條形圖來找到解決方案:

import pandas as pd
from bokeh.charts import Bar, output_file, show
from bokeh.charts.attributes import cat

#Creating the DataFrame for chart1: Filtering/Grouping/Sorting
df_chart1 = df_rolling[df_rolling.SCRAP>0]
df_chart1 = df_chart1.groupby(by='MATERIAL GROUP',as_index=False)    ['SCRAP'].sum()
df_chart1 = df_chart1.sort_values(by='SCRAP',ascending=False)

#Plotting chart1
chart1 = Bar(df_chart1,values='SCRAP',
             label=cat(columns='MATERIAL GROUP',sort=False),
             plot_width=1250)

output_file('DMSY_November_2015.html')
show(chart1)

暫無
暫無

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

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