【问题标题】:Building a sorted Bar-chart with pandas & bokeh使用 pandas 和 bokeh 构建排序条形图
【发布时间】:2016-01-19 09:24:00
【问题描述】:

作为一些基本数据分析的一部分,我正在尝试使用散景以 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)

【问题讨论】:

  • 如果有一个不依赖 pandas 的解决方案就好了。

标签: python python-3.x pandas bar-chart bokeh


【解决方案1】:

通过创建排序的 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)

【讨论】:

    猜你喜欢
    • 2017-06-28
    • 2015-03-17
    • 2014-05-03
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2014-10-02
    • 2017-04-15
    相关资源
    最近更新 更多