def plotNumLegend(self, colVecL, breakL, nb_breaks, filename=None, legendDir=None, type='png', int_labels=False): if filename is None: filename = 'legend_%i_%i_%i' % (len(colVecL), min(breakL), max(breakL)) if legendDir is None: legendDir = self.legendDir full_filename = os.path.join(legendDir, filename) max_break = max(breakL) min_break = min(breakL) tickBreakL = [float(x) / (nb_breaks - 1) * (max_break - min_break) - min_break for x in range(nb_breaks)] if int_labels: labels = ['%i' % int(x) for x in tickBreakL] else: labels = ['%.3f' % x for x in tickBreakL] rdev = plot_utilities.RDevice(name = full_filename, title='', plotType=type, width=640, height=120) r("par(mar=c(3,1,0,1), las=2)") #legendA = rpy.reshape(breakL, (len(breakL), 1)) legendA = r.matrix(breakL, byrow=False, ncol=1) r.image(legendA, 1, legendA, col=colVecL, axes=False, ann=False) r.box() r.axis(1, at=tickBreakL, labels=labels, tick=True, line=0, cex=0.8, cex_axis=0.8) rdev.close() return
def drawCpuUsage(d,xat,xlbs,pic): data=r.c(d) pch = 22 # point like like a square lty = 1 # line style solid line col = "blue" # line color ltype = "o" # line only http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/plot.html ylim = (0,100) # y domain xaxis=1 yaxis=2 vertical_text=2 horizontal_text=1 text_size=0.8 yat=[x for x in range(0,110,10)] ylbs=["%d%%"%x for x in yat] legend_x=1 legend_y=100 # for r.text method warn_val=40 # val over this value will display a text on point xtext=[idx for idx,val in enumerate(d) if val > warn_val] # text x pos ytext=[val for idx,val in enumerate(d) if val > warn_val] # text y pos labtext=["%d%%"%val for idx,val in enumerate(d) if val > warn_val] # label for text # summary dmax=max(d) sumry="max %.2f%%" % dmax sumrycol="red" if dmax>50 else "green" # plot r.png(pic, width=900,height=450*0.6) r.plot(data, type=ltype, col=col, ylim=ylim, pch=pch, lty=lty, axes=False, ann=False) # draw text that over 40 # http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/text.html r.text(xtext, ytext, labels=labtext, pos=3, cex=0.8, col="red") # summary text r.mtext(sumry, side=3, cex=1, col=sumrycol) # axis r.axis(xaxis, las=vertical_text, at=xat, lab=r.c(xlbs)) r.axis(yaxis, las=horizontal_text, at=yat, lab=r.c(ylbs)) r.box() # titles r.title(main="CPU Sampling") r.title(xlab="Time") r.title(ylab="CPU Usage") # reference line # r.abline(h=50, col="gray") # at 50% # legend r.legend(legend_x, legend_y, r.c(("trunk")), col=col, cex=text_size, pch=pch, lty=lty) r.dev_off()
def test(): data=r.c([1.25,3.45,6.75,20.2,9.9]) # draw image using rpy r.png("test.png", width=300,height=300) r.plot(data, type="o", col="blue", ylim=(0,100), pch=22, lty=1, axes=False, ann=False) r.axis(1, at=(1,2,3,4,5), lab=r.c("a","b","c","d","e")) r.axis(2, las=1, at=(0,50,100)) r.box() r.title(main="CPU usage sampling result") r.title(xlab="Time") r.title(ylab="CPU") r.legend(1,100,r.c("trunk"), cex=0.8, col=r.c("blue"), pch=22, lty=1) r.dev_off()
def rplot(data, par={}, title={}, xlab="", ylab=""): r.plot_new() xmax = -1e6 xmin = 1e6 ymax = -1e6 ymin = 1e6 for x in data: #print x[0] #print x[1] xmax = max(xmax, max(x[0])) xmin = min(xmin, min(x[0])) ymax = max(ymax, max(x[1])) ymin = min(ymin, min(x[1])) #print xmin, xmax, ymin, ymax r.plot_window(xlim=(xmin, xmax), ylim=(ymin, ymax)) r.title( main=title.get("main", ""), xlab=title.get("xlab", ""), ylab=title.get("ylab", "")) r.box(col="black") ; r.axis(1) ; r.axis(2) for i, x in enumerate(data): r.lines(x[0], x[1], col=i+1, **par)
def plotArray(self, hitDataParam, filename, plotDir=None, labeledPosD=None, title='', type='png', label_colors = True, colVecL=None, legend_plot = False, legend_filename=None, control_color='black', numeric_as_int = False, grid=True, max_val=0.3, min_val=0.0, cut_data = True): if plotDir is None: plotDir = self.plotDir full_filename = os.path.join(plotDir, filename) #hitData = copy.deepcopy(hitDataParam) hitData = hitDataParam #nrow = len(hitData) #ncol = len(hitData[0]) nrow = self.nb_row ncol = self.nb_col xL = range(1, self.nb_col + 1) yL = range(1, self.nb_row + 1) rdev = plot_utilities.RDevice(name = full_filename, title=title, plotType=type, width=640, height=250) if label_colors: # in this case, the data is considered to consist of labels labelL = [] for i in range(nrow): for j in range(ncol): if hitData[i][j] not in labelL: labelL.append(hitData[i][j]) if colVecL is None: colVecL = r.rainbow(len(labelL)) colBreaksL = range(len(colVecL) + 1) if legend_plot: self.plotLabelLegend(colVecL, plotType=type, filename=legend_filename) else: # in this case, the data is numeric. if cut_data: max_rel_cell_count = max_val min_rel_cell_count = 0.0 for i in range(nrow): for j in range(ncol): hitData[i][j] = max(min_rel_cell_count, min(max_rel_cell_count, hitData[i][j])) else: max_rel_cell_count = max([max(x) for x in hitData.tolist() ]) min_rel_cell_count = min([min(x) for x in hitData.tolist() ]) if numeric_as_int: nb_colors = max_rel_cell_count else: nb_colors = 500 if colVecL is None: pattern = [(0,0,0),(0.7,0,0),(1,1,0),(1,1,1)] colVecL = colors.make_colors(pattern, nb_colors) colBreaksL = [1.0/ (len(colVecL) - 1) * x * (max_rel_cell_count - min_rel_cell_count) + min_rel_cell_count for x in range(len(colVecL) + 1)] if legend_plot: self.plotNumLegend(colVecL, colBreaksL, 16, filename=legend_filename, type=type, int_labels=numeric_as_int, legendDir = plotDir) axisSize = .8 r("par(mar=c(1.6,1.6,0.1,0.1))") r.image(xL, yL, r.t(hitData), axes = False, ann=False, cex=1, col=colVecL, breaks=colBreaksL) r.box() if not labeledPosD is None: for label in labeledPosD.keys(): posL = labeledPosD[label] if len(posL) > 0: xlL = [(int(x)-1) % self.nb_col + 1 for x in posL] ylL = [(int(x)-1) / self.nb_col + 1 for x in posL] r.points(xlL, ylL, pch=label, col=control_color, cex=axisSize) print print xlL print ylL print # grid if grid: for i in range(self.nb_col): r.abline(h=i+.5, lty=3, lwd=1, col='grey') for i in range(self.nb_row): r.abline(v=i+.5, lty=3, lwd=1, col='grey') r.axis(1, at=xL, labels=[str(x) for x in xL], tick=False, line=-1.0, cex_axis=axisSize) r.axis(2, at=yL, labels=[str(y) for y in yL], tick=False, line=-1.0, cex_axis=axisSize) rdev.close() return
def myhist(lnw, color="black", makenewplot=True, main=None, bins=None, support=None, xlab=None, ylab="", sub=None, xmin=None, xmax=None, bin_numbers=True): nbins = len(lnw) if bins!=None: assert(nbins==len(bins)-1) xss = [[]] yss = [[]] xlim = None connect_to_prev = False if bins==None: for bin in range(nbins): if (lnw[bin] > -inf or lnw[bin] < inf) and (support==None or support[bin]) and (xmin==None or xmin<=bin) and (xmax==None or (bin+1)<=xmax): xss[-1] += [bin, bin+1] yss[-1] += [lnw[bin], lnw[bin]] elif xss[-1]!=[]: xss.append([]) yss.append([]) if xmin==None and xmax==None: xlim = (0, nbins+1) else: xs = reduce(operator.add, xss, []) xlim = (min(xs), max(xs)) else: for bin in range(nbins): if (lnw[bin] > -inf or lnw[bin] < inf) and (support==None or support[bin]) and (xmin==None or xmin<=bins[bin]) and (xmax==None or bins[bin+1]<=xmax): xss[-1] += [bins[bin], bins[bin+1]] yss[-1] += [lnw[bin], lnw[bin]] elif xss[-1]!=[]: xss.append([]) yss.append([]) if xmin==None and xmax==None: xlim = (bins[0], bins[-1]) else: xs = reduce(operator.add, xss, []) xlim = (min(xs), max(xs)) # Do the plotting if makenewplot: r.plot_new() ys = reduce(operator.add, yss, []) ymin = min(ys) ymax = max(ys) r.plot_window(xlim=xlim, ylim=(ymin, ymax)) if bins!=None and bin_numbers: labels = array(r.pretty(range(nbins), 8), dtype=int) labels = labels[(0<=labels) & (labels<nbins)] ats = bins[labels] r.axis(1) r.axis(1, tick=False, labels=labels, at=ats, mgp=(3,2,0)) r.axis(2) this_xlab = "E, bin" else: r.axis(1) r.axis(2) this_xlab = "E" r.axis(1) r.axis(2) r.box() r.title(ylab=ylab) if xlab==None: r.title(xlab=this_xlab) else: r.title(xlab=xlab) if main!=None: r.title(main=main) if sub!=None: r.title(sub=sub) for (xs, ys) in zip(xss, yss): r.lines(xs, ys, col=color, lwd=0.5) return (xss, yss)