2016-09-08 6 views
1

私はPython Bokehライブラリのデータフレームdfから棒グラフを作成しようとしています。月 のための「価値」のMEANためPython Bokeh - blending

  • 1つのバー:

    value datetime 
    5 01-01-2015 
    7 02-01-2015 
    6 03-01-2015 
    ... ... (for 3 years) 
    

    私は月額3つのバーを示した棒グラフがしたい:私が持っているデータは、単にのように見えます月の「価値」のMAXのための

  • 1つのバー

は、私が作成することができる午前月の「価値」の平均のための1つのバーMEAN/MAX/MINの1つの棒グラフ:

from bokeh.charts import Bar, output_file, show 
p = Bar(df, 'datetime', values='value', title='mybargraph', 
     agg='mean', legend=None) 
output_file('test.html') 
show(p) 

どのようにして3つの棒(平均、最大、最小)を同じプロットにすることができますか?可能であれば、お互いに積み重ねてください。

blendのように見えますが(この例のように:http://bokeh.pydata.org/en/latest/docs/gallery/stacked_bar_chart.html)、どのように動作するのかの詳細な説明は見つかりません。 bokehのウェブサイトは素晴らしいですが、この特定のアイテムについてはそれほど詳細ではありません。

誰でも手伝ってください。

答えて

2

このブレンドの例では、私は正しい軌道に乗りました。

import pandas as pd 
from pandas import Series 
from dateutil.parser import parse 
from bokeh.plotting import figure 
from bokeh.layouts import row 
from bokeh.charts import Bar, output_file, show 
from bokeh.charts.attributes import cat, color 
from bokeh.charts.operations import blend 

output_file("datestats.html") 

ただ、いくつかのサンプルデータ、あなたが合うようにそれを変更して自由に感じます。 まず、データを適切なフォーマットにまとめる必要がありました。

# Sample data 
vals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 
dates = ["01-01-2015", "02-01-2015", "03-01-2015", "04-01-2015", 
     "01-02-2015", "02-02-2015", "03-02-2015", "04-02-2015", 
     "01-03-2015", "02-03-2015", "03-03-2015", "04-03-2015" 
     ] 

あなたの日付形式は「日 - 月 - 年」だったように見えた - パンダはそれを正しく認識することになるので、私はdateutil.parserを使用。

# Format data as pandas datetime objects with day-first custom 
days = [] 
days.append(parse(x, dayfirst=True) for x in dates) 

また、それが月ごとにグループ化された必要 - 私は、日付をダウンサンプリングするリサンプリング月ごとに適切な値を取得し、データフレームにマージパンダを使用。

# Put data into dataframe broken into min, mean, and max values each for month 
ts = Series(vals, index=days[0]) 
firstmerge = pd.merge(ts.resample('M').min().to_frame(name="min"), 
         ts.resample('M').mean().to_frame(name="mean"), 
         left_index=True, right_index=True) 
frame = pd.merge(firstmerge, ts.resample('M').max().to_frame(name="max"), 
       left_index=True, right_index=True) 

ボケは、あなたがグラフのx値としてパンダのデータフレームのインデックスを使用することができますdiscussed here として が、私は、日付ラベルの新しい列を追加しましたので、それは、datetime値を好きではなかったです。以下のコメントをご覧ください***。

# You can use DataFrame index for bokeh x values but it doesn't like timestamp 
frame['Month'] = frame.index.strftime('%m-%Y') 

最後にチャートパートに到達します。オリンピックメダルの例のように、Barにいくつかの引数を渡します。 これで好きなように演奏しますが、注記私は凡例をチャートの外側にまとめて追加しました。多くのデータポイントを持っている場合は、ここで構築された方法でチャート上で非常に乱雑になります。

# Main object to render with stacking 
bar = Bar(frame, 
      values=blend('min', 'mean', 'max', 
         name='values', labels_name='stats'), 
      label=cat(columns='Month', sort=False), 
      stack=cat(columns='values', sort=False), 
      color=color(columns='values', 
         palette=['SaddleBrown', 'Silver', 'Goldenrod'], 
         sort=True), 
      legend=None, 
      title="Statistical Values Grouped by Month", 
      tooltips=[('Value', '@values')] 
     ) 

# Legend info (displayed as separate chart using bokeh.layouts' row) 
factors = ["min", "mean", "max"] 
x = [0] * len(factors) 
y = factors 
pal = ['SaddleBrown', 'Silver', 'Goldenrod'] 
p = figure(width=100, toolbar_location=None, y_range=factors) 
p.rect(x, y, color=pal, width=10, height=1) 
p.xaxis.major_label_text_color = None 
p.xaxis.major_tick_line_color = None 
p.xaxis.minor_tick_line_color = None 

# Display chart 
show(row(bar, p)) 

Bokeh_output

あなたは/このコードをコピー&ペーストした場合、これはあなたがが表示されますものです。
自分でレンダリングしたり、レンダリングする場合は、各ブロックにカーソルを合わせるとツールチップ(値)が表示されます。

私はできる限りすべてを抽象化しませんでした(色が気になります)。

これは、作成したいチャートのタイプですが、スタックトータル(分+平均+最大)は意味のある情報を提供しないため、異なるチャートスタイルがデータをより情報として表示するようです。しかし、あなたのデータが本当に何であるか分かりません。

*** timeseries chartと考えることができます。これは、プロットする前に行われたデータの一部を削除する可能性があります。

また、スタックする代わりにgrouping your barsと考えることもできます。そうすれば、毎月の数字を簡単に視覚化することができます。