绘制2D数据:具有不同颜色图的热图 [英] Plotting of 2D data : heatmap with different colormaps
问题描述
我想可视化我拥有的2D数据.例如,以下是具有四个属性的数据:
I want to visualize 2D data that I have. For example following is the data with four attributes:
att1 att2 att3
fun1 10 0 2
fun2 0 1 3
fun3 1 10 5
fun4 2 3 10
我想为每个数据点分配不同的颜色.颜色的强度取决于该列中属性的值,并且每列必须具有不同的颜色.
I want to assign each data point a different colour. The intensity of the color will depend on the value of the attribute in that column, and each column must have a different color.
以下是所需的图像:
有人知道我如何用Python或R制作它吗?
Does anyone have any idea how I can make it in Python or R?
推荐答案
使用Python:
我找到了更好的方法:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# data loading
df = pd.read_csv("file.csv", index_col=0)
# plotting
fig,ax = plt.subplots()
ax.matshow(df.mask(((df == df) | df.isnull()) & (df.columns != "att1")),
cmap=cm.Reds) # You can change the colormap here
ax.matshow(df.mask(((df == df) | df.isnull()) & (df.columns != "att2")),
cmap=cm.Greens)
ax.matshow(df.mask(((df == df) | df.isnull()) & (df.columns != "att3")),
cmap=cm.Blues)
plt.xticks(range(3), df.columns)
plt.yticks(range(4), df.index)
plt.show()
一些细节:
df.mask(((df == df) | df.isnull()) & (df.columns != "att1"))
att1 att2 att3
fun1 10 NaN NaN
fun2 0 NaN NaN
fun3 1 NaN NaN
fun4 2 NaN NaN
较旧的版本,带有numpy掩码数组:
Older version, with numpy masked array :
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from numpy.ma import masked_array
import numpy as np
df = pd.read_clipboard() # just copied your example
# define masked arrays to mask all but the given column
c1 = masked_array(df, mask=(np.ones_like(df)*(df.values[0]!=df.values[0][0])))
c2 = masked_array(df, mask=(np.ones_like(df)*(df.values[0]!=df.values[0][1])))
c3 = masked_array(df, mask=(np.ones_like(df)*(df.values[0]!=df.values[0][2])))
fig,ax = plt.subplots()
ax.matshow(c1,cmap=cm.Reds) # You can change the colormap here
ax.matshow(c2,cmap=cm.Greens)
ax.matshow(c3,cmap=cm.Blues)
plt.xticks(range(3), df.columns)
plt.yticks(range(4), df.index)
一些细节:
df是一个数据框:
att1 att2 att3
fun1 10 0 2
fun2 0 1 3
fun3 1 10 5
fun4 2 3 10
c1,c2,c3是掩码数组(针对第1、2和3列):
c1, c2, c3 are masked arrays (for columns 1, 2 and 3):
>>> c1
masked_array(data =
[[10 -- --]
[0 -- --]
[1 -- --]
[2 -- --]],
mask =
[[False True True]
[False True True]
[False True True]
[False True True]],
fill_value = 999999)
或者,您可以从一个numpy的2D数组开始:
alternatively, you can start from a numpy 2D array :
>> data
array([[10, 0, 2],
[ 0, 1, 3],
[ 1, 10, 5],
[ 2, 3, 10]])
,并将所有df
和df.values
替换为data
(二维数组),除了标签部分.
and replace all df
and df.values
with data
(the 2D array), except in the labeling part.
这篇关于绘制2D数据:具有不同颜色图的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!