'Timeseries graph changing color based on variable in python
I have a dataframe like so:
Restaurant TimeStamp count color
McDonalds 2020-03-31 14:55:37 13 orange
Wendys 2020-03-31 19:11:16 2 forestgreen
Wendys 2020-03-31 19:11:21 3 forestgreen
Wendys 2020-03-31 19:11:43 4 forestgreen
Wendys 2020-03-31 19:11:44 1 forestgreen
Wendys 2020-03-31 19:44:12 1 forestgreen
Wendys 2020-03-31 19:48:44 1 forestgreen
KFC 2020-03-31 21:09:26 4 lightblue
I want to make a time series plot which will plot a line graph where the y-axis is count and the x-axis will be the time stamp. I want the line to have a dot for each time-stamp and the line/dot be a different color everytime the restaurant changes. For example, the line and dots will be forestgreen from 2020-03-31 19:11:16 to 2020-03-31 21:09:26 (the time frame for which the timestamp is at Wendys).
How can I do this in seaborn or matplotlib?
The output would look something like this:
Solution 1:[1]
I think this is a solution with matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = [["McDonalds", "2020-03-31 18:55:37", 9, "orange"],
["Wendys" , "2020-03-31 19:11:16", 2, "forestgreen"],
["Wendys" , "2020-03-31 19:11:21", 3, "forestgreen"],
["Wendys" , "2020-03-31 19:11:43", 4, "forestgreen"],
["Wendys" , "2020-03-31 19:11:44", 1, "forestgreen"],
["Wendys" , "2020-03-31 19:44:12", 1, "forestgreen"],
["Wendys" , "2020-03-31 19:48:44", 1, "forestgreen"],
["KFC" , "2020-03-31 20:09:26", 4, "lightblue"],
["KFC" , "2020-03-31 20:19:26", 3, "lightblue"]]
df = pd.DataFrame(data, columns = ['Restaurant', 'TimeStamp','count','color'])
df['TimeStamp'] = pd.to_datetime(df['TimeStamp'])
#plot dot for each time-stamp
plt.scatter(df['TimeStamp'], df['count'], c = df['color'])
#color change detection id
b = np.array(df[:-1]['color']) != np.array(df[1:]['color'])
id_change_color = np.array(b.nonzero())[0] + 1
id_change_color = np.concatenate((np.array([0]), id_change_color,np.array([len(df.index)])))
#plot every color section
for i in range(id_change_color.shape[0] - 1):
id_inf, id_sup = id_change_color[i:i+2]
plt.plot(df.iloc[id_inf:id_sup+1]['TimeStamp'],
df.iloc[id_inf:id_sup+1]['count'],
color = df.iloc[id_inf]['color'])
plt.ylabel('count')
plt.xlabel('TimeStamp')
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pierre Debaisieux |


