没有汇集的堆积条形图的问题

Trouble with Stacked bar graph without pooling

我很清楚我想绘制什么,但我不确定从哪里开始使用 matplotlib/seaborn。

我有 ~999 不等 行 0、1 和 2。这是一行的示例:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1]

我想制作一个水平图,其中每个数字映射到某个设定单位长度的颜色。

这看起来像是堆积条形图。但我相信 plt.bar 的功能会汇集值,这样只能有三种连续的颜色。但是我需要实现图表,以便在颜色之间可以进行任意数量的切换。

对于 1000 行,我想如果使用 imshow 而不是条形图将数据显示为图像会更好。将您的数据(012)放入一个包含 999 行和与最长序列一样多的列的数组中,用 -1.[=18 初始化该数组=]

示例只有 100 行以提高可读性:

import matplotlib.pyplot as plt
import numpy as np

# generate some sample data
n, m = 10, 20
a = np.random.randint(0, 3, (n,m))
s = np.random.randint(int(m/2), m, n)
for i in range(n):
    a[i,s[i]:] = -1

# show them as image
cmap = plt.matplotlib.colors.ListedColormap(['w', 'r', 'lime', 'b'])
plt.imshow(a, cmap=cmap)

然后您可以根据需要调整轴刻度和标签。