Python seaborn 热图网格 - 未采用预期的列
Python seaborn heatmap grid - Not taking expected columns
我有以下 pandas 数据框。基本上,7 个不同的动作类别,5 个不同的目标,每个类别有 1 个或多个独特的端点,然后每个端点在每个目标中获得一定的分数。
共有 250 个端点。
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 面网格热图。
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()
它实际上生成热图网格。但是,问题是由于某种原因每个热图都使用相同的列。请参阅下面随附的屏幕截图。
(请忽略颜色条和限制。)
这很奇怪。首先,索引不有序。其次,每个热图框只取最后三个端点(端点 248、249 和 250)。这是不正确的。对于类别 1,它应该只采用端点 1。我不希望那里有灰色框..
对于类别 2,它应该采用端点 2、3、4、5。不是端点 248、249、250。
我该如何解决这两个问题?欢迎任何建议或意见。
正如 mwaskom 所建议的:使用 sharex 参数来解决您的问题:
...
with sns.plotting_context(font_scale=5.5):
g = sns.FacetGrid(data, col="action", col_wrap=7, size=5, aspect=0.5,
sharex=False)
...
我有以下 pandas 数据框。基本上,7 个不同的动作类别,5 个不同的目标,每个类别有 1 个或多个独特的端点,然后每个端点在每个目标中获得一定的分数。 共有 250 个端点。
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 面网格热图。
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()
它实际上生成热图网格。但是,问题是由于某种原因每个热图都使用相同的列。请参阅下面随附的屏幕截图。
这很奇怪。首先,索引不有序。其次,每个热图框只取最后三个端点(端点 248、249 和 250)。这是不正确的。对于类别 1,它应该只采用端点 1。我不希望那里有灰色框..
对于类别 2,它应该采用端点 2、3、4、5。不是端点 248、249、250。
我该如何解决这两个问题?欢迎任何建议或意见。
正如 mwaskom 所建议的:使用 sharex 参数来解决您的问题:
...
with sns.plotting_context(font_scale=5.5):
g = sns.FacetGrid(data, col="action", col_wrap=7, size=5, aspect=0.5,
sharex=False)
...