[英]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.