Skip to content

Instantly share code, notes, and snippets.

@Bktero
Created February 22, 2019 08:27
Show Gist options
  • Save Bktero/92de1c44e7c94fc01e905e6cc654eeff to your computer and use it in GitHub Desktop.
Save Bktero/92de1c44e7c94fc01e905e6cc654eeff to your computer and use it in GitHub Desktop.
[Python] Use Numpy and Matplotlib to save data to a file and load these data to plot them
import os.path
import matplotlib.pyplot as plt
import numpy as np
FILENAME = 'data.txt'
SEPARATOR = '\n'
# Data can be generated with any tool as long as the format is good
# Create one to show the format of the file
if not os.path.isfile(FILENAME):
generated = np.random.random_sample(100)
generated.tofile(FILENAME, sep=SEPARATOR)
# Load the file as a numpy array
loaded = np.fromfile(FILENAME, sep=SEPARATOR)
# Plot this array
plt.plot(loaded)
print(loaded)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment