Skip to content

Instantly share code, notes, and snippets.

@bkamapantula
Created August 2, 2014 18:19
Show Gist options
  • Save bkamapantula/b5d036e960a673197379 to your computer and use it in GitHub Desktop.
Save bkamapantula/b5d036e960a673197379 to your computer and use it in GitHub Desktop.
matplotlib plot - two data series
"""
Input
======
input text file considered here contains 3 columns.
"""
import matplotlib as mpl
import matplotlib.pyplot as plt
import sys
import os
in_file = open(sys.argv[1], "r")
x_data = [row.split("\t")[0] for row in in_file] # this is essentially column 0 data
in_file.seek(0)
col_1_data = [row.split("\t")[1] for row in in_file]
in_file.seek(0)
col_2_data = [row.split("\t")[2] for row in in_file]
in_file.close()
plt.plot(x_data, col_1_data, label="Column 1 values", marker='o')
plt.plot(x_data, col_2_data, label="Column 1 values", marker='x')
plt.xlabel("X-axis label (Units)")
plt.ylabel("Y-axis label (Units)")
# adding extra_ticks is optional. useful if you need breathing space for the first and final data points
# 'pre' and 'post' are numbers where 'pre' value precedes the least value and 'post' value succeeds the max value
extra_ticks = ['pre', 'post']
# plt.xticks() gets tick locations and values
plt.xticks( list(plt.xticks()[0]) + extra_ticks)
"""
legend location can be changed using loc parameter.
bbox_to_anchor is used to change legend position with respect to the legend location. additionally, height and width parameters can be passed
size in prop parameter changes font size within legend
refer to http://matplotlib.org/users/legend_guide.html?highlight=legend%20location#legend-location for more info.
"""
plt.legend(bbox_to_anchor=(1, 0.7), loc=6, prop={'size':12})
# save the image with the same name as the input file with data
image_name = os.path.splitext(sys.argv[1])[0]
# mentioning orientation, format and dpi are optional
plt.savefig(image_name, orientation='landscape', format='pdf', dpi=(200))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment