Sunday, July 19, 2015

Python: plots (1)

Abstract: graph scatterplot using matplotlib





The script:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 19 09:49:42 2015

@author: yuan
"""
import numpy as np
from numpy.random import randn
import matplotlib.pyplot as plt

#function of scatter plot
def scatterplot(x, y, xlab='x', ylab='y', xlim=(), ylim=(), col='black',
lty='-', main='plot'):
if len(xlim)==0:
xlim=(min(x),max(x))
if len(ylim)==0:
ylim=(min(y),max(y))

#plot
plt.plot(x,y, color=col, linestyle=lty)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.xlim(xlim)
plt.ylim(ylim)
plt.suptitle(main, fontsize=20)
plt.show()


#plot1
x=np.arange(10)
y=np.arange(10,20)
scatterplot(x,y, xlim=(2,8), ylim=(4,19), xlab='x axis', ylab='y axis',
col='red', lty='-', main='a sold red line')


#plot2
r=randn(100).cumsum()
print r, range(len(r))
scatterplot(range(len(r)),r, lty='--', main='dash line')



No comments:

Post a Comment