Last active
October 4, 2015 14:27
-
-
Save xuhdev/2652756 to your computer and use it in GitHub Desktop.
Use matplotlib to plot in a column from several ascii data files
This file contains hidden or 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
#!/usr/bin/env python | |
# Requires numpy and matplotlib | |
# This file is in public domain | |
def usage(): | |
import sys | |
print >>sys.stderr, sys.argv[0] + " [options] file1 [file2 file3 ...]" | |
print >>sys.stderr, """ | |
Options: | |
-x OR --xlabel Spedify the x label | |
--xmin The x minimum limit | |
--xmax The x maximum limit | |
--ymin The y minimum limit | |
--ymax The y maximum limit | |
--face-color The filling color list separated by ':'. See | |
http://matplotlib.sourceforge.net/api/colors_api.html#module-matplotlib.colors | |
for the color list. | |
-y OR --ylabel Specify a y label. If you have multiple plots, each -y or --ylabel will specify one. | |
--no-y-ticks Don't display any ticks on y axis. | |
""" | |
def main(): | |
import sys | |
import numpy | |
import getopt | |
import matplotlib.pyplot as plt | |
if len(sys.argv) <= 1: | |
usage() | |
sys.exit(2) | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], 'x:y:', | |
['xlabel=', 'xmin=', 'xmax=', 'ymin=', 'ymax=', 'face-color=', 'ylabel=', 'no-y-ticks']) | |
except getopt.GetoptError as err: | |
print >>sys.stderr, str(err) | |
print >>sys.stderr, '' | |
usage() | |
sys.exit(1) | |
xmin = None | |
xmax = None | |
ymin = None | |
ymax = None | |
no_y_ticks = False | |
xlabel = '' # default xlabel is empty | |
face_color_list = [] | |
ylabel_list = [] | |
for o, a in opts: | |
if o in ("-x", "--xlabel"): | |
xlabel = a | |
elif o in ("-y", "--ylabel"): | |
ylabel_list.append(a) | |
elif o == "--xmin": | |
xmin = float(a) | |
elif o == "--xmax": | |
xmax = float(a) | |
elif o == "--ymin": | |
ymin = float(a) | |
elif o == "--ymax": | |
ymax = float(a) | |
elif o == "--face-color": | |
face_color_list = a.split(':') | |
elif o == "--no-y-ticks": | |
no_y_ticks = True | |
else: | |
assert False, "unhandled option" | |
data = [] | |
for filename in args: | |
data.append(numpy.loadtxt(filename, usecols = (0, 1))) | |
# the number of plots | |
plot_count = len(data) | |
if plot_count <= 0: | |
usage() | |
sys.exit(3) | |
# the color is blue by default | |
if len(face_color_list) > plot_count: | |
del face_color_list[plot_count:] | |
elif len(face_color_list) < plot_count: | |
face_color_list.extend( [ 'b' for i in range(len(face_color_list), plot_count + 1) ] ) | |
# ylabel is empty by default | |
if len(ylabel_list) > plot_count: | |
del ylabel_list[plot_count:] | |
elif len(ylabel_list) < plot_count: | |
ylabel_list.extend( [ '' for i in range(len(ylabel_list), plot_count + 1) ] ) | |
f, axes = plt.subplots(plot_count, sharex = True, sharey = True) | |
f.subplots_adjust(hspace = 0) | |
for one_data, ax, facecolor, ylabel in zip(data, axes, face_color_list, ylabel_list): | |
ax.fill_between(one_data[:, 0], one_data[:, 1], facecolor = facecolor) | |
plt.xlim(xmin, xmax) | |
plt.ylim(ymin, ymax) | |
if no_y_ticks: | |
plt.yticks([]) | |
ax.set_ylabel(ylabel) | |
plt.xlabel(xlabel) | |
plt.show() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment