python bokeh, how to make a correlation plot?
<p>How can I make a correlation heatmap in Bokeh?</p>

<pre><code>import pandas as pd
import bokeh.charts

df = pd.util.testing.makeTimeDataFrame(1000)
c = df.corr()

p = bokeh.charts.HeatMap(c) # not right

# try to make it a long form
# (and it's ugly in pandas to use 'index' in melt)

c['x'] = c.index
c = pd.melt(c, 'x', ['A','B','C','D'])

# this shows the right 4x4 matrix, but values are still wrong
p = bokeh.charts.HeatMap(c, x = 'x', y = 'variable', values = 'value') 
</code></pre>

<p>By the way, can I make a colorbar on the side, instead of legends in the plot? And also how to choose the color range/mapping eg dark blue (-1) to white (0) to dark red (+1)?</p>

<p>If you want that level of control, I have to suggest using the (only slightly) lower level <a href="http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html" rel="nofollow"><code>bokeh.plotting</code> interface</a>. You can see an example of a categorical heatmap generated using this interface in the gallery:</p>

<p><a href="http://bokeh.pydata.org/en/latest/docs/gallery/categorical.html" rel="nofollow">http://bokeh.pydata.org/en/latest/docs/gallery/categorical.html</a></p>

<hr>

<p>Regarding a legend, for a colormap like this you actually will want a discrete <code>ColorBar</code> instead of a <code>Legend</code>. This is a new feature that will be present in the upcoming <code>0.12.2</code> release later this week <em>(today's date: 2016-08-28)</em>. These new colorbar annotations can be located outside the main plot area. Currently, to see the documentation for it, you will have to refer to the "dev preview" docs site:</p>

<p><a href="http://bokeh.pydata.org/en/dev/docs/user_guide/annotations.html#color-bars" rel="nofollow">http://bokeh.pydata.org/en/dev/docs/user_guide/annotations.html#color-bars</a></p>

<p>There is also an example in the GitHub repo:</p>

<p><a href="https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/color_data_map.py" rel="nofollow">https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/color_data_map.py</a></p>

<p>Note that last example also uses another new feature to do the colormapping in the browser, instead of having to precompute the colors in python. Basically all together it looks like:</p>

<pre><code># create a color mapper with your palette - can be any list of colors
mapper = LinearColorMapper(palette=Viridis3, low=0, high=100)

p = figure(toolbar_location=None, tools='', title=title)
p.circle(
    x='x', y='y', source=source

    # use the mapper to colormap according to the 'z' column (in the browser)
    fill_color={'field': 'z', 'transform': mapper},  
)

# create a ColorBar and addit to the side of the plot
color_bar = ColorBar(color_mapper=mapper, location=(0, 0))
p.add_layout(color_bar, 'right')
</code></pre>

<p>There are more sophisticated options too, e.g. if you want to control the ticking on the colorbar more carefully you could add a custom ticker or tick formatter just like on a normal <code>Axis</code>, to achieve things like:</p>

<p><a href="http://i.stack.imgur.com/XnHVB.png" rel="nofollow"><img src="http://i.stack.imgur.com/XnHVB.png" alt="enter image description here"></a></p>

<p>It's not clear what your actual requirements are, so I just mention this in case it is useful to know. </p>

<p>Until <code>0.12.2</code> is released, you can use these new features by installing a "dev build" or release candidate. The main docs site has simple instructions for <a href="http://bokeh.pydata.org/en/dev/docs/installation.html#developer-builds" rel="nofollow">installing developer builds</a>.</p>

<hr>

<p>Finally, Bokeh is a large project and finding the best way to do so often involves asking for more information and context, and in general, having a discussion. That kind of collaborative help seems to be frowned upon at  SO, (they are "not real answers") so I'd encourage you to also check out the <a href="https://groups.google.com/a/continuum.io/forum/?pli=1#!forum/bokeh" rel="nofollow">public mailing list</a> for help anytime. </p>

python heatmap bokeh