python bokeh, how to make a correlation plot?

前端 未结 3 1484
慢半拍i
慢半拍i 2021-01-21 15:45

How can I make a correlation heatmap in Bokeh?

import pandas as pd
import bokeh.charts

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

p = bokeh.cha         


        
3条回答
  •  死守一世寂寞
    2021-01-21 16:46

    In modern Bokeh you should use the bokeh.plotting interface. You can see an example of a categorical heatmap generated using this interface in the gallery:

    http://docs.bokeh.org/en/latest/docs/gallery/categorical.html


    Regarding a legend, for a colormap like this you actually will want a discrete ColorBar instead of a Legend. This is a new feature that will be present in the upcoming 0.12.2 release later this week (today's date: 2016-08-28). These new colorbar annotations can be located outside the main plot area.

    There is also an example in the GitHub repo:

    https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/color_data_map.py

    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:

    # 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')
    

    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 Axis, to achieve things like:

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


    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 project Discourse for help anytime.

提交回复
热议问题