python - Plotting a Heat Table Based on bokeh -
i trying make heat map 1 bokeh:
where code here: http://bokeh.pydata.org/en/latest/docs/gallery/unemployment.html
i got pretty close, reason printing values in diagonal order.
i tried format data same way , substitute it, got little more complicated that. here data:
from collections import ordereddict import numpy np import pandas pd bokeh.plotting import columndatasource, figure, show, output_file bokeh.models import hovertool import pandas.util.testing tm; tm.n = 3 df = pd.read_csv('mydata.csv', usecols=[1, 16]) df = df.set_index('recvd_dttm') df.index = pd.to_datetime(df.index, format='%m/%d/%y %h:%m') result = df.groupby([lambda idx: idx.month, 'companyname']).agg(len).reset_index() result.columns = ['month', 'companyname', 'numbercalls'] pivot_table = result.pivot(index='month', columns='companyname', values='numbercalls').fillna(0) s = pivot_table.sum().sort(ascending=false,inplace=false) pivot_table = pivot_table.ix[:,s.index[:46]] pivot_table = pivot_table.transpose() pivot_table.to_csv('pivot_table.csv') pivot_table = pivot_table.reset_index() pivot_table['companyname'] = [str(x) x in pivot_table['companyname']] companies = list(pivot_table['companyname']) months = ["1","2","3","4","5","6","7","8","9","10","11","12"] pivot_table = pivot_table.set_index('companyname') # colormap original plot colors = [ "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d" ] # set data plotting. need have values every # pair of year/month names. map rate color. month = [] company = [] color = [] rate = [] y in pivot_table.index: m in pivot_table.columns: month.append(m) company.append(y) num_calls = pivot_table.loc[y,m] rate.append(num_calls) color.append(colors[min(int(num_calls)-2, 8)]) source = columndatasource( data=dict(months=months, companies=companies, color=color, rate=rate) ) output_file('heatmap.html') tools = "resize,hover,save,pan,box_zoom,wheel_zoom" p = figure(title="customer calls year", x_range=companies, y_range=list(reversed(months)), x_axis_location="above", plot_width=1400, plot_height=900, toolbar_location="left", tools=tools) p.rect("companies", "months", 1, 1, source=source, color="color", line_color=none) p.grid.grid_line_color = none p.axis.axis_line_color = none p.axis.major_tick_line_color = none p.axis.major_label_text_font_size = "10pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = np.pi/3 hover = p.select(dict(type=hovertool)) hover.tooltips = ordereddict([ ('company name', '@companies'), ('number of calls', '@rate'), ]) show(p) # show plot
import pandas pd import numpy np import matplotlib.pyplot plt # following previous post simulate data np.random.seed(0) dates = np.random.choice(pd.date_range('2015-01-01 00:00:00', '2015-06-30 00:00:00', freq='1h'), 10000) company = np.random.choice(['company' + x x in '1 2 3 4 5'.split()], 10000) df = pd.dataframe(dict(recvd_dttm=dates, companyname=company)).set_index('recvd_dttm').sort_index() df['c'] = 1 df.columns = ['companyname', ''] result = df.groupby([lambda idx: idx.month, 'companyname']).agg({df.columns[1]: sum}).reset_index() result.columns = ['month', 'companyname', 'counts'] pivot_table = result.pivot(index='companyname', columns='month', values='counts') x_labels = ['month'+str(x) x in pivot_table.columns.values] y_labels = pivot_table.index.values fig, ax = plt.subplots() x = ax.imshow(pivot_table, cmap=plt.cm.winter) plt.colorbar(mappable=x, ax=ax) ax.set_xticks(np.arange(len(x_labels))) ax.set_yticks(np.arange(len(y_labels))) ax.set_xticklabels(x_labels) ax.set_yticklabels(y_labels) ax.set_xlabel('month') ax.set_ylabel('company') ax.set_title('customer calls year')
Comments
Post a Comment