Catalog | MATRIX-RPLOT-0129 |
Version | 0003 |
Date | 08.23.10 |
URL | /solvers/rplotpage/matrix |
Source File | /solvers/plots/rplot/matrix.qpy |
Doc File | /solvers/plots/rplot/doc/rplot.tex |
XML-RPC | TBD |
Solvers | Python, R |
Author | Dr. Ernesto P. Adorio |
The matplot function of R allows the user to plot other vectors against a given vector.
Given an input matrix X, our matrix plot allows one to specify a "base" column for which the other
columns are plotted against it.
Various plot settings allows one to plot either points or lines for other columns.
The main input box expects a matrix with each row written on each line.
A maximum of seven columns is allowed by this matrix plot routine.
Here is a view of the matrix plot page menu screen:
And here is the resulting plot drawn by our online solver.
The actual R code to generate the graph is
png('tmpksstQF.png', width=6*72, height=6*72)
> X <- c(21.69935,-11.798573,104.36818,33.40331,-12.988921,105.79699,48.8454,-12.244453,111.39495,44.10297,-9.557104,136 15997,48.51514,-7.568557,150.82756,65.80403,-9.416977,147.99936,34.86882,-8.473734,93.32171,45.74425,-7.322524,83.93707, 1.8046,-4.894993,151.96477,54.41312,-5.934022,43.50714,53.41098,-10.627767,91.71084,58.93975,-11.212206,118.9405,35.4372 ,-8.715245,55.91096,56.1509,-13.563171,40.80423,81.91758,-11.491009,95.21836,42.168,-13.77088,100.34292,55.15041,-7.5223 7,68.27453,35.32567,-8.874406,129.32671,49.89936,-16.804003,112.49538,43.73633,-8.427571,47.67621,52.84546,-8.812953,88. 2893,55.83368,-10.618802,108.34818,37.9956,-11.34301,64.80144,47.22976,-14.296704,95.19047,62.69356,-11.104689,89.95547) > M <- matrix(X, ncol = 3, byrow=T) > M <- M[order(M[,1]),] > M
> matplot( M[,1], M[,-1],type=c('p','p','p','p','p','p'),lty=c(2,2,2,2,2,2),
+ lwd=c(1,1,1,1,1,1),pch=c(1,2,3,4,5,6),col=c('black','blue','red','green','yellow','violet'),
+ cex=c(1,1,1,1,1,1),ylab=c('y,z'),xlab=c('x'),main="Matrix plot",sub="matplot generator")
> graphics.off()
> #img tmpksstQF.png
The Qpy code is presented here so that improvements may be facilitated faster.
# matrix.qpy # 2006.09.02 0.0.1 first version # 2006.09.20 0.0.2 split from pie, dot page. # 2010.08.23 0.0.3 revised. __version__ = "0.0.3 2010.08.23" __author__ = "ernesto.adorio@gmail.com" __title__ = "Matrix PLots using R" __catalog__ = "MATPLOT-RPLOT-0129" __url__ = "/solvers/rplotpage/matrix/" import time import tempfile import commands import os from qp.fill.directory import Directory from qp.fill.form import Form, StringWidget, TextWidget,CheckboxWidget,SingleSelectWidget from qp.fill.css import BASIC_FORM_CSS from qp.sites.extreme.lib.tmpfilesmanager import TmpFilesManager from qp.sites.extreme.lib.uicommon import renderheader, renderfooter, processheader, processfooter from qp.pub.common import page from qp.sites.extreme.lib.checkinput import checkInputs, getFormStrings from qp.sites.extreme.lib.qpyutils import printRlines, showLogo from qp.sites.extreme.lib.webutils import vecRead, GraphicsFile, as_R_cvector, as_R_vector, as_R_matrix from qp.sites.extreme.lib import config def getcarg(s, defaultval): s = s.strip() if s in ["", "None"]: return defaultval return as_R_cvector(s.split()) def getdarg(s, defaultval): s = s.strip() if s in ["", "None"]: return defaultval return as_R_vector(s.split()) def Solve(fields): # Get the fields. (gX, gY, matdata, colnames, against, sortq, plottype, lty, lwd, pch, cex, xlab, ylab, col,main, sub) = fields if colnames == "": return "ERROR: blank column names field" colnames = colnames.split() ncol = len(colnames) if ncol > 7: return "ERROR: more than 7 columns specified." against = against.strip() if against not in ["None",""]: try: apos = colnames.index(against) + 1 except: raise ValueError, "Variable to plot against [%s]is not in column names." % against else: apos = 0 matdata=matdata.strip() if matdata in ["", "None"]: raise ValueError, "ERROR: empty matrix data field." X = vecRead(matdata) extraargs = "" extraargs += ',type=%s' % getcarg(plottype, "p") extraargs += ',lty=%s' % getdarg(lty, 1) extraargs += ',lwd=%s' % getdarg(lwd, 1) extraargs += ',pch=%s' % getdarg(pch, "c(1,2,3,4,5,6,7)") extraargs += ',col=%s' % getcarg(col, "black") extraargs += ',cex=%s' % getdarg(cex, 1) extraargs += ',ylab=%s' % getcarg(ylab, '""') extraargs += ',xlab=%s' % getcarg(xlab, '""') extraargs += ',main="%s"' % main extraargs += ',sub="%s"' % sub # Start of R code. fname1 = GraphicsFile(str("png")) barefile1 = fname1.split(str("/"))[-1] Rcode = """png('%s', width=6*72, height=6*72)\n""" % fname1 Rcode += "X <- " + as_R_vector(X) + "\n" Rcode += """M <- matrix(X, ncol = %s, byrow=T)\n""" % ncol if apos != 0: if sortq: Rcode += "M <- M[order(M[,%s]),]\n" % apos Rcode += "M\n" Rcode += "matplot( M[,%s], M[,-%s] %s)" %(apos,apos, extraargs) else: Rcode += "matplot(M %s)" % extraargs Rcode += """ graphics.off() #img %s """ % (barefile1,) # Write to temporary file. (f, name) = tempfile.mkstemp(suffix=str(".r"), prefix=str("tmp"), dir=config.tmp_dir) os.write(f, str(Rcode)) (status, output) = commands.getstatusoutput(str("R -q --no-save < %s") % name) os.close(f) return output class MatrixplotPage(Directory): def get_exports(self): yield ('', 'index', 'MatrixPlot', '') def index[html](self): form = Form(enctype="multipart/form-data") # enctype for file upload form.add(StringWidget, name="gX", title="gX", value = "6", size=3) form.add(StringWidget, name="gY", title="gY", value = "6", size=3) sample = """ 21.69935 -11.798573 104.36818 33.40331 -12.988921 105.79699 48.84540 -12.244453 111.39495 44.10297 -9.557104 136.15997 48.51514 -7.568557 150.82756 65.80403 -9.416977 147.99936 34.86882 -8.473734 93.32171 45.74425 -7.322524 83.93707 71.80460 -4.894993 151.96477 54.41312 -5.934022 43.50714 53.41098 -10.627767 91.71084 58.93975 -11.212206 118.94050 35.43727 -8.715245 55.91096 56.15090 -13.563171 40.80423 81.91758 -11.491009 95.21836 42.16800 -13.770880 100.34292 55.15041 -7.522367 68.27453 35.32567 -8.874406 129.32671 49.89936 -16.804003 112.49538 43.73633 -8.427571 47.67621 52.84546 -8.812953 88.02893 55.83368 -10.618802 108.34818 37.99560 -11.343010 64.80144 47.22976 -14.296704 95.19047 62.69356 -11.104689 89.95547 """ form.add(TextWidget, name = "matdata", title="", \ value = sample, cols ="65", rows = "10") form.add(StringWidget, name = "colnames", title="Column names (not blank!)", value = "x y z", size = "35") form.add(StringWidget, name = "against", title="Against", value = "x", size = "10") form.add(CheckboxWidget, name = "sortq", title="Sort?") form.add(StringWidget, name = "plottype", size = 35, value = "p p p p p p") form.add(StringWidget, name = "lty", size = 35, value = "2 2 2 2 2 2") form.add(StringWidget, name = "lwd", size = 35, value = "1 1 1 1 1 1") form.add(StringWidget, name = "pch", size = 35, value = "1 2 3 4 5 6") form.add(StringWidget, name = "cex", size = 35, value = "1 1 1 1 1 1") form.add(StringWidget, name = "xlab", size = 35, value = "x") form.add(StringWidget, name = "ylab", size = 35, value = "y,z") form.add(StringWidget, name = "main", size = 35, value = "Matrix plot") form.add(StringWidget, name = "sub", size = 35, value = "matplot generator") form.add(StringWidget, name = "col", size = 35, value = "black blue red green yellow violet") form.add_hidden("time", value = time.time()) form.add_submit("submit", "submit") def render [html] (): renderheader(__title__) """
%s | %s | %s | %s | %s |
Plot type, type [p l b c o h s S n] | %s |
Line type, lty [1 2 3 4 5] | %s |
Line width, lwd | %s |
Point char, pch [any character]] | %s |
Char expand, cex | %s |
X label, xlab | %s |
Y label, ylab | %s |
Colors, col | %s |
Main title, main | %s |
Subtitle, sub | %s |
When statistics, pairs plots and box blots are required, use the solver Cat STAT-MSS-0064 instead. """ renderfooter(form, __version__, __catalog__, __author__) if not form.is_submitted(): return page('matrixplotpage', render(), style= BASIC_FORM_CSS) def process [html] (): processheader(__title__) calctime_start = time.time() # Get the problem parameters (gX, gY, matdata, colnames, against, sortq, plottype, lty, lwd, pch, cex, xlab, ylab, col, main, sub) = getFormStrings(form, [ "gX", "gY", "matdata", "colnames", "against", "sortq","plottype", "lty", "lwd", "pch", "cex", "xlab", "ylab", "col", "main", "sub" ]) inflag = checkInputs( [("gX", gX, ("float", 3, 10)), ("gY", gY, ("float", 3, 10)), ]) if inflag[0]: "
" inflag[1] "" else: output = Solve((gX, gY, matdata, colnames, against, sortq, plottype, lty, lwd, pch, cex, xlab, ylab, col, main, sub)) "
" printRlines(output) "" showLogo("Rlogo.jpg") processfooter(form, calctime_start, "./", __url__) process()
Constructive comments from our blog readers are always welcome.
No comments:
Post a Comment