나는 비슷한 스레드를 살펴 보았지만 그들 중 어느 것도 나를 위해 일하지 않았습니다. 내가하려는 것은 히트 맵과 높이가 같은 끝에 각 행에 대한 조인트 컬러 바가있는 2x4 히트 맵을 표시하는 것입니다.
axes
각 행에 4 개의 행렬과 1 개의 컬러 바 축이 끝에있는 2x5로 만듭니다 . l2_distances
여기에는 각 키 MAPPING_DICT
가 행렬 (모두 동일한 크기)과 연결된 4 개의 항목이있는 사전 이 있습니다 .
가장 좋은 방법은 cbar=False
내가 플로팅하는 각 히트 맵에 대해 설정 하고 축 (0-3)에 배치하는 반면 각 행의 마지막 히트 맵, 즉 인덱스에서 축에 플로팅 된 히트 맵 3
이 cbar_ax=axes[0,4]
.
import seaborn as sns
import matplotlib.pyplot as plt
MAPPING_DICT = {"P": 0, "A": 1, "C": 2, "S": 3}
fig, axes = plt.subplots(2,5, sharex=True, sharey=True)
for env_name in l2_distances:
l2_dist_matrix = l2_distances[env_name]
cbar_flag = True if MAPPING_DICT[env_name]==3 else False
sns.heatmap(l2_dist_matrix, ax=axes[0, MAPPING_DICT[env_name]], linewidths=0.2, square=True, cbar=cbar_flag, cbar_ax=axes[0,4], cmap="Blues", xticklabels=False, yticklabels=False, robust=True)
그러나 이것은 컬러 바가 (다소) 올바른 지점에 플로팅되지만 레이블이없고 높이가 잘못되어 있기 때문에 제대로 작동하지 않습니다. 다음은 (컬러 맵 동작에 영향을주지 않는 몇 가지 추가 시각화 추가가있는) 모양의 맨 위 행뿐입니다. 맨 아래 행은 기본적으로 유사합니다.
나는 명시 적으로 새로운 축 위치를 설정하는 것으로 놀았지만 이것은 다소 지루하고 잘 작동하지 않습니다. 내가 놓친 것이 있습니까?
가장 큰 문제는 사용하는 것입니다 sharex=True
및 sharey=True
년 Colorbar에게 줄거리의 나머지 부분과 동일한 축을 제공 할 것입니다. 이것은 컬러 바를 너무 엉망으로 만듭니다.
Matplotlib의 두 축에서 sharex 또는 sharey를 설정 해제하는 방법은 컬러 바에 대한 공유를 제거하는 방법을 보여줍니다. 이것은 여전히 내가 해결할 수없는 까다로운 부작용이 있습니다.
이 솔루션은 다음과 줄거리 생성 sharex=False
및 sharey=False
colorbars 제외한 모든 줄거리를 공유 시작 후합니다. 컬러 바는 다른 서브 플롯만큼 넓을 필요가 없으므로 적절 width_ratios
하게 설정할 수 있습니다.
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
MAPPING_DICT = {"P": 0, "A": 1, "C": 2, "S": 3}
l2_distances = {"P": np.random.rand(10, 10), "A": np.random.rand(10, 10), "C": np.random.rand(10, 10),
"S": np.random.rand(10, 10)}
fig, axes = plt.subplots(nrows=2, ncols=5, sharex=False, sharey=False, figsize=(16, 8),
gridspec_kw={'width_ratios': [10, 10, 10, 10, 1]})
shax = axes[0, 0].get_shared_x_axes()
shay = axes[0, 0].get_shared_y_axes()
for ax in axes[:, :-1].ravel():
shax.join(axes[0, 0], ax)
shay.join(axes[0, 0], ax)
for row in range(axes.shape[0]):
for env_name in l2_distances:
l2_dist_matrix = l2_distances[env_name]
print(env_name, l2_dist_matrix.shape)
cbar_flag = True if MAPPING_DICT[env_name] == 3 else False
sns.heatmap(l2_dist_matrix, ax=axes[row, MAPPING_DICT[env_name]], linewidths=0.2, square=True,
cbar=cbar_flag, cbar_ax=axes[row, -1], cmap="Blues", xticklabels=False, yticklabels=False,
robust=True)
plt.show()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다