Skip to content

Instantly share code, notes, and snippets.

@sukhbinder
Created January 25, 2022 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sukhbinder/dd09787803226ee98e68c9aab6a7ccec to your computer and use it in GitHub Desktop.
Save sukhbinder/dd09787803226ee98e68c9aab6a7ccec to your computer and use it in GitHub Desktop.
Comet Plot using matplotlib in python
import matplotlib.pyplot as plt
import numpy as np
def comet(x,y=None, time=0.05, fill=False):
"""
Displays a comet plot
by Sukhbinder
date: 15 Feb 2021
For the story read this https://sukhbinder.wordpress.com/2021/09/15/comet-plot-in-python/
"""
x = np.asarray(x)
plt.ion()
plt.xlim(x.min(), x.max())
plt.axis("off")
if y is not None:
y = np.asarray(y)
plt.ylim(y.min(), y.max())
else:
plt.ylim(0, len(x))
if y is not None:
plot = plt.plot(x[0], y[0])[0]
else:
plot = plt.plot(x[0])[0]
for i in range(len(x)+1):
if y is not None:
plot.set_data(x[0:i], y[0:i])
else:
plot.set_xdata(x[0:i])
plt.draw()
plt.pause(time)
if fill:
plt.fill_between(x,y, zorder=100)
plt.ioff()
@sukhbinder
Copy link
Author

For the story behind the plot read my blog post at https://sukhbinder.wordpress.com/2021/09/15/comet-plot-in-python/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment