Issue
I have 3 lists, each containing numbers, representing a time. The time represents occuring of an event. For example, in this A
, I have a number for each occurence of event A
. I want to represent this data on a graph. In either of the following two ways:
1)
aabaaabbccacac
2)
a-> xx xxx x x
b-> x xx
c-> xx x x
Solution
You could use plt.hlines:
import matplotlib.pyplot as plt
import random
import numpy as np
import string
def generate_data(N = 20):
data = [random.randrange(3) for x in range(N)]
A = [i for i, x in enumerate(data) if x == 0]
B = [i for i, x in enumerate(data) if x == 1]
C = [i for i, x in enumerate(data) if x == 2]
return A,B,C
def to_xy(*events):
x, y = [], []
for i,event in enumerate(events):
y.extend([i]*len(event))
x.extend(event)
x, y = np.array(x), np.array(y)
return x,y
def event_string(x,y):
labels = np.array(list(string.uppercase))
seq = labels[y[np.argsort(x)]]
return seq.tostring()
def plot_events(x,y):
labels = np.array(list(string.uppercase))
plt.hlines(y, x, x+1, lw = 2, color = 'red')
plt.ylim(max(y)+0.5, min(y)-0.5)
plt.yticks(range(y.max()+1), labels)
plt.show()
A,B,C = generate_data(20)
x,y = to_xy(A,B,C)
print(event_string(x,y))
plot_events(x,y)
yields
BBACBCACCABACCBCABCC
Answered By - unutbu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.