risk_factor_df= pd.read_csv("kag_risk_factors_cervical_cancer(1).csv") diagnoses_num_partner_compare_cols = ['Dx:Cancer', 'Dx:HPV', "Number_of_sexual_partners",] corr_matrix = risk_factor_df[diagnoses_num_partner_compare_cols].corr() print(corr_matrix) diagnoses_num_partner_heatmap = px.imshow(corr_matrix, aspect="auto", color_continuous_scale="gnbu", text_auto=True) diagnoses_num_partner_heatmap.show()用pyecharts绘图
时间: 2024-03-09 08:45:21 浏览: 184
三星8G flash芯片资料
5星 · 资源好评率100%
以下是使用 Pyecharts 绘制相关系数热力图的示例代码:
```
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import HeatMap
# 读取数据
risk_factor_df = pd.read_csv("kag_risk_factors_cervical_cancer(1).csv")
# 挑选需要绘制相关系数热力图的列
diagnoses_num_partner_compare_cols = ['Dx:Cancer', 'Dx:HPV', "Number_of_sexual_partners"]
# 计算相关系数矩阵
corr_matrix = risk_factor_df[diagnoses_num_partner_compare_cols].corr()
# 转换成二维列表
corr_list = []
for i in range(corr_matrix.shape[0]):
for j in range(corr_matrix.shape[1]):
corr_list.append([i, j, corr_matrix.iloc[i, j]])
# 绘制热力图
heatmap = (
HeatMap()
.add_xaxis(diagnoses_num_partner_compare_cols)
.add_yaxis("", diagnoses_num_partner_compare_cols, corr_list)
.set_global_opts(
title_opts=opts.TitleOpts(title="Correlation Heatmap"),
visualmap_opts=opts.VisualMapOpts(
min_=-1, max_=1, is_piecewise=True, pos_top="10%", pos_right="5%"
),
tooltip_opts=opts.TooltipOpts(formatter='{c}'),
)
)
# 显示图表
heatmap.render_notebook()
```
其中,我们使用了 Pandas 计算相关系数矩阵,然后将矩阵转换为二维列表,传递给 Pyecharts 的 HeatMap 组件绘制热力图。注意,我们设置了热力图的颜色范围、提示框格式等参数,可以根据需要自行修改。
阅读全文