Python 没有池的堆叠条形图的问题

Python 没有池的堆叠条形图的问题,python,matplotlib,seaborn,data-science,bioinformatics,Python,Matplotlib,Seaborn,Data Science,Bioinformatics,我很清楚我想要绘制什么,但我不确定从哪里开始使用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

我很清楚我想要绘制什么,但我不确定从哪里开始使用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行,如果您使用图像而不是条形图来显示数据,我想您会感觉更好。将数据(
0
1
2
)放入一个999行的数组中,列数与最长的序列数相同,然后使用
-1
初始化该数组

示例仅包含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)


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

对于1000行,如果您使用图像而不是条形图来显示数据,我想您会更好。将数据(
0
1
2
)放入一个999行的数组中,列数与最长的序列数相同,然后使用
-1
初始化该数组

示例仅包含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)


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

我没有料到堆栈溢出会出现迷幻之旅,但我们到了。我没有料到堆栈溢出会出现迷幻之旅,但我们到了。