【发布时间】:2019-08-02 03:03:09
【问题描述】:
我希望 seaborn 热图在热图的每个单元格中显示多个值。为了清楚起见,这是我想看到的手动示例:
data = np.array([[0.000000,0.000000],[-0.231049,0.000000],[-0.231049,0.000000]])
labels = np.array([['A\nExtra Stuff','B'],['C','D'],['E','F']])
fig, ax = plt.subplots()
ax = sns.heatmap(data, annot = labels, fmt = '')
这里作为示例,让 seaborn.heat 在单元格中显示 flightsRoundUp 值。
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
def RoundUp(x):
return int(np.ceil(x/10)*10)
# Load the example flights dataset and conver to long-form
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
flightsRoundUp = flights.applymap(RoundUp)
# Draw a heatmap with the numeric values in each cell
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, annot=flightsRoundUp, fmt="", linewidths=.5, ax=ax)
在所有单元格中同时显示flightsRoundUp 和flights 的最佳方式是什么?类似于上面的第一个手动示例,但对于所有单元格都以类似矢量的方式...
【问题讨论】:
-
你的意思是this?
-
不完全是。但这帮助我将问题减少到能够通过
np.core.defchararray.add(X, Y)组合 X 和 Y,我还没有完全弄清楚。以下是 X 和 Y 示例:X = pd.DataFrame({'a':[1, 2, np.nan], 'b':[10, np.nan, 30]}) Y = pd.DataFrame({'A':[11, 222, np.nan], 'B':[110, np.nan, 330]}) -
如果元素都是没有任何nan的字符串。您分享的上述链接将是我问题的确切解决方案。
-
你试过
...add(X.values.astype(str), Y.values.astype(str))吗? -
@ImportanceOfBeingErnest 这行得通。谢谢!