0

I'm importing data from a .json file, where I transformed the dictionary into a list of tuples. These tuples represent the data as a timestamp and a value marked at that specified timestamp, such as this example:

participant_1 = [(1, 8), (2, 2), (3, 2), (4, 1), (5, 3), (6, 5), (7, 6), (8, 6), (9, 8), (10, 9), (11, 9), (12, 9), (13, 3), (14, 3), (15, 4), (16, 5), (17, 6), (18, 6), (19, 7), (20, 8), (21, 8), (22, 9), (23, 9), (24, 9), (25, 9), (26, 9), (27, 9)]
participant_2 = [(1, 5), (2, 5), (3, 1), (4, 3), (5, 4), (6, 5), (7, 5), (8, 7), (9, 8), (10, 9), (11, 10), (12, 10), (13, 10), (14, 10), (15, 10), (16, 10), (17, 10), (18, 0), (19, 0), (20, 0), (21, 0), (22, 0), (23, 0), (24, 0), (25, 0), (26, 0), (27, 0)]

I'll have multiple lists (of multiple participants) where the timestamp (first value of the tuple) will not change but the second (marked value) will. What I want to do is plot a graph where I can compare the marked values (therefore, the x-axis will be the time and the y-axis the marked values).

The way I want to compare the data is by horizontal bars where a different color would represent the marked value. These values range from 0 - 10. Thus, for each of these values, I would like to assign a color. In this way, there would be multiple horizontal bars, for each participant, and for each marked value, a different color (so that I can see the differences between the marked values of participants).

I do not wish for multiple bars for each participant - more like a stacked graph where the marked value would be one color, and those change according to the timestamp. In this way, I would be able to compare the marked values of the participants in a timeframe. I have an example from a paper:

Example

However, I couldn't find any way to do this yet.

Thanks.

  • Does this answer your question? [Python matplotlib multiple bars](https://stackoverflow.com/questions/14270391/python-matplotlib-multiple-bars) – Jan Jaap Meijerink Jan 24 '22 at 15:22
  • Not exactly. I think it is similar to this: https://stackoverflow.com/questions/38830250/how-to-fill-matplotlib-bars-with-a-gradient However, I am still in doubt in how to set up the colors to a specific number. Thanks – Arthur Faraco Jan 24 '22 at 16:44

1 Answers1

0

You could convert each list to a dataframe, using the timestamp as index. The concatenation of these lists as columns to an assembling dataframe can be shown as a heatmap.

Here is some example code:

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

participant_1 = [(1, 8), (2, 2), (3, 2), (4, 1), (5, 3), (6, 5), (7, 6), (8, 6), (9, 8), (10, 9), (11, 9), (12, 9), (13, 3), (14, 3), (15, 4), (16, 5), (17, 6), (18, 6), (19, 7), (20, 8), (21, 8), (22, 9), (23, 9), (24, 9), (25, 9), (26, 9), (27, 9)]
participant_2 = [(1, 5), (2, 5), (3, 1), (4, 3), (5, 4), (6, 5), (7, 5), (8, 7), (9, 8), (10, 9), (11, 10), (12, 10), (13, 10), (14, 10), (15, 10), (16, 10), (17, 10), (18, 0), (19, 0), (20, 0), (21, 0), (22, 0), (23, 0), (24, 0), (25, 0), (26, 0), (27, 0)]

participants = [participant_1, participant_2]
names = ['participant 1', 'participant 2']
pd.concat({name: pd.DataFrame(particip_data, columns=['timestamp', name]).set_index('timestamp')
           for name, particip_data in zip(names, participants)}).reset_index()

full_df = pd.concat([pd.DataFrame(particip_data, columns=['timestamp', name]).set_index('timestamp')
                     for name, particip_data in zip(names, participants)],
                    axis=1)
fig, ax = plt.subplots(figsize=(15, 3))
cmap = plt.get_cmap('turbo', 11)
sns.heatmap(ax=ax, data=full_df.T, annot=True,
            cmap='turbo', vmin=-0.5, vmax=10.5, cbar_kws={'ticks': np.arange(11), 'pad': 0.02})
ax.tick_params(labelrotation=0)
plt.tight_layout()
plt.show()

heatmap of concatenated lists

JohanC
  • 71,591
  • 8
  • 33
  • 66