bqplot中的GridHeatMap网格热力图使用详解

bqplot中的GridHeatMap网格热力图使用详解

bqplot Plotting library for IPython/Jupyter notebooks bqplot 项目地址: https://gitcode.com/gh_mirrors/bq/bqplot

概述

bqplot是一个基于Python的交互式可视化库,特别适合在Jupyter环境中使用。其中GridHeatMap(网格热力图)是一种强大的数据可视化工具,能够直观地展示二维数据矩阵中数值的分布情况。本文将详细介绍如何使用bqplot创建和定制网格热力图。

基础准备

首先需要导入必要的库:

import numpy as np
import bqplot.pyplot as plt
from bqplot import LinearScale

创建基础热力图

生成随机数据作为示例:

np.random.seed(0)
data = np.random.randn(10, 10)

创建基础热力图非常简单:

fig = plt.figure(padding_y=0.0)
grid_map = plt.gridheatmap(data)
fig

自定义显示格式

可以设置热力图中数值的显示格式和样式:

grid_map.display_format = ".2f"  # 保留两位小数
grid_map.font_style = {
    "font-size": "16px", 
    "fill": "blue", 
    "font-weight": "bold"
}

控制坐标轴显示

通过axes_options参数可以控制坐标轴的可见性:

axes_options = {
    "column": {"visible": False},
    "row": {"visible": False},
    "color": {"visible": False},
}

fig = plt.figure(padding_y=0.0)
grid_map = plt.gridheatmap(data, axes_options=axes_options)
fig

非均匀热力图

热力图的行列可以是非均匀分布的:

fig = plt.figure(padding_y=0.0)
plt.scales(scales={"x": LinearScale(), "y": LinearScale(reverse=True)})

# 创建非均匀行数据
row_data = np.arange(10)
row_data[5:] = np.arange(6, 11)  # 第5行(从顶部数)高度是其他行的两倍
column_data = np.arange(10, 20)

grid_map = plt.gridheatmap(data, row=row_data, column=column_data)
fig

数据对齐方式

网格热力图提供了多种数据对齐方式:

  1. 默认对齐方式:数据对齐到网格矩形的起始位置
fig = plt.figure(padding_y=0.0)
plt.scales(scales={"x": LinearScale(), "y": LinearScale(reverse=True, max=15)})

row_data = np.arange(10)
column_data = np.arange(10, 20)

grid_map = plt.gridheatmap(data, row=row_data, column=column_data)
fig
  1. 末端对齐方式:使用row_align="end"参数
fig = plt.figure(padding_y=0.0)
plt.scales(scales={"x": LinearScale(), "y": LinearScale(reverse=True, min=-5, max=15)})

grid_map = plt.gridheatmap(data, row=row_data, column=column_data, row_align="end")
fig

样式自定义

可以调整热力图的透明度和边框样式:

fig = plt.figure(padding_y=0.0)
grid_map = plt.gridheatmap(data, opacity=0.3, stroke="white", axes_options=axes_options)
fig

交互式选择

网格热力图支持类似Excel的单元格选择功能:

data = np.random.randn(10, 10)
fig = plt.figure(padding_y=0.0)
grid_map = plt.gridheatmap(
    data,
    interactions={"click": "select"},
    selected_style={"stroke": "blue", "stroke-width": 3},
    axes_options=axes_options,
)

fig

可以通过grid_map.selected属性获取当前选中的单元格索引。

事件处理

可以为热力图添加点击事件处理器:

from IPython.display import display
from ipywidgets import Output

out = Output()

@out.capture()
def print_event(self, target):
    print(target)

grid_map.on_element_click(print_event)

display(figure)
display(out)

总结

bqplot的GridHeatMap提供了丰富的功能来创建和定制网格热力图。通过本文介绍的各种参数和方法,您可以:

  1. 创建基础或非均匀的热力图
  2. 自定义数据显示格式和样式
  3. 控制坐标轴显示
  4. 调整数据对齐方式
  5. 修改视觉样式如透明度和边框
  6. 实现交互式选择和事件处理

这些功能使得GridHeatMap成为分析二维数据分布的有力工具,特别适合在Jupyter环境中进行数据探索和可视化分析。

bqplot Plotting library for IPython/Jupyter notebooks bqplot 项目地址: https://gitcode.com/gh_mirrors/bq/bqplot

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江涛奎Stranger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值