I have a dataframe of the following form:
import pandas as pd
df = pd.DataFrame({'t': [0, 1, 2, 3, 4, 5, 6],
'l': [["c", "d"], ["a", "b"], ["c", "d"], ["a", "b"], ["c", "d"], ["c", "d"], ["c", "d"]]})
The column l consists of lists, where the list-entries can either be in the set {a,b,c,d}. I want to plot the contents of l for each value of t in the following manner which basically shows which of the four possible values {a,b,c,d} are acticated at a time t:
In order to create the above plot, what I did was to create the following dataframe based on df above (-1 is not activated, otherwise non-negative):
df_plot = pd.DataFrame({'t': [0, 1, 2, 3, 4,5,6],
'a': [-1, 0, -1, 0, -1,-1,-1],
'b': [-1, 1, -1, 1, -1,-1,-1],
'c': [2, -1, 2, -1, 2,2,2],
'd': [3, -1, 3, -1, 3,3,3]})
import numpy as np
ax = df_plot.plot(x="t", y=["a","b","c","d"],style='.', ylim=[-0.5,3.5], yticks=np.arange(0,3.1,1),legend=False)
labels = ["a","b","c","d"]
ax.set_yticklabels(labels)
This technically gives me what I want, however, I'd like to think that there is an easier and more professional way to plot this - is there a smarter way using one of Python's libraries?


t?tI just want to mark which one ofa,b,cordhave been activated. All possible combinations are possible, it is merely due to my laziness that the example above only has{a,b}and{c,d}