Skip to content

Instantly share code, notes, and snippets.

@ashishraste
Last active June 24, 2019 05:07
Show Gist options
  • Select an option

  • Save ashishraste/ff97e7e3398e3d90eeff122d8032d78d to your computer and use it in GitHub Desktop.

Select an option

Save ashishraste/ff97e7e3398e3d90eeff122d8032d78d to your computer and use it in GitHub Desktop.
Python Snippets

Python Snippets

Matplotlib Visualisation

Multicolored lines based on categorical values

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm
import pandas as pd

num_classes = 4
ts = range(10)
df = pd.DataFrame(data={'TOTAL': np.random.rand(len(ts)), 'Label': np.random.randint(0, num_classes, len(ts))}, index=ts)
print(df)

cmap = ListedColormap(['r', 'g', 'b', 'y'])
norm = BoundaryNorm(range(num_classes+1), cmap.N)
points = np.array([df.index, df['TOTAL']]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(df['Label'])

fig1 = plt.figure()
plt.gca().add_collection(lc)
plt.xlim(df.index.min(), df.index.max())
plt.ylim(-1.1, 1.1)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment