1
我有以下熊猫数据框。基本上,7个不同的行动类别,5个不同的目标,每个类别有1个或许多独特的终点,然后每个终点在每个目标得到一定的分数。 总共有250个端点。Python seaborn热图网格 - 未采取预期的列
action,target,endpoint,score
Category1,target1,endpoint1,813.0
Category1,target2,endpoint1,757.0
Category1,target3,endpoint1,155.0
Category1,target4,endpoint1,126.0
Category1,target5,endpoint1,75.5
Category2,target1,endpoint2,106.0
Category2,target1,endpoint3,101.0
Category2,target1,endpoint4,499.0
Category2,target1,endpoint5,207.0
Category2,target2,endpoint2,316.0
Category2,target2,endpoint3,208.0
Category2,target2,endpoint4,161.0
Category2,target2,endpoint5,198.0
<omit>
Category3,target1,endpoint8,193.0
Category3,target1,endpoint9,193.0
Category3,target1,endpoint10,193.0
Category3,target1,endpoint11,193.0
Category3,target2,endpoint8,193.0
Category3,target2,endpoint9,193.0
<List goes on...>
现在,我想将此数据框映射为每个类别的热图。 所以,我用下面的代码使用了seabron facet网格热图。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_csv('rawData.csv')
data = data.drop('Unnamed: 0', 1)
def facet_heatmap(data, **kwargs):
data2 = data.pivot(index="target", columns='endpoint', values='score')
ax1 = sns.heatmap(data2, cmap="YlGnBu", linewidths=2)
for item in ax1.get_yticklabels():
item.set_rotation(0)
for item in ax1.get_xticklabels():
item.set_rotation(70)
with sns.plotting_context(font_scale=5.5):
g = sns.FacetGrid(data, col="action", col_wrap=7, size=5, aspect=0.5)
cbar_ax = g.fig.add_axes([.92, .3, .02, .4])
g = g.map_dataframe(facet_heatmap, cbar=cbar_ax, min=0, vmax=2000)
# <-- Specify the colorbar axes and limits
g.set_titles(col_template="{col_name}", fontweight='bold', fontsize=18)
g.fig.subplots_adjust(right=3) # <-- Add space so the colorbar doesn't overlap the plot
plt.savefig('seabornPandas.png', dpi=400)
plt.show()
它实际上会生成热图表格。但是,问题是每个heatmap出于某种原因使用相同的列。请参阅下面的附图。
这是很奇怪的。首先,索引不合适。其次,每个heatmap框仅包含最后三个端点(端点248,249和250)。这是不正确的。对于第1类,它应该只需要端点1。我不指望有一个灰色框..
对于类别2,它应该采取端点2,3,4,5。不是端点248,249,250。
我该如何解决这两个问题?任何建议或意见,欢迎。
你确保'data2'看起来lthe方式,您认为它看起来? –
听起来就像你需要关闭x轴共享,但是一般来说,如果x因子嵌套在col因子中(而不是交叉),它不完全是用方面网格绘制的正确结构。 – mwaskom
@AndrasDeak是的,我确认数据2看起来如预期。每个目标都显示每个端点。 –