Matplot: How to build true / false or active / deactivated data?

I want to build binary data true/false or active/deactive , similar to the following image: True / False Plot

The horizontal axis is time, and the vertical axis is some objects (here are some sensors) that are active (white) or deactivated (black). How can I build such graphs using pyplot .

I searched to find the name of these graphs, but I could not find it.

+8
python scipy matplotlib plot
source share
1 answer

What you are looking for is imshow :

 import matplotlib.pyplot as plt import numpy as np # get some data with true @ probability 80 % data = np.random.random((20, 500)) > .2 fig = plt.figure() ax = fig.add_subplot(111) ax.imshow(data, aspect='auto', cmap=plt.cm.gray, interpolation='nearest') 

Then you just need to get the Y shortcuts somewhere.

enter image description here

It seems that the image in your question has some interpolation on the image. Let's have a few more things:

 import matplotlib.pyplot as plt import numpy as np # create a bit more realistic-looking data # - looks complicated, but just has a constant switch-off and switch-on probabilities # per column # - the result is a 20 x 500 array of booleans p_switchon = 0.02 p_switchoff = 0.05 data = np.empty((20,500), dtype='bool') data[:,0] = np.random.random(20) < .2 for c in range(1, 500): r = np.random.random(20) data[data[:,c-1],c] = (r > p_switchoff)[data[:,c-1]] data[-data[:,c-1],c] = (r < p_switchon)[-data[:,c-1]] # create some labels labels = [ "label_{0:d}".format(i) for i in range(20) ] # this is the real plotting part fig = plt.figure() ax = fig.add_subplot(111) ax.imshow(data, aspect='auto', cmap=plt.cm.gray) ax.set_yticks(np.arange(len(labels))) ax.set_yticklabels(labels) 

creates enter image description here

However, interpolation is not always good here. To simplify the separation of different lines, you can use colors:

 import matplotlib.pyplot as plt import matplotlib.colors import numpy as np # create a bit more realistic-looking data # - looks complicated, but just has a constant switch-off and switch-on probabilities # per column # - the result is a 20 x 500 array of booleans p_switchon = 0.02 p_switchoff = 0.05 data = np.empty((20,500), dtype='bool') data[:,0] = np.random.random(20) < .2 for c in range(1, 500): r = np.random.random(20) data[data[:,c-1],c] = (r > p_switchoff)[data[:,c-1]] data[-data[:,c-1],c] = (r < p_switchon)[-data[:,c-1]] # create some labels labels = [ "label_{0:d}".format(i) for i in range(20) ] # create a color map with random colors colmap = matplotlib.colors.ListedColormap(np.random.random((21,3))) colmap.colors[0] = [0,0,0] # create some colorful data: data_color = (1 + np.arange(data.shape[0]))[:, None] * data # this is the real plotting part fig = plt.figure() ax = fig.add_subplot(111) ax.imshow(data_color, aspect='auto', cmap=colmap, interpolation='nearest') ax.set_yticks(np.arange(len(labels))) ax.set_yticklabels(labels) 

creates

enter image description here

Of course, you will want to use something less strange, like a coloring scheme, but it really depends on your artistic views. Here the fact is that all True elements in line n have the value n+1 , and all False elements are 0 in data_color . This allows you to create a color map. Naturally, if you need a cyclic color map with two or three colors, just use the data_color module in imshow , for example, data_color % 3 .

+15
source share

All Articles