具有熊猫数据框的hvplot.heatmap:如何指定值尺寸?

时间:2019-03-19 11:54:24

标签: python pandas dataframe bokeh holoviews

我有一个简单的数据框,其中包含要使用hvpolot.heatmap可视化的列和行。 我可以做一些类似的事情:

df.style.background_gradient(cmap='summer')

..在Jupyter中,看起来像: enter image description here

数据框非常简单:

> df.index
Index(['ackerland', 'friedhof', 'gartenland', 'gehoelz', 'golfplatz',
       'gruenland', 'heide', 'kleingarten', 'laubholz', 'mischholz', 'moor',
       'nadelholz'],
      dtype='object')
> df.columns
Index(['hiking', 'biking', 'walking', 'sport', 'friends', 'family', 'picnic'], dtype='object')

但是当我这样做时:

>import hvplot.pandas
>df.hvplot.heatmap(colorbar=True)
ValueError: Dimensions must be defined as a tuple, string, dictionary or Dimension instance, found a NoneType type.```

这也不起作用:

>df.hvplot.heatmap(x=df.index, y=df.columns, colorbar=True)
ValueError: The truth value of a Index is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我已经阅读了大多数与此相关的文档,但仍然不完全了解如何在hvplot / holoviews / bokeh中为pandas数据框指定值尺寸:

[edit]添加了feature request

2 个答案:

答案 0 :(得分:5)

1)如果数据采用的是宽格式,则将类别作为索引,将列作为值,例如:

+--------------------------+
|         colA  colB  colC |
+--------------------------+
| group1    10 5.000 1.200 |
| group2    12 3.000 4.500 |
| group3    14 1.200 2.300 |
+--------------------------+

然后,您只需使用hvplot> = 0.5来df.heatmap.hvplot()

import pandas as pd
import holoviews as hv
import hvplot.pandas
hv.extension('bokeh')

df = pd.DataFrame({
    'colA': [10, 12, 14],
    'colB': [5, 3.0, 1.2],
    'colC': [1.2, 4.5, 2.3]},
    index=['group1', 'group2', 'group3'],
)

df.hvplot.heatmap()

如果要向热图添加数据标签,可以执行以下操作:

heatmap = df.hvplot.heatmap()

heatmap * hv.Labels(heatmap)



2)但是,当您的数据是这样时,组只是另一列而不是索引:

+------------------------------+
|      group colA  colB  colC  |
+------------------------------+
| 1   group1    10 5.000 1.200 |
| 2   group2    12 3.000 4.500 |
| 3   group3    14 1.200 2.300 |
+------------------------------+

然后,您可以使用df.set_index('group')将组设置为索引(并应用解决方案1),也可以将数据melt设置为长格式:

df_melt = df.melt(id_vars='group')

融化数据后,如下所示:

+---+--------+----------+--------+
|   | group  | variable | value  |
+---+--------+----------+--------+
| 0 | group1 | colA     | 10.000 |
| 1 | group2 | colA     | 12.000 |
| 2 | group3 | colA     | 14.000 |
| 3 | group1 | colB     | 5.000  |
| 4 | group2 | colB     | 3.000  |
+---+--------+----------+--------+

此合并的数据采用您可以使用x和y以及C关键字的格式

df_melt.hvplot.heatmap(x='group', y='variable', C='value')

或者您可以使用融化的(长)数据在HoloViews中创建热图

hv.HeatMap(df_melt, kdims=['group', 'variable'], vdims=['value'])

融合数据的优点在于,您现在还可以轻松地将数据标签添加到热图:

heatmap = df_melt.hvplot.heatmap(x='group', y='variable', C='value')
labels = hv.Labels(data=df_melt, kdims=['group', 'variable'], vdims=['value'])

heatmap * labels

向热图添加数据标签/值的另一种(甚至)更简单的方法是这样的:

heatmap = df_melt.hvplot.heatmap(x='group', y='variable', C='value')
heatmap * hv.Labels(heatmap)



结果图:

nice heatmap with labels using hvplot or holoviews

有关hvplot中热图的更多信息:
https://hvplot.holoviz.org/reference/pandas/heatmap.html

有关holoviews中热图的更多信息:
https://holoviews.org/reference/elements/bokeh/HeatMap.html

有关holoviews中(数据)标签的更多信息:
https://holoviews.org/reference/elements/bokeh/Labels.html

答案 1 :(得分:1)

对于你想要的,你可以这样做:

df.hvplot.heatmap(x='index', y='columns', colorbar=True)