Skip to content

Instantly share code, notes, and snippets.

@fishnsotong
Last active January 6, 2017 01:11
Show Gist options
  • Save fishnsotong/a2e88486afbec4f9234ef8c5bab630f5 to your computer and use it in GitHub Desktop.
Save fishnsotong/a2e88486afbec4f9234ef8c5bab630f5 to your computer and use it in GitHub Desktop.
Making a line plot in python by importing data from a csv file.
import numpy as np
import matplotlib.pyplot as plt
# importing data from csv file
xs, ys, zs = np.loadtxt("data.txt", unpack=True, delimiter=",", dtype=float)
# plotting
plt.plot(xs, ys, "b-", label="line one") # blue line, label displayed in legend
plt.plot(xs, zs, "r-", label="line two") # red line, for scatter plot replace with "ro"
# labels
plt.ylim(1, 100) # sets range for y axis
plt.xlim(1, 100) # sets range for x axis
plt.legend(loc='best', prop={'size': 10}) # size and location of plot legend
plt.xlabel("x axis label")
plt.ylabel("y axis label")
plt.title("title of figure")
plt.yticks([48, 56, 64, 72, 80, 88, 96]) # use this array to demarcate ticks on y axis
# save plot to file in current working directory
plt.savefig("fig.png")
# display plot on screen
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment