python bokeh,如何制作相关图? [英] python bokeh, how to make a correlation plot?

查看:70
本文介绍了python bokeh,如何制作相关图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Bokeh中建立关联热图?

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

顺便说一句,我可以在侧面制作一个颜色条,而不是在情节中创建图例吗?以及如何选择颜色范围/映射,例如深蓝色(-1)到白色(0)到深红色(+1)?

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)?

推荐答案

在现代散景中,您应该使用

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

关于图例,对于这样的色彩图,您实际上将需要离散的ColorBar而不是Legend.这是一项新功能,将在本周晚些时候发布的0.12.2版本(今天的日期:2016-08-28)中提供.这些新的颜色栏注释可以位于主图区域之外.

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.

GitHub仓库中还有一个示例:

There is also an example in the GitHub repo:

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

请注意,上一个示例还使用了另一个新功能来在浏览器中进行颜色映射,而不必预先在python中计算颜色.基本上总的来说,像这样:

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

还有更复杂的选项,例如如果您想更仔细地控制颜色栏上的刻度,可以像在普通Axis上一样添加自定义刻度或刻度格式,以实现以下目的:

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.

最后,Bokeh是一个大型项目,而找到最佳方法通常需要征求更多信息和背景信息,并且通常需要进行讨论. SO似乎不喜欢这种协作帮助(它们是不是真正的答案"),所以我鼓励您也查看

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.

这篇关于python bokeh,如何制作相关图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆