2

I am trying to plot the sequence of a set of time sorted values, in which each value is represented by a different colour. For instance, if my values are like

state_seq = [2, 2, 2, 2, 0, 0, 0, 1, 3, 6, 3, 3, 3, 0, 0]
time_seq = ['2013-05-29 09:31:00', '2013-05-29 09:46:00', '2013-07-23 12:28:00', '2013-07-23 01:53:00', '2013-08-05 02:02:00', '2013-08-05 02:08:00', '2013-08-05 04:28:00', '2013-08-06 10:20:00', '2013-08-06 03:03:00', '2013-08-06 04:13:00', '2013-08-06 04:17:00', '2013-08-06 04:36:00', '2013-08-07 11:07:00', '2013-08-07 12:28:00', '2013-08-07 12:31:00']

I want each unique element of state_seq to be represented with a colour and plot the sequence. As of now, I am using seaborn palplot to get this done in a trivial way as,

color_set = ["#95a5a6", "#34495e","#9b59b6", "#3498db", "#800000", "#2ecc71", 'y', '#FF4500']
palette = list()
for s in state_seq:
  palette.append(color_set[s])    

sns.palplot(palette)

Which gives me something like this (this output may not exactly match with the code snippet - I edited the code for better clarity) Sequence from seaborn pal plot

This approach has a setback that I can not represent my time labels in x-axis. Is there a better python alternative perhaps similar to the R-package TraMineR explained here Is it possible to make a graph with pattern fills using TraMineR and R base graphs?

Community
  • 1
  • 1
Unni
  • 5,348
  • 6
  • 36
  • 55
  • Why not use the *barchart* of `matplotlib` with equally high bars (w/o borders) and removed y-axis? – jbndlr Aug 05 '16 at 10:43

1 Answers1

2

I assume you want square boxes for each value, regardless of the time gap between each. To have a width indicative of the time gap you could convert time_seq to datetime objects and then plot the dates directly with matplotlib.

import matplotlib.pyplot as plt
import numpy as np

state_seq = [2, 2, 2, 2, 0, 0, 0, 1, 3, 6, 3, 3, 3, 0, 0]
time_seq = ['2013-05-29 09:31:00', '2013-05-29 09:46:00', '2013-07-23 12:28:00', '2013-07-23 01:53:00', '2013-08-05 02:02:00', '2013-08-05 02:08:00', '2013-08-05 04:28:00', '2013-08-06 10:20:00', '2013-08-06 03:03:00', '2013-08-06 04:13:00', '2013-08-06 04:17:00', '2013-08-06 04:36:00', '2013-08-07 11:07:00', '2013-08-07 12:28:00', '2013-08-07 12:31:00']
color_set = ["#95a5a6", "#34495e","#9b59b6", "#3498db", "#800000", "#2ecc71", 'y', '#FF4500']

for i in range(len(time_seq)):
    # fill the yaxis with our colour for each time_seq entry for width 1
    plt.fill_between((i,i+1), 1, color=color_set[state_seq[i]])

# keep the coloured boxes square
plt.axis("scaled")

plt.xlim(0, len(time_seq))
plt.axes().get_yaxis().set_visible(False)

# add custom tick labels based on time_seq and set to middle of boxes
plt.xticks(np.arange(0.5,len(time_seq)+0.5, 1),  time_seq, rotation='vertical')
# remove xaxis ticks
plt.axes().get_xaxis().set_ticks_position("none")

plt.savefig("boxes.png", bbox_inches="tight")

Produces: enter image description here

Community
  • 1
  • 1
Jdog
  • 10,071
  • 4
  • 25
  • 42