堆积条形图麻烦而无池 [英] Trouble with Stacked bar graph without pooling

查看:49
本文介绍了堆积条形图麻烦而无池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对要绘制的内容有清楚的了解,但是我不确定从哪里开始使用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,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,2,1,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,1,2,2,2,2,2,2,2,2,2,2,2,1,1] 

我想做一个水平图,其中每个数字都映射到某种已设置单位长度的颜色.

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

解决方案

对于1000行,我想如果使用

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

I have a clear idea of what I would like to plot, but I am not sure where to start using matplotlib/seaborn.

I have ~999 unequal lines of 0s, 1s, and 2. Here is an example of one line:

[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]

I would like to make a horizontal plot where each number maps to a color of some set unit length.

This looks like a stacked bar plot. But I believe the the functionality of plt.bar would pool values such that there could only be three contiguous colors. However I need to implement the graph such that there could be any number of switches between colors.

解决方案

With 1000 rows I guess you're better off if you display your data as an image using imshow rather than a bar chart. Put your data (0, 1 or 2) in an array of 999 rows and as many columns as the longest sequence, initilize that array with -1.

Example with just 100 rows for better readability:

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)

You can then adjust axis ticks and labels as needed.

这篇关于堆积条形图麻烦而无池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆