简体   繁体   English

使用seaborn在python中绘制3列热图

[英]Plotting heatmap for 3 columns in python with seaborn

v1      v2      yy
15.25   44.34   100.00
83.05   59.78   100.00
96.61   65.09   100.00
100.00  75.47   100.00
100.00  50.00   100.00
100.00  68.87   100.00
100.00  79.35   100.00
100.00  100.00  100.00
100.00  63.21   100.00
100.00  100.00  100.00
100.00  68.87   100.00
0.00    56.52   92.86
10.17   52.83   92.86
23.73   46.23   92.86

In the dataframe above, I want to plot a heatmap using v1 and v2 as x and y axis and yy as the value. 在上面的数据框中,我想绘制一个热图,使用v1和v2作为x和y轴,yy作为值。 How can I do that in python? 我怎么能在python中做到这一点? I tried seaborn: 我试过seaborn:

df = df.pivot('v1', 'v2', 'yy')
ax = sns.heatmap(df)

However, this does not work. 但是,这不起作用。 Any other solution? 还有其他方法吗?

A seaborn heatmap plots categorical data. seaborn heatmap绘制了分类数据。 This means that each occuring value would take the same space in the heatmap as any other value, independent on how far they are separated numerically. 这意味着每个出现的值将在热图中占用与任何其他值相同的空间,而与它们在数字上分开的距离无关。 This is usually undesired for numerical data. 这对于数值数据通常是不期望的。 Instead one of the following techniques may be chosen. 而是可以选择以下技术之一。

Scatter

A colored scatter plot may be just as good as a heatmap. 彩色散点图可能与热图一样好。 The colors of the points would represent the yy value. 点的颜色代表yy值。

ax.scatter(df.v1, df.v2, c=df.yy,  cmap="copper")

在此输入图像描述

 u = u"""v1 v2 yy 15.25 44.34 100.00 83.05 59.78 100.00 96.61 65.09 100.00 100.00 75.47 100.00 100.00 50.00 100.00 100.00 68.87 100.00 100.00 79.35 100.00 100.00 100.00 100.00 100.00 63.21 100.00 100.00 100.00 100.00 100.00 68.87 100.00 0.00 56.52 92.86 10.17 52.83 92.86 23.73 46.23 92.86""" import pandas as pd import matplotlib.pyplot as plt import io df = pd.read_csv(io.StringIO(u), delim_whitespace=True ) fig, ax = plt.subplots() sc = ax.scatter(df.v1, df.v2, c=df.yy, cmap="copper") fig.colorbar(sc, ax=ax) ax.set_aspect("equal") plt.show() 

Hexbin

You may want to look into hexbin . 你可能想看看hexbin The data would be shown in hexagonal bins and the data is aggregated as the mean inside each bin. 数据将以六边形箱显示,数据汇总为每个箱内的平均值。 The advantage here is that if you choose the gridsize large, it will look like a scatter plot, while if you make it small, it looks like a heatmap, allowing to adjust the plot easily to the desired resolution. 这里的优点是,如果你选择gridsize large,它看起来就像一个散点图,而如果你把它做得很小,它看起来像一个热图,可以很容易地将图调整到所需的分辨率。

h1 = ax.hexbin(df.v1, df.v2, C=df.yy, gridsize=100, cmap="copper")
h2 = ax2.hexbin(df.v1, df.v2, C=df.yy, gridsize=10, cmap="copper")

在此输入图像描述

 u = u"""v1 v2 yy 15.25 44.34 100.00 83.05 59.78 100.00 96.61 65.09 100.00 100.00 75.47 100.00 100.00 50.00 100.00 100.00 68.87 100.00 100.00 79.35 100.00 100.00 100.00 100.00 100.00 63.21 100.00 100.00 100.00 100.00 100.00 68.87 100.00 0.00 56.52 92.86 10.17 52.83 92.86 23.73 46.23 92.86""" import pandas as pd import matplotlib.pyplot as plt import io df = pd.read_csv(io.StringIO(u), delim_whitespace=True ) fig, (ax, ax2) = plt.subplots(nrows=2) h1 = ax.hexbin(df.v1, df.v2, C=df.yy, gridsize=100, cmap="copper") h2 = ax2.hexbin(df.v1, df.v2, C=df.yy, gridsize=10, cmap="copper") fig.colorbar(h1, ax=ax) fig.colorbar(h2, ax=ax2) ax.set_aspect("equal") ax2.set_aspect("equal") ax.set_title("gridsize=100") ax2.set_title("gridsize=10") fig.subplots_adjust(hspace=0.3) plt.show() 

Tripcolor

A tripcolor plot can be used to obtain colored reagions in the plot according to the datapoints, which are then interpreted as the edges of triangles, colorized according the edgepoints' data. tripcolor绘图可用于根据数据点获取绘图中的彩色区域,然后将数据点解释为三角形的边缘,根据边缘点的数据进行着色。 Such a plot would require to have more data available to give a meaningful representation. 这样的情节需要有更多的数据来提供有意义的表示。

ax.tripcolor(df.v1, df.v2, df.yy,  cmap="copper")

在此输入图像描述

 u = u"""v1 v2 yy 15.25 44.34 100.00 83.05 59.78 100.00 96.61 65.09 100.00 100.00 75.47 100.00 100.00 50.00 100.00 100.00 68.87 100.00 100.00 79.35 100.00 100.00 100.00 100.00 100.00 63.21 100.00 100.00 100.00 100.00 100.00 68.87 100.00 0.00 56.52 92.86 10.17 52.83 92.86 23.73 46.23 92.86""" import pandas as pd import matplotlib.pyplot as plt import io df = pd.read_csv(io.StringIO(u), delim_whitespace=True ) fig, ax = plt.subplots() tc = ax.tripcolor(df.v1, df.v2, df.yy, cmap="copper") fig.colorbar(tc, ax=ax) ax.set_aspect("equal") ax.set_title("tripcolor") plt.show() 

Note that a tricontourf plot may equally be suited, if more datapoints throughout the grid are available. 请注意,如果整个网格中有更多数据点,则tricontourf可能同样适合。

ax.tricontourf(df.v1, df.v2, df.yy,  cmap="copper")

The problem that your data has duplicate values like: 您的数据具有重复值的问题,例如:

100.00  100.00  100.00
100.00  100.00  100.00

You have to drop duplicate values then pivot and plot like here: 您必须删除重复值,然后旋转并绘制如下:

import seaborn as sns
import pandas as pd

# fill data

df = pd.read_clipboard()
df.drop_duplicates(['v1','v2'], inplace=True)
pivot = df.pivot(index='v1', columns='v2', values='yy')
ax = sns.heatmap(pivot,annot=True)
plt.show()

print (pivot)

在此输入图像描述

Pivot: 枢:

v2      44.34   46.23   50.00   52.83   56.52   59.78   63.21   65.09   \
v1                                                                       
0.00       NaN     NaN     NaN     NaN   92.86     NaN     NaN     NaN   
10.17      NaN     NaN     NaN   92.86     NaN     NaN     NaN     NaN   
15.25    100.0     NaN     NaN     NaN     NaN     NaN     NaN     NaN   
23.73      NaN   92.86     NaN     NaN     NaN     NaN     NaN     NaN   
83.05      NaN     NaN     NaN     NaN     NaN   100.0     NaN     NaN   
96.61      NaN     NaN     NaN     NaN     NaN     NaN     NaN   100.0   
100.00     NaN     NaN   100.0     NaN     NaN     NaN   100.0     NaN   

v2      68.87   75.47   79.35   100.00  
v1                                      
0.00       NaN     NaN     NaN     NaN  
10.17      NaN     NaN     NaN     NaN  
15.25      NaN     NaN     NaN     NaN  
23.73      NaN     NaN     NaN     NaN  
83.05      NaN     NaN     NaN     NaN  
96.61      NaN     NaN     NaN     NaN  
100.00   100.0   100.0   100.0   100.0  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM