Created
January 25, 2022 05:04
-
-
Save sukhbinder/dd09787803226ee98e68c9aab6a7ccec to your computer and use it in GitHub Desktop.
Comet Plot using matplotlib in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the story behind the plot read my blog post at https://sukhbinder.wordpress.com/2021/09/15/comet-plot-in-python/