具有DataFrame的热图 [英] Heat Map with DataFrame
本文介绍了具有DataFrame的热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个格式为pandas的熊猫框
I have a pandas data frame of the form
State RF LOG KNN MLP DT LDA AB
0 AR 0.95 0.87 0.81 0.89 0.81 0.84 0.87
1 FL 0.83 0.86 0.85 0.86 0.89 0.82 0.85
2 NJ 0.89 0.81 0.88 0.83 0.89 0.84 0.83
3 NV 0.77 0.72 0.89 0.79 0.79 0.73 0.70
4 TX 0.71 0.70 0.71 0.77 0.70 0.70 0.92
5 CA 0.69 0.81 0.81 0.88 0.88 0.60 0.89
例如,如何在Seaborn上绘制热图,使它在X轴上的列名称为:[RF,LOG,KNN,MLP,DT,LDA,AB],在Y-纵轴表示列状态[AR,FL,NJ,NV,TX,CA]的名称,并且在正方形中显示的相应值是热"值.指标?
How could I make a heat map, for example, on Seaborn, that in the X-axis has the names of the columns: [RF, LOG, KNN, MLP, DT, LDA, AB], in the Y-axis the names of the column State [AR, FL, NJ, NV, TX, CA], and the corresponding values, displayed in the squares, are the "heat" indicators?
推荐答案
如果为状态列建立索引,则可以直接绘制热图.
If you index the columns of the states, you can draw a heat map directly.
import pandas as pd
import numpy as np
import io
import seaborn as sns
sns.set_theme()
data = '''
State RF LOG KNN MLP DT LDA AB
0 AR 0.95 0.87 0.81 0.89 0.81 0.84 0.87
1 FL 0.83 0.86 0.85 0.86 0.89 0.82 0.85
2 NJ 0.89 0.81 0.88 0.83 0.89 0.84 0.83
3 NV 0.77 0.72 0.89 0.79 0.79 0.73 0.70
4 TX 0.71 0.70 0.71 0.77 0.70 0.70 0.92
5 CA 0.69 0.81 0.81 0.88 0.88 0.60 0.89
'''
df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
df.set_index('State', inplace=True)
ax = sns.heatmap(df)
这篇关于具有DataFrame的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文