색상 그라디언트를 사용하여 선형 음영 플롯에서 파이썬 목록 값을 그리는 방법

Nov 13 2020

아래 줄거리를 무엇이라고 부를지 모르겠습니다. 1D 리본 플롯?

기본적으로 목록에 이와 같은 것을 만들고 싶습니다 (풍부한 색상의 상자가있는 선형 플롯). 그러한 음모의 이름에 대한 모든 단서 또는 목록에서 풍부함을 시각화하는 방법이 도움이 될 것입니다.

보너스

그림에 표시된 이미지와 같이 플롯 위에 (다른 목록 또는 사전 개체의) 텍스트를 추가하는 방법에 대한 생각이 있다면 감사 할 것입니다.

답변

3 xjcl Nov 13 2020 at 05:47

다음은 기반으로 한 접근 방식입니다 pcolor.

import numpy as np
import matplotlib.pyplot as plt

labels = ['All cellular component', 'extracellular region', 'plasm membrane',
          'synapse', 'cell junction', 'cell protection', 'cytoplasmic vesicle',
          'endosome', 'vacuole', 'golgi apparatus', 'endoplasmic reticulum',
          'cytosol', 'mitochondrion', 'nucleus', 'chromosome', 'cytoskeleton',
          'protein-containing complex', 'other components']

# Dummy values based on word length
values = np.array([[len(l) for l in labels]]) - min(map(len, labels))

fig, ax = plt.subplots()
im = ax.pcolor(np.arange(len(labels)+1) - .5, [0, 1],  # center xticks like imshow
               values, cmap='Blues', edgecolors='grey', linewidths=1)
for spine in ax.spines.values():
    spine.set_edgecolor('grey')

ax.set_xticklabels(labels)
ax.set_xticks( np.arange(len(labels)) )  # Show all data
ax.set_yticks([])  # No Y axis

ax.xaxis.tick_top()  # Put labels on top
plt.xticks(rotation=45, ha="left", rotation_mode="anchor")  # Rotate labels 45 deg
plt.axis('scaled')  # square pixels
ax.tick_params(axis='both', which='both', length=0)  # Hide ticks

fig.tight_layout()
plt.show()