-1

Let say i have these two list.

p=[0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4,3,3,3,3,3,5,5,5,5]
q=[1,1,1,1,3,3,3,3,3,4,4,4,4,2,2,1,1,1,1,1,2,2,2,2,5,5,5,5,0,0,0,0]

I want to plot it like in this picture with the color represent the number in the list like green is 0 or yellow is 1. Above lists are not the real data for the graph in the picture below. The picture is just example of the graph i want to create. enter image description here

Aswad
  • 35
  • 6

1 Answers1

0

The given graph can be created using pandas as a horizontal stacked bar chart. You will need to convert your dataset to a dataframe before that though.

from itertools import groupby
data=[(key,len(list(group))) for key, group in groupby(p)]
print(data)

list1, list2 = zip(*data)

df2=pd.DataFrame([list2],columns=list1)
df2.plot(kind='bar', stacked=True)

Source

You can plot the same using matplotlib to customize the orientation, xticks, yticks, label methods, width and more to look like your requirement.

fireball.1
  • 1,413
  • 2
  • 17
  • 42
  • I tried your code but its slightly different from what i want. I need same color for a number like if there 4 between 3 like in list p, the second 3 still use same color like the first 3. If you notice in the pic, there are three times the green color comes up in first graph. – Aswad Feb 02 '20 at 07:51
  • You need to change how your data frame looks for something like this and associate colours according to that. This code is plotting the overall frequency in the array and not continuous frequency. – fireball.1 Feb 02 '20 at 08:14
  • can you tell me how should I change my data frame? – Aswad Feb 03 '20 at 04:45
  • 1
    I got it. My friend added these to your code and got the graph. Thank you for helping. color=["r", "g", "b", "k", "c", "m", "y"] colorlist = [color[i] for i in list1] – Aswad Feb 03 '20 at 05:44