def linear_fit(x, y): """ linear fitting function with -99999 error removal Input: x --- independent variable array y --- dependent variable array Output: intc --- intercept slope--- slope """ # #--- first remove error entries (which is -99999) # xn = [] yn = [] for i in range(0, len(x)): if float(y[i]) != -99999: xn.append(float(x[i])) yn.append(float(y[i])) if len(xn) > 3: try: (a, b, berr) = robust.robust_fit(xn, yn) return [a, b] except: return [0, 0] else: return [0, 0]
def fit_line(x, y, xmin, xmax): """ fit robust fit line on the data on log-log plane input: x --- x data y --- y data xmin --- min of x xmax --- max of x """ # #--- convert the data into log # xl = numpy.log10(x) yl = numpy.log10(y) # #--- fit a line on log-log plane with robust fit # if len(xl) > 4: [a, b, e] = robust.robust_fit(xl, yl) else: a = 0.0 b = 0.0 e = 0.0 # #--- compute plotting data points on non-log plane; it is used by the plotting routine # xsave = [] ysave = [] step = (xmax - xmin) / 100.0 for k in range(0, 100): xe = xmin + step * k ye = 10**(a + b * math.log10(xe)) xsave.append(xe) ysave.append(ye) return [xsave, ysave, a, b]
def create_trend_data_plot(x, y, ccd, dtype, col_name, col_dname): """ create a trend plot input: x --- a list of x data y --- a list of y data ccd --- ccd name dytpe --- data type col_name --- short-hand column name col_dname --- full column name output: <plot_dir>/<ccd>_<dtype>_<col_name>.png """ # #--- fit a trend line # [intc, slope, err] = robust.robust_fit(x, y) # #--- set plotting range # [xmin, xmax, ymin, ymax] = set_plotting_range(x, y) plt.close('all') ax = plt.subplot(111) ax.set_autoscale_on(False) ax.set_xbound(xmin, xmax) ax.set_xlim(left=xmin, right=xmax, auto=False) ax.set_ylim(bottom=ymin, top=ymax, auto=False) # #--- plot data # plt.plot(x, y, color='blue', marker='.', markersize='1', lw=0) # #--- plot trending line # yb = intc + slope * xmin ye = intc + slope * xmax plt.plot([xmin, xmax], [yb, ye], color='red', marker='.', markersize='1', lw=2) # #--- label axes # plt.xlabel('Time (year)') plt.ylabel(col_dname) # #--- save the plot # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10, 5) outname = plot_dir + ccd + '_' + dtype + '_' + col_name + '.png' plt.savefig(outname, format='png', dpi=200)
def plot_s2hvst_vt_ratio(sfile, indata): """ create s2hvst - valid rate/total rate plot input: sfile --- input file name indata --- a list of lists of data output: <head_name>_s2hvst_vt_ratio.png """ # #--- create ratio data from vald and total count rate # [rdata, rerr] = compute_ratio(indata[12], indata[9], indata[14], indata[11]) # #--- setting plotting surface # mpl.rcParams['font.size'] = 14 props = font_manager.FontProperties(size=12) xmin = 4 xmax = 9 psize = 9 fsize = 14 tfsize = fsize + 2 ymin = 0.20 ymax = 0.50 a1 = plt.subplot(111) plt.axis([xmin, xmax, ymin, ymax]) plt.plot(indata[21], rdata, lw=0, marker='+', markersize=psize) # #--- add fitting line # (sint, slope, serror) = robust.robust_fit(indata[21], rdata, iter=100) start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax], [start, stop], lw=2.0, marker='+', markersize=psize) xdiff = xmax - xmin ydiff = ymax - ymin xpos = xmin + 0.1 * xdiff ypos = ymax - 0.1 * ydiff text = 'Slope: ' + str(round(slope, 4)) + '+/-' + str(round(serror, 4)) plt.text(xpos, ypos, text, size=tfsize, weight='bold') # #--- add the legend and x axis name # a1.set_xlabel("S2HVST", size=fsize) a1.set_ylabel("Valid Rate / Total Rate", size=fsize) # #--- save the plot in file # save_plot_in_file(sfile, 's2hvst_vt_ratio.png')
def get_int_slope(x, y, xl, yl): """ estimate a + b * x line with a robust method and then compare with a simple two point estimate and choose a shallower slope fitting input: x --- a list of x values y --- a list of y values xl --- a secondary list of x value yl --- a seocndary list of y value output: a --- intercept b --- slope """ # #--- choose the data only in the last one year # bound = ecf.current_time() - 1.0 xsave = [] ysave = [] for k in range(0, len(x)): if x[k] < bound: continue xsave.append(x[k]) ysave.append(y[k]) # #--- if there are not enough data, give up the fitting # if len(xsave) < 6: return [999, 999] try: # #--- robust fit # [a, b, err] = rfit.robust_fit(xsave, ysave) # #--- two point fit # [al, bl] = get_int_slope_supl(xl, yl) # #--- choose a shallower slope fit # if abs(bl) < abs(b): a = al b = bl except: a = 999 b = 999 return [a, b]
def get_int_slope(x, y, xl, yl): """ estimate a + b * x line with a robust method and then compare with a simple two point estimate and choose a shallower slope fitting input: x --- a list of x values y --- a list of y values xl --- a secondary list of x value yl --- a seocndary list of y value output: a --- intercept b --- slope """ # #--- choose the data only in the last one year # bound = ecf.current_time() - 1.0 xsave = [] ysave = [] for k in range(0, len(x)): if x[k] < bound: continue xsave.append(x[k]) ysave.append(y[k]) # #--- if there are not enough data, give up the fitting # if len(xsave) < 6: return [999, 999] try: # #--- robust fit # [a,b,err] = rfit.robust_fit(xsave, ysave) # #--- two point fit # [al, bl] = get_int_slope_supl(xl, yl) # #--- choose a shallower slope fit # if abs(bl) < abs(b): a = al b = bl except: a = 999 b = 999 return [a,b]
def linear_fit(x, y): """ linear fitting function with -99999 error removal Input: x --- independent variable array y --- dependent variable array Output: intc --- intercept slope--- slope """ # #--- first remove error entries (which is -99999) # xn = [] yn = [] for i in range(0, len(x)): if float(y[i]) != -99999: xn.append(float(x[i])) yn.append(float(y[i])) if len(xn) > 3: try: (a, b, berr) = robust.robust_fit(xn, yn) return [a, b] except: return [0,0] # #--- modify array to numpy array # # d = numpy.array(xn) # v = numpy.array(yn) # #--- kmpfit # # param = [0, 1] # fitobj = kmpfit.Fitter(residuals=residuals, data=(d,v)) # fitobj.fit(params0=paramsinitial) # return fitobj.params # # #--- chauvenet exclusion and linear fit # # try: # [a, b, ae, be] = chauv.run_chauvenet(d, v) # return [a, b] # except: # return [0, 0] else: return [0, 0]
def linear_fit(x, y): """ linear fitting function with -99999 error removal Input: x --- independent variable array y --- dependent variable array Output: intc --- intercept slope--- slope """ # #--- first remove error entries # sum = 0 sum2 = 0 tot = 0 for i in range(0, len(y)): if y[i] > 0: sum += y[i] sum2 += y[i] * y[i] tot += 1 if tot > 0: avg = sum / tot sig = math.sqrt(sum2 / tot - avg * avg) else: avg = 3.0 lower = 0.0 upper = avg + 2.0 xn = [] yn = [] for i in range(0, len(x)): if (y[i] > lower) and (y[i] < upper): xn.append(x[i]) yn.append(y[i]) if len(yn) > 10: [intc, slope, serr] = robust.robust_fit(xn, yn, iter=50) else: [intc, slope, serr] = [0, 0, 0] return [intc, slope, 0.0, serr]
def plot_single_panel(xmin, xmax, ymin, ymax, xdata, ydata, yerror, xname, yname, label, outname, resolution=100): """ plot a single data set on a single panel Input: xmin --- min x xmax --- max x ymin --- min y ymax --- max y xdata --- independent variable ydata --- dependent variable yerror --- error in y axis xname --- x axis label ynane --- y axis label label --- a text to indecate what is plotted outname --- the name of output file resolution-- the resolution of the plot in dpi Output: png plot named <outname> """ # #--- fit line --- use robust method # (sint, slope, serr) = robust.robust_fit(xdata, ydata) lslope = '%2.3f' % (round(slope, 3)) # #--- close everything opened before # plt.close('all') # #--- set font size # mpl.rcParams['font.size'] = 12 props = font_manager.FontProperties(size=9) # #--- set plotting range # ax = plt.subplot(111) ax.set_autoscale_on(False) ax.set_xbound(xmin, xmax) ax.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax.set_ylim(ymin=ymin, ymax=ymax, auto=False) # #--- plot data # plt.plot(xdata, ydata, color='blue', marker='o', markersize=4.0, lw=0) # #--- plot error bar # plt.errorbar(xdata, ydata, yerr=yerror, lw=0, elinewidth=1) # #--- plot fitted line # start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax], [start, stop], color='red', lw=2) # #--- label axes # plt.xlabel(xname) plt.ylabel(yname) # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.5 * xdiff ydiff = ymax - ymin ypos = ymax - 0.08 * ydiff label = label + ': Slope: ' + str(lslope) plt.text(xpos, ypos, label) # #--- set the size of the plotting area in inch (width: 10.0in, height 2.08in x number of panels) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 5.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plot_data(t_list, d_list, fid, xlabel, outname, xs=0): """ create plot input: t_list --- a list of x data d_list --- a list of y data fid --- fid light # xlabel --- the label for x axis outname --- plot file name xs --- indicator which way to set xaxis range output: outname --- png plot created """ [t_list, d_list] = remove_na_from_lists(t_list, d_list) tarray = numpy.array(t_list) darray = numpy.array(d_list) idx = ~numpy.isnan(darray) tarray = tarray[idx] darray = darray[idx] idx = (darray < 100) & (darray > -100) t_list = list(tarray[idx]) d_list = list(darray[idx]) # #--- set plotting range # if xs == 0: xmin = int(min(t_list)) - 1 xmax = int(max(t_list)) + 1 else: xmin = (int(10 * min(t_list)) - 0.5) / 10.0 xmax = (int(10 * max(t_list)) + 1.5) / 10.0 ymin = (int(10 * min(d_list)) - 0.5) / 10.0 ymax = (int(10 * max(d_list)) + 1.5) / 10.0 # #--- y label # ylabel = 'MAG_I_AVG_' + str(fid) # #--- start plotting # plt.close('all') props = font_manager.FontProperties(size=9) ax = plt.subplot(111) ax.set_autoscale_on(False) ax.set_xbound(xmin, xmax) ax.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax.set_ylim(ymin=ymin, ymax=ymax, auto=False) ax.set_facecolor("lime") # #--- plot data # plt.plot(t_list, d_list, marker='D', markersize=2, lw=0) # #--- compute fitting line and plot a fitted line # try: if len(t_list) > 3: out = rlf.robust_fit(t_list, d_list) pstart = out[0] + out[1] * xmin pstop = out[0] + out[1] * xmax plt.plot([xmin, xmax], [pstart, pstop], markersize=0, lw=1, linestyle='dashed', color='blue') except: pass plt.xlabel(xlabel) plt.ylabel(ylabel) # #--- set the size of the plot # fig = matplotlib.pyplot.gcf() fig.set_size_inches(7, 3.5) # #--- save the plot in png format # plt.savefig(outname, format='png', dip=300) plt.close('all')
def plot_multi_entries(xmin, xmax, ymin, ymax, xSets, ySets, xname, yname, entLabels, outname, yerror = 0,fsize = 9, psize = 2.0,lsize=0, resolution=100, linefit=0, connect=0): """ This function plots multiple data in a single panel. Input: xmin, xmax, ymin, ymax: plotting area xSets: a list of lists containing x-axis data ySets: a list of lists containing y-axis data xname: a name of x-axis yname: a name of y-axis entLabels: a list of the names of each data outname: a name of plotting file yerror: a list of lists of y error, if it is '0', no error bar, default = 0 fsize: font size, default = 9 psize: plotting point size, default = 2.0 lsize: fitted line width, default = 1 resolution: plotting resolution in dpi linefit --- if it is 1, fit a line estimated by robust method connect: connected line size. if it is '0', no connected line Output: a png plot: out.png """ colorList = ('lime', 'yellow', 'red', 'lime', 'green', 'blue', 'maroon', 'black', 'fushia', 'olive') markerList = ('o', '*', '+', '^', 's', 'D', '1', '2', '3', '4') plt.close('all') # #---- set a few parameters # mpl.rcParams['font.size'] = fsize props = font_manager.FontProperties(size=9) plt.subplots_adjust(hspace=0.08) # #---- set a panel # ax = plt.subplot(111) ax.set_autoscale_on(False) #---- these three may not be needed for the new pylab, but ax.set_xbound(xmin,xmax) #---- they are necessary for the older version to set ax.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax.set_ylim(ymin=ymin, ymax=ymax, auto=False) tot = len(entLabels) # #--- start plotting each data set # lnamList = [] for i in range(0, tot): xdata = xSets[i] ydata = ySets[i] color = colorList[i] marker = markerList[0] label = entLabels[i] if tot > 1: lnam = 'p' + str(i) lnamList.append(lnam) exec '%s, = plt.plot(xdata, ydata, color="%s", lw =lsize , marker="%s", markersize=3, label=entLabels[i])' %(lnam, color, marker) else: # #--- if there is only one data set, ignore legend # plt.plot(xdata, ydata, color=color, lw =connect , marker='o', markersize=psize) if yerror != 0: p, = plt.errorbar(xdata, ydata, yerr=yerror[i], lw = 0, elinewidth=1) if linefit > 0: (sint, slope,serror) = robust.robust_fit(xdata, ydata) start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax],[start,stop], color=color, lw =lsize ) # #--- add legend # if tot > 1: line = '[' for ent in lnamList: if line == '[': line = line + ent else: line = line +', ' + ent line = line + ']' exec "leg = legend(%s, entLabels, prop=props)" % (line) leg.get_frame().set_alpha(0.5) ax.set_xlabel(xname, size=fsize) ax.set_ylabel(yname, size=fsize) # #--- set the size of the plotting area in inch (width: 10.0in, height 5.0in) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 5.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plot_data(sdata, mdata, year, msid, oname, ymin, ymax, ydrange, msid_list): """ create two panel plots for tephin vs msid and its deviation input: sdata --- a list of tephin data mdata --- a list of msid data (mid/min/max) year --- year of the plot msid --- msid oname --- output name ymin --- y min of the first plot ymax --- y max of the first plot ydrange --- the range of the deviation y axis msid_list --- msid list output: oname in png format """ plt.close('all') fig, ax = plt.subplots(1, figsize=(8,6)) props = font_manager.FontProperties(size=14) mpl.rcParams['font.size'] = 14 mpl.rcParams['font.weight'] = 'bold' xmin = 1.5e4 xmax = 3.5e4 ydiff = ymax - ymin ypos = ymax - 0.1 * ydiff ax1 = plt.subplot(211) ax1.set_xlim(xmin, xmax) ax1.set_ylim(ymin, ymax) ax1.set_xlabel("Shevart (cnts)") ax1.set_ylabel(msid.upper()) # #--- set the size of plot # fig.set_size_inches(10.0, 5.0) fig.tight_layout() # #---- trending plots # points = ax1.scatter(sdata, mdata, marker='o', s=4 ,lw=0) # #---- envelope # period = 500 [xmc, ymc, xmb, ymb, xmt, ymt] = create_trend_envelope(sdata, mdata, period) # #--- trending area # try: ax1.fill_between(xmc, ymb, ymt, facecolor='#00FFFF', alpha=0.3, interpolate=True) except: pass # #--- center moving average # ax1.plot(xmc, ymc, color='#E9967A', lw=4) plt.text(16500, ypos, str(year)) # #---- derivative plot # [xd, yd, ad] = find_deriv(sdata, mdata, 'xxx', step=20) [dymin, dymax, dypos] = set_y_plot_range(yd) ymax = ydrange ymin = -1.0 * abs(ymax) ydiff = ymax - ymin ypos = ymax - 0.1 * ydiff ax2 = plt.subplot(212) ax2.set_xlim(xmin, xmax) ax2.set_ylim(ymin, ymax) ax2.set_xlabel("Shevart (cnts)") line = msid + '/ cnts' ax2.set_ylabel(line) points = ax2.scatter(xd, yd, marker='o', s=4 ,lw=0) try: [xx, yy] = remove_extreme_vals(xd, yd, p=96) [a, b, e] = rbl.robust_fit(xx, yy) except: [a, b, e] = least_sq(xd, yd, remove=1) ys = a + b * xmin ye = a + b * xmax ax2.plot([xmin, xmax], [ys, ye], color='green', lw=3) line = 'Slope: ' + "%3.3e" % (b) mpl.rcParams['font.size'] = 12 plt.title('dy / d(Cnts)', loc='left') plt.text(16500, ypos, line) # #--- set the size of plot # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 10.0) fig.tight_layout() plt.savefig(oname, format='png', dpi=100) plt.close('all')
def plot_panel2(data1, data2, data3, data4, ydpos, ylims, ylab, outname, lfit=0): """ plotting dtheta information input: data1 --- data for acis-i data is a list of list and the first is a list of time data2 --- data for acis-s data3 --- data for hrc-i data4 --- data for hrc-s ydpos --- the index of the data you want to use ylim --- a list of lists of y limits [(ymin ymax),...] ylab --- a list of labels for each data set to be plotted outname --- a name of output file lfit --- indicator of whether to fit line lfit=1: yes output: <web_dir>/Plots/<outname>.png """ plt.close('all') dlen = len(ylab) mlen = dlen - 1 # #--- set panel parameters # plt.subplots_adjust(hspace=0.08) props = font_manager.FontProperties(size=9) # #--- set xrange # atime = [] data = [] [xlist, xmin, xmax, xlabel, xtext] = set_x_range(data1[0]) atime.append(xlist) data.append(data1[ydpos]) [xlist, xmin, xmax, xlabel, xtext] = set_x_range(data2[0]) atime.append(xlist) data.append(data2[ydpos]) [xlist, xmin, xmax, xlabel, xtext] = set_x_range(data3[0]) atime.append(xlist) data.append(data3[ydpos]) [xlist, xmin, xmax, xlabel, xtext] = set_x_range(data4[0]) atime.append(xlist) data.append(data4[ydpos]) xrange = [xmin, xmax] # #--- plot each panel # for k in range(0, dlen): j = k + 1 line = str(dlen) + '1' + str(j) exec("ax%s = plt.subplot(%s)" % (str(k), line)) exec("ax%s.set_xlim(left=xmin, right=xmax, auto=False)" % (str(k))) # #--- if ymin and ymax are given, use them # if len(ylims) == dlen: ymin = ylims[k][0] ymax = ylims[k][1] exec("ax%s.set_ylim(ymin=ymin, ymax=ymax, auto=False)" % (str(k))) xdata = atime[k] ydata = data[k] exec( "ax%s.plot(xdata, ydata, color='blue', marker='.', markersize=1, lw=0)" % (str(k))) # #--- for the case that a fitting line is requested if lfit == 1: [xt, yt] = remove_out_layer(xdata, ydata, ymin, ymax) try: [a, b, e] = rfit.robust_fit(xt, yt) y1 = a + b * xmin y2 = a + b * xmax yrange = [y1, y2] exec("ax%s.plot(xrange, yrange, color='red', lw=2)" % (str(k))) except: pass ydiff = ymax - ymin ytext = ymax - 0.1 * ydiff line = "Slope: " + '%3.3f' % (round(b, 3)) plt.text(xtext, ytext, line) # #--- y axis label # exec("ax%s.set_ylabel('%s')" % (str(k), ylab[k])) # #--- x axis label # exec('ax%s.set_xlabel("%s")' % (str(mlen), xlabel)) # #--- add x ticks label only on the last panel # for k in range(0, dlen): ax = 'ax' + str(k) if k != mlen: line = eval("%s.get_xticklabels()" % (ax)) for label in line: label.set_visible(False) else: pass # #--- save the plot in a file # fig = matplotlib.pyplot.gcf() height = 2.0 * dlen + 0.5 fig.set_size_inches(10.0, height) outfile = web_dir + 'Plots/' + outname plt.savefig(outfile, format='png', dpi=100.0)
def plotPanel(xmin, xmax, yMinSets, yMaxSets, xSets, ySets, eSets, xname, yname, entLabels, ydiv): """ This function plots multiple data in separate panels Input: xmin, xmax, ymin, ymax: plotting area xSets: a list of lists containing x-axis data ySets: a list of lists containing y-axis data eSets: a list of lists containing error values of y-axis yMinSets: a list of ymin yMaxSets: a list of ymax entLabels: a list of the names of each data ydiv: a location of dividing spot Output: a png plot: out.png """ # #--- set line color list # colorList = ('blue', 'green', 'red', 'aqua', 'lime', 'fuchsia', 'maroon', 'black', 'yellow', 'olive') # #--- clean up the plotting device # plt.close('all') # #---- set a few parameters # mpl.rcParams['font.size'] = 13 props = font_manager.FontProperties(size=9) plt.subplots_adjust(hspace=0.06) tot = len(entLabels) # #--- start plotting each data # for i in range(0, len(entLabels)): axNam = 'ax' + str(i) # #--- setting the panel position # j = i + 1 if i == 0: line = str(tot) + '1' + str(j) else: line = str(tot) + '1' + str(j) + ', sharex=ax0' line = str(tot) + '1' + str(j) exec "%s = plt.subplot(%s)" % (axNam, line) exec "%s.set_autoscale_on(False)" % (axNam) #---- these three may not be needed for the new pylab, but exec "%s.set_xbound(xmin,xmax)" % (axNam) #---- they are necessary for the older version to set exec "%s.set_xlim(xmin=xmin, xmax=xmax, auto=False)" % (axNam) exec "%s.set_ylim(ymin=yMinSets[i], ymax=yMaxSets[i], auto=False)" % (axNam) # #--- since the cti seems evolving after year <ydiv>, fit two different lines before and after that point # xdata = xSets[i] ydata = ySets[i] edata = eSets[i] xdata1 = [] ydata1 = [] edata1 = [] xdata2 = [] ydata2 = [] edata2 = [] for k in range(0, len(xdata)): if xdata[k] < ydiv: xdata1.append(xdata[k]) ydata1.append(ydata[k]) edata1.append(edata[k]) else: xdata2.append(xdata[k]) ydata2.append(ydata[k]) edata2.append(edata[k]) # #---- actual data plotting # p, = plt.plot(xdata, ydata, color=colorList[i], marker='*', markersize=4.0, lw =0) errorbar(xdata, ydata, yerr=edata, color=colorList[i], markersize=4.0, fmt='*') # #--- fitting straight lines with robust method and plot the results # xdata1 = numpy.array(xdata1) ydata1 = numpy.array(ydata1) edata1 = numpy.array(edata1) (intc, slope, err) = robust.robust_fit(xdata1, ydata1) ystart = intc + slope * 2000 ystop = intc + slope * ydiv lxdata = [2000, ydiv] lydata = [ystart, ystop] p, = plt.plot(lxdata, lydata, color=colorList[i], marker='', markersize=1.0, lw =2) xdata2 = numpy.array(xdata2) ydata2 = numpy.array(ydata2) edata2 = numpy.array(edata2) (intc2, slope2,err) = robust.robust_fit(xdata2, ydata2) ystart = intc2 + slope2 * ydiv ystop = intc2 + slope2 * xmax lxdata = [ydiv, xmax] lydata = [ystart, ystop] p, = plt.plot(lxdata, lydata, color=colorList[i], marker='', markersize=1.0, lw =2) # #--- add legend # lslope = round(slope, 3) lslope2 = round(slope2, 3) line = entLabels[i] + ' Slope: ' + str(lslope) + ' (before '+ str(ydiv) + ') / ' + str(lslope2) + ' (after '+ str(ydiv) + ')' leg = legend([p], [line], prop=props, loc=2) leg.get_frame().set_alpha(0.5) exec "%s.set_ylabel(yname, size=8)" % (axNam) # #--- add x ticks label only on the last panel # for i in range(0, tot): ax = 'ax' + str(i) if i != tot-1: exec "line = %s.get_xticklabels()" % (ax) for label in line: label.set_visible(False) else: pass xlabel(xname) # #--- set the size of the plotting area in inch (width: 10.0in, height 2.08in x number of panels) # fig = matplotlib.pyplot.gcf() height = (2.00 + 0.08) * tot fig.set_size_inches(10.0, height) # #--- save the plot in png format # plt.savefig('out.png', format='png', dpi=100)
def plot_step_delta(steps, delta, outname, title): """ plot a single data set on a single panel Input: steps --- translation steps delta --- sim temperature difference before and after translation outname --- the name of output file title --- title of the plot Output: png plot named <outname> """ fsize = 14 #--- font size fweight = 'bold' #--- font weight psize = 3.0 #--- plotting point size marker = '.' #--- marker shape pcolor = 7 #--- color of the point lcolor = 4 #--- color of the fitted lin lsize = 3.0 #--- fitted line width resolution = 300 #--- the resolution of the plot # #--- close everything opened before # plt.close('all') # #--- set font size # mpl.rcParams['font.size'] = fsize mpl.rcParams['font.weight'] = fweight mpl.rcParams['axes.linewidth'] = 2 #set(0,'defaultlinelinewidth',8) props = font_manager.FontProperties(size=12) plt.subplots_adjust(hspace=0.05) [xmin, xmax, ymin, ymax] = set_min_max(steps, delta) ymin = -5 ymax = 20 # #--- set plotting range # plt.subplot(111) xlim((xmin, xmax)) ylim((ymin, ymax)) # #--- plot data # plt.plot(steps, delta, color='fuchsia', marker='*', markersize=psize, lw=0) # #--- fit line --- use robust method # (sint, slope, serr) = robust.robust_fit(steps, delta) lslope = '%2.3f' % (round(slope * 10000, 3)) # #--- plot fitted line # start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax], [start, stop], color=colorList[lcolor], lw=lsize) label = 'Slope: ' + str(lslope) + ' ($^o$C / 10$^4$ steps)' # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.05 * xdiff ydiff = ymax - ymin ypos = ymax - 0.10 * ydiff xpos3 = xmax - 0.20 * xdiff ypos3 = ymax + 0.005 * ydiff xpos4 = xmin + 0.05 * xdiff plt.text(xpos, ypos, label, size=fsize) # #--- label axes # yname = '$\Delta$ T ($^o$C)' plt.ylabel(yname, size=fsize, weight=fweight) # #--- label x axis # xname = 'Translation Steps' plt.xlabel(xname, size=fsize, weight=fweight) # #--- title # plt.text(xpos3, ypos3, title, size=fsize) plt.text(xpos4, ypos3, 'TSC Motor Temp vs. Move Distance', size=fsize) # # #--- set the size of the plotting area in inch (width: 10.0in, height 5 in) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 5.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plot_multi_entries(xmin, xmax, ymin, ymax, xSets, ySets, xname, yname, entLabels, outname, yerror = 0,fsize = 9, psize = 2.0,lsize=0, resolution=100, linefit=0, connect=0): """ This function plots multiple data in a single panel. Input: xmin, xmax, ymin, ymax: plotting area xSets: a list of lists containing x-axis data ySets: a list of lists containing y-axis data xname: a name of x-axis yname: a name of y-axis entLabels: a list of the names of each data outname: a name of plotting file yerror: a list of lists of y error, if it is '0', no error bar, default = 0 fsize: font size, default = 9 psize: plotting point size, default = 2.0 lsize: fitted line width, default = 1 resolution: plotting resolution in dpi linefit --- if it is 1, fit a line estimated by robust method connect: connected line size. if it is '0', no connected line Output: a png plot: out.png """ colorList = ('green', 'blue', 'red', 'lime', 'line', 'yellow', 'maroon', 'black', 'fushia', 'olive') markerList = ('o', '*', '+', '^', 's', 'D', '1', '2', '3', '4') plt.close('all') # #---- set a few parameters # mpl.rcParams['font.size'] = fsize props = font_manager.FontProperties(size=9) plt.subplots_adjust(hspace=0.08) # #---- set a panel # ax = plt.subplot(111) ax.set_autoscale_on(False) #---- these three may not be needed for the new pylab, but ax.set_xbound(xmin,xmax) #---- they are necessary for the older version to set ax.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax.set_ylim(ymin=ymin, ymax=ymax, auto=False) tot = len(entLabels) # #--- start plotting each data set # lnamList = [] for i in range(0, tot): xdata = xSets[i] ydata = ySets[i] color = colorList[i] marker = markerList[0] label = entLabels[i] if tot > 1: lnam = 'p' + str(i) lnamList.append(lnam) exec('%s, = plt.plot(xdata, ydata, color="%s", lw =lsize , marker="%s", markersize=3, label=entLabels[i])' %(lnam, color, marker)) else: # #--- if there is only one data set, ignore legend # plt.plot(xdata, ydata, color=color, lw =connect , marker='o', markersize=psize) if yerror != 0: p, = plt.errorbar(xdata, ydata, yerr=yerror[i], lw = 0, elinewidth=1) if linefit > 0: (sint, slope,serror) = robust.robust_fit(xdata, ydata) start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax],[start,stop], color=color, lw =lsize ) # #--- add legend # if tot > 1: line = '[' for ent in lnamList: if line == '[': line = line + ent else: line = line +', ' + ent line = line + ']' leg = eval("legend(%s, entLabels, prop=props)" % (line)) leg.get_frame().set_alpha(0.5) ax.set_xlabel(xname, size=fsize) ax.set_ylabel(yname, size=fsize) # #--- set the size of the plotting area in inch (width: 10.0in, height 5.0in) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 5.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plot_multi_panel(xmin, xmax, yMinSets, yMaxSets, xSets, ySets, xname, yname, entLabels, outname, yerror=0, fsize=9, psize=2.0, marker='o', pcolor=7, lcolor=7, lsize=0, resolution=100, linefit=0, connect=0): """ This function plots multiple data in separate panels Input: xmin, xmax plotting range of x axis xSets: a list of lists containing x-axis data ySets: a list of lists containing y-axis data yMinSets: a list of ymin yMaxSets: a list of ymax xname: x axis label yname: y axis label entLabels: a list of the names of each data outname: a name of plotting file yerror: a list of lists of error on y, or if it is '0' no error bar on y, default = 0 fsize: font size, default = 9 psize: plotting point size, default = 2.0 marker: marker shape, defalut = 'o' pcolor: color of the point, default= 7 ---> 'black' lcolor: color of the fitted line, default = 7 ---> 'black' colorList = ('blue', 'red', 'green', 'aqua', 'fuchsia','lime', 'maroon', 'black', 'olive', 'yellow') lsize: fitted line width, defalut = 1 resolution: plotting resolution in dpi, default = 100 linefit: if 1, linear line is fitted, default: 0 connect: connected line size. if it is '0', no connected line Output: a png plot: outname """ # #--- set line color list # colorList = ('blue', 'red', 'green', 'aqua', 'fuchsia', 'lime', 'maroon', 'black', 'olive', 'yellow') # #--- clean up the plotting device # plt.close('all') # #---- set a few parameters # mpl.rcParams['font.size'] = fsize props = font_manager.FontProperties(size=9) plt.subplots_adjust(hspace=0.08) tot = len(entLabels) # #--- start plotting each data # for i in range(0, tot): axNam = 'ax' + str(i) # #--- setting the panel position # j = i + 1 if i == 0: line = str(tot) + '1' + str(j) else: line = str(tot) + '1' + str(j) + ', sharex=ax0' line = str(tot) + '1' + str(j) exec("%s = plt.subplot(%s)" % (axNam, line)) exec("%s.set_autoscale_on(False)" % ( axNam)) #---- these three may not be needed for the new pylab, but exec("%s.set_xbound(xmin,xmax)" % (axNam)) #---- they are necessary for the older version to set exec("%s.set_xlim(xmin=xmin, xmax=xmax, auto=False)" % (axNam)) exec("%s.set_ylim(ymin=yMinSets[i], ymax=yMaxSets[i], auto=False)" % (axNam)) xdata = xSets[i] ydata = ySets[i] # #---- actual data plotting # p, = plt.plot(xdata, ydata, color=colorList[pcolor], marker=marker, markersize=psize, lw=connect) if yerror != 0: p, = plt.errorbar(xdata, ydata, yerr=yerror[i], lw=0, elinewidth=1) if linefit > 0: (sint, slope, serr) = robust.robust_fit(xdata, ydata) start = sint + slope * xmin stop = sint + slope * xmax p, = plt.plot([xmin, xmax], [start, stop], color=colorList[lcolor], lw=lsize) # #--- add legend # leg = legend([p], [entLabels[i]], prop=props, loc=2) leg.get_frame().set_alpha(0.5) exec("%s.set_ylabel(yname, size=fsize)" % (axNam)) # #--- add x ticks label only on the last panel # for i in range(0, tot): ax = 'ax' + str(i) if i != tot - 1: exec("line = %s.get_xticklabels()" % (ax)) for label in line: label.set_visible(False) else: pass xlabel(xname) # #--- set the size of the plotting area in inch (width: 10.0in, height 2.08in x number of panels) # fig = matplotlib.pyplot.gcf() height = (2.00 + 0.08) * tot fig.set_size_inches(10.0, height) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def multi_section_linear_fit(x, y, xmin, xmax, bpoints, snum=100, shift=0): """ compute two line fit on a data and return lists of fitted line data input: x --- independent data y --- dependent data xmin --- min of x xmax --- max of x bpoints --- a list of breaking point e.g, [2000] snum --- numbers of data points to return shift --- whether shift data. useful when x is far from 0 output: int_list --- a list of intercept values slope_list --- a list of slope values serr_list --- a list of errors of slope xa_list --- a list of x points for the fitted line ya_list --- a list of y points for the fitted line """ # #--- add xmax as the last "breaking" point # bsets = [] for ent in bpoints: bsets.append(ent) bsets.append(xmax) bpnum = len(bsets) # #--- separate data into sections # xs = [] ys = [] for k in range(0, bpnum): xs.append([]) ys.append([]) # #--- when the x is far from 0, robust method does not work well; #--- so add "shift" (possibly negative) to make the data near 0 # for i in range(0, len(x)): for k in range(0, bpnum): if x[i] <= bsets[k]: xs[k].append(x[i] + shift) ys[k].append(y[i]) break # #--- compute intercept and slope for each section # int_list = [] slope_list = [] serr_list = [] xa_list = [] ya_list = [] for k in range(0, bpnum): # #--- if the numbers of the data is too low, skip the section # if len(xs[k]) < 3: continue try: (sint, slope, serror) = robust.robust_fit(xs[k], ys[k]) except: sint = 0.0 slope = 0.0 serror = 0.0 int_list.append(sint) slope_list.append(slope) serr_list.append(serror) # #--- create plotting data sets # smin = min(xs[k]) smax = max(xs[k]) step = (smax - smin) / float(snum) if k > 0: add = bsets[k - 1] + shift else: add = 0 tx_list = [] ty_list = [] for j in range(0, snum): tx = step * j + add ty = sint + slope * tx # #--- shift back x value # tx -= shift tx_list.append(tx) ty_list.append(ty) xa_list.append(tx_list) ya_list.append(ty_list) return [int_list, slope_list, serr_list, xa_list, ya_list]
def plot_data(sdata, mdata, year, msid, oname, ymin, ymax, ydrange, msid_list, lupdate): """ create two panel plots for msid vs sun angle and its deviation input: sdata --- a list of sun angle data mdata --- a list of msid data (mid/min/max) year --- year of the plot msid --- msid oname --- output name ymin --- y min of the first plot ymax --- y max of the first plot ydrange --- the range of the deviation y axis msid_list --- msid list lupdate --- if 1, update y plotting range. output: oname in png format """ plt.close('all') fig, ax = plt.subplots(1, figsize=(8, 6)) props = font_manager.FontProperties(size=14) mpl.rcParams['font.size'] = 14 mpl.rcParams['font.weight'] = 'bold' xmin = 40 xmax = 170 if ymax == -999: [ymin, ymax, ypos] = set_y_plot_range(mdata) # #--- since we want to all years to be in the same plotting range, this scripts adjust #--- the plotting range. you may need to run a couple of times for the full range to #--- adjust plotting range for the all plot # [ymin, ymax, ydev] = update_yrange(msid_list, msid, ymin=ymin, ymax=ymax, ydev=ydrange) else: if lupdate == 2: [ymint, ymaxt, ypos] = set_y_plot_range(mdata) mchk = 0 if ymint < ymin: ymin = ymint mchk = 1 if ymaxt > ymaxt: ymax = ymaxt mchk = 1 if mchk == 1: [ymin, ymax, ydev] = update_yrange(msid_list, msid, ymin=ymin, ymax=ymax, ydev=drange) ydiff = ymax - ymin ypos = ymax - 0.1 * ydiff ax1 = plt.subplot(211) ax1.set_xlim(xmin, xmax) ax1.set_ylim(ymin, ymax) ax1.set_xlabel("Sun Angle") ax1.set_ylabel(msid.upper()) # #--- set the size of plot # fig.set_size_inches(10.0, 5.0) fig.tight_layout() # #---- trending plots # points = ax1.scatter(sdata, mdata, marker='o', s=20, lw=0) # #---- envelope # period = 5 [xmc, ymc, xmb, ymb, xmt, ymt] = create_trend_envelope(sdata, mdata, period) # #--- trending area # try: ax1.fill_between(xmc, ymb, ymt, facecolor='#00FFFF', alpha=0.3, interpolate=True) except: pass # #--- center moving average # ax1.plot(xmc, ymc, color='#E9967A', lw=4) plt.text(50, ypos, str(year)) # #---- derivative plot # [xd, yd, ad] = find_deriv(sdata, mdata, step=5) [dymin, dymax, dypos] = set_y_plot_range(yd) if lupdate == 2: if abs(dymin) > abs(dymax): dymax = abs(dymin) ymax = ydrange ymin = -1.0 * abs(ymax) ydiff = ymax - ymin ypos = ymax - 0.1 * ydiff ax2 = plt.subplot(212) ax2.set_xlim(xmin, xmax) ax2.set_ylim(ymin, ymax) ax2.set_xlabel("Sun Angle") line = msid + '/ Deg' ax2.set_ylabel(line) points = ax2.scatter(xd, yd, marker='o', s=20, lw=0) try: try: [a, b, e] = rbl.robust_fit(xd, yd) except: [a, b, e] = least_sq(xd, yd, 96) except: a = 0 b = 0 ys = a + b * xmin ye = a + b * xmax ax2.plot([xmin, xmax], [ys, ye], color='green', lw=3) line = 'Slope: ' + "%3.3e" % (b) mpl.rcParams['font.size'] = 12 plt.title('dy / d(sun angle)', loc='left') plt.text(50, ypos, line) # #--- set the size of plot # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 10.0) fig.tight_layout() plt.savefig(oname, format='png', dpi=100) plt.close('all')
def linear_fit(x, y): """ linear fitting function with -99999 error removal Input: x --- independent variable array y --- dependent variable array Output: intc --- intercept slope--- slope """ # #--- first remove error entries # sum = 0 sum2 = 0 tot = 0 for i in range(0, len(y)): if y[i] > 0: sum += y[i] sum2 += y[i] * y[i] tot += 1 if tot > 0: avg = sum / tot sig = math.sqrt(sum2 / tot - avg * avg) else: avg = 3.0 lower = 0.0 upper = avg + 2.0 xn = [] yn = [] for i in range(0, len(x)): # if (y[i] > 0) and (y[i] < yupper): #--- removing -99999/9999 error if (y[i] > lower) and (y[i] < upper): xn.append(x[i]) yn.append(y[i]) if len(yn) > 10: # [intc, slope, serr] = robust.robust_fit(xn, yn, iter=50) [intc, slope, serr] = robust.robust_fit(xn, yn, iter=1) else: [intc, slope, serr] = [0, 0, 0] # #--- modify array to numpy array # # d = numpy.array(xn) # v = numpy.array(yn) # #--- kmpfit # # param = [0, 1] # fitobj = kmpfit.Fitter(residuals=residuals, data=(d,v)) # fitobj.fit(params0=param) # # [intc, slope] = fitobj.params # [ierr, serr] = fitobj.stderr # #--- chauvenet exclusion of outlyers and linear fit # # [intc, slope, ierr, serr] = chauv.run_chauvenet(d,v) return [intc, slope, 0.0, serr]
def plot_data(x, y, xmin, xmax, ymin, ymax, inst, ychoice): """ plot data input: x --- a list of independent variable y --- a list of dependent variable xmin --- x min xmax --- x max ymin --- y min ymax --- y max inst --- instrument name ychoice --- y or z output: <web_dir>/Plots/inst_<ychoice>.png """ # #--- close everything opened before # plt.close('all') # #--- set font size # mpl.rcParams['font.size'] = 16 props = font_manager.FontProperties(size=16) # #--- set plotting range # ax = plt.subplot(111) ax.set_autoscale_on(False) ax.set_xbound(xmin,xmax) ax.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax.set_ylim(ymin=ymin, ymax=ymax, auto=False) # #--- plot data # plt.plot(x, y, color='blue', marker='.', markersize=8, lw=0) # #--- put y = 0 line # plt.plot([xmin, xmax], [0, 0], color='black', linestyle='-.', lw=1) # #--- fit a line # (sint, slope, serr) = robust.robust_fit(x, y) ybeg = sint + slope * xmin yend = sint + slope * xmax plt.plot([xmin, xmax], [ybeg, yend], color='red', lw=2) # #--- add text # line = 'Slope: %3.2f' % (slope) xpos = xmin + 0.05 * (xmax - xmin) ypos = 1.6 plt.text(xpos, ypos, line, color='red') plt.xlabel('Time (Year)') ylabel = ychoice.upper() + ' Axis Error (arcsec)' plt.ylabel(ylabel) # #--- save the plot in png format # width = 10.0 height = 5.0 resolution = 200 outname = web_dir + 'Plots/' + inst + '_' + ychoice + '.png' fig = matplotlib.pyplot.gcf() fig.set_size_inches(width, height) plt.tight_layout() plt.savefig(outname, format='png', dpi=resolution) plt.close('all')
def plot_single_panel(xmin, xmax, ymin, ymax, xdata, ydata, yerror, xname, yname, label, outname, fsize=9, psize=2.0, marker='o', pcolor=7, lcolor=7, lsize=1, resolution=100, linefit=1, connect=0): """ plot a single data set on a single panel Input: xmin --- min x xmax --- max x ymin --- min y ymax --- max y xdata --- independent variable ydata --- dependent variable yerror --- error in y axis; if it is '0', no error bar xname --- x axis label ynane --- y axis label label --- a text to indecate what is plotted outname --- the name of output file fsize --- font size, default = 9 psize --- plotting point size, default = 2.0 marker --- marker shape, defalut = 'o' pcolor --- color of the point, default= 7 ---> 'black' lcolor --- color of the fitted line, default = 7 ---> 'black' colorList = ('blue', 'red', 'green', 'aqua', 'fuchsia','lime', 'maroon', 'black', 'olive', 'yellow') lsize: fitted line width, defalut = 1 resolution-- the resolution of the plot in dpi linefit --- if it is 1, fit a line estimated by robust method connect --- if it is > 0, lsize data point with a line, the larger the value thinker the line Output: png plot named <outname> """ colorList = ('blue', 'green', 'red', 'aqua', 'lime', 'fuchsia', 'maroon', 'black', 'yellow', 'olive') # #--- fit line --- use robust method # if linefit == 1: (sint, slope, serr) = robust.robust_fit(xdata, ydata) lslope = '%2.3f' % (round(slope, 3)) # #--- close everything opened before # plt.close('all') # #--- set font size # mpl.rcParams['font.size'] = fsize props = font_manager.FontProperties(size=9) # #--- set plotting range # ax = plt.subplot(111) ax.set_autoscale_on(False) ax.set_xbound(xmin, xmax) ax.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax.set_ylim(ymin=ymin, ymax=ymax, auto=False) # #--- plot data # plt.plot(xdata, ydata, color=colorList[pcolor], marker=marker, markersize=psize, lw=connect) # #--- plot error bar # if yerror != 0: plt.errorbar(xdata, ydata, yerr=yerror, lw=0, elinewidth=1) # #--- plot fitted line # if linefit == 1: start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax], [start, stop], color=colorList[lcolor], lw=lsize) # #--- label axes # plt.xlabel(xname, size=fsize) plt.ylabel(yname, size=fsize) # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.5 * xdiff ydiff = ymax - ymin ypos = ymax - 0.08 * ydiff if linefit == 1: label = label + ': Slope: ' + str(lslope) plt.text(xpos, ypos, label, size=fsize) # #--- set the size of the plotting area in inch (width: 10.0in, height 5 in) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 5.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plot_single_panel(xmin, xmax, ymin, ymax, xdata, ydata, yerror, xname, yname, label, outname, fsize = 9, psize = 2.0, marker = 'o', pcolor =7, lcolor=7,lsize=1, resolution=100, linefit=1, connect=0): """ plot a single data set on a single panel Input: xmin --- min x xmax --- max x ymin --- min y ymax --- max y xdata --- independent variable ydata --- dependent variable yerror --- error in y axis; if it is '0', no error bar xname --- x axis label ynane --- y axis label label --- a text to indecate what is plotted outname --- the name of output file fsize --- font size, default = 9 psize --- plotting point size, default = 2.0 marker --- marker shape, defalut = 'o' pcolor --- color of the point, default= 7 ---> 'black' lcolor --- color of the fitted line, default = 7 ---> 'black' colorList = ('blue', 'red', 'green', 'aqua', 'fuchsia','lime', 'maroon', 'black', 'olive', 'yellow') lsize: fitted line width, defalut = 1 resolution-- the resolution of the plot in dpi linefit --- if it is 1, fit a line estimated by robust method connect --- if it is > 0, lsize data point with a line, the larger the value thinker the line Output: png plot named <outname> """ colorList = ('blue', 'green', 'red', 'aqua', 'lime', 'fuchsia', 'maroon', 'black', 'yellow', 'olive') # #--- fit line --- use robust method # if linefit == 1: (sint, slope, serr) = robust.robust_fit(xdata, ydata) lslope = '%2.3f' % (round(slope, 3)) # #--- close everything opened before # plt.close('all') # #--- set font size # mpl.rcParams['font.size'] = fsize props = font_manager.FontProperties(size=9) # #--- set plotting range # ax = plt.subplot(111) ax.set_autoscale_on(False) ax.set_xbound(xmin,xmax) ax.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax.set_ylim(ymin=ymin, ymax=ymax, auto=False) # #--- plot data # plt.plot(xdata, ydata, color=colorList[pcolor], marker=marker, markersize=psize, lw = connect) # #--- plot error bar # if yerror != 0: plt.errorbar(xdata, ydata, yerr=yerror, lw = 0, elinewidth=1) # #--- plot fitted line # if linefit == 1: start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax], [start, stop], color=colorList[lcolor], lw = lsize) # #--- label axes # plt.xlabel(xname, size=fsize) plt.ylabel(yname, size=fsize) # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.5 * xdiff ydiff = ymax - ymin ypos = ymax - 0.08 * ydiff if linefit == 1: label = label + ': Slope: ' + str(lslope) plt.text(xpos, ypos, label, size=fsize) # #--- set the size of the plotting area in inch (width: 10.0in, height 5 in) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 5.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plotPanel(xmin, xmax, yMinSets, yMaxSets, xSets, ySets, eSets, xname, yname, entLabels, ydiv): """ This function plots multiple data in separate panels Input: xmin, xmax, ymin, ymax: plotting area xSets: a list of lists containing x-axis data ySets: a list of lists containing y-axis data eSets: a list of lists containing error values of y-axis yMinSets: a list of ymin yMaxSets: a list of ymax entLabels: a list of the names of each data ydiv: a location of dividing spot Output: a png plot: out.png """ # #--- set line color list # colorList = ('blue', 'green', 'red', 'aqua', 'lime', 'fuchsia', 'maroon', 'black', 'yellow', 'olive') # #--- clean up the plotting device # plt.close('all') # #---- set a few parameters # mpl.rcParams['font.size'] = 13 props = font_manager.FontProperties(size=9) plt.subplots_adjust(hspace=0.06) tot = len(entLabels) # #--- start plotting each data # for i in range(0, len(entLabels)): axNam = 'ax' + str(i) # #--- setting the panel position # j = i + 1 if i == 0: line = str(tot) + '1' + str(j) else: line = str(tot) + '1' + str(j) + ', sharex=ax0' line = str(tot) + '1' + str(j) exec "%s = plt.subplot(%s)" % (axNam, line) exec "%s.set_autoscale_on(False)" % (axNam) #---- these three may not be needed for the new pylab, but exec "%s.set_xbound(xmin,xmax)" % (axNam) #---- they are necessary for the older version to set exec "%s.set_xlim(xmin=xmin, xmax=xmax, auto=False)" % (axNam) exec "%s.set_ylim(ymin=yMinSets[i], ymax=yMaxSets[i], auto=False)" % (axNam) # #--- since the cti seems evolving after year <ydiv>, fit two different lines before and after that point # xdata = xSets[i] ydata = ySets[i] edata = eSets[i] xdata1 = [] ydata1 = [] edata1 = [] xdata2 = [] ydata2 = [] edata2 = [] for k in range(0, len(xdata)): if xdata[k] < ydiv: xdata1.append(xdata[k]) ydata1.append(ydata[k]) edata1.append(edata[k]) else: xdata2.append(xdata[k]) ydata2.append(ydata[k]) edata2.append(edata[k]) # #---- actual data plotting # p, = plt.plot(xdata, ydata, color=colorList[i], marker='*', markersize=4.0, lw =0) errorbar(xdata, ydata, yerr=edata, color=colorList[i], markersize=4.0, fmt='*') # #--- fitting straight lines with robust method and plot the results # xdata1 = numpy.array(xdata) ydata1 = numpy.array(ydata) edata1 = numpy.array(edata) (intc, slope, err) = robust.robust_fit(xdata1, ydata1) ystart = intc + slope * 2000 ystop = intc + slope * xmax lxdata = [2000, xmax] lydata = [ystart, ystop] p, = plt.plot(lxdata, lydata, color=colorList[i], marker='', markersize=1.0, lw =2) # #--- add legend # lslope = round(slope, 3) line = entLabels[i] + ' Slope: ' + str(lslope) leg = legend([p], [line], prop=props, loc=2) leg.get_frame().set_alpha(0.5) exec "%s.set_ylabel(yname, size=8)" % (axNam) # #--- add x ticks label only on the last panel # for i in range(0, tot): ax = 'ax' + str(i) if i != tot-1: exec "line = %s.get_xticklabels()" % (ax) for label in line: label.set_visible(False) else: pass xlabel(xname) # #--- set the size of the plotting area in inch (width: 10.0in, height 2.08in x number of panels) # fig = matplotlib.pyplot.gcf() height = (2.00 + 0.08) * tot fig.set_size_inches(10.0, height) # #--- save the plot in png format # plt.savefig('out.png', format='png', dpi=100)
def plot_multi_panel(xmin, xmax, yMinSets, yMaxSets, xSets, ySets, xname, yname, entLabels, outname, yerror=0, fsize = 9, psize = 2.0, marker = 'o', pcolor =7, lcolor=7,lsize=0, resolution=100, linefit=0, connect=0): """ This function plots multiple data in separate panels Input: xmin, xmax plotting range of x axis xSets: a list of lists containing x-axis data ySets: a list of lists containing y-axis data yMinSets: a list of ymin yMaxSets: a list of ymax xname: x axis label yname: y axis label entLabels: a list of the names of each data outname: a name of plotting file yerror: a list of lists of error on y, or if it is '0' no error bar on y, default = 0 fsize: font size, default = 9 psize: plotting point size, default = 2.0 marker: marker shape, defalut = 'o' pcolor: color of the point, default= 7 ---> 'black' lcolor: color of the fitted line, default = 7 ---> 'black' colorList = ('blue', 'red', 'green', 'aqua', 'fuchsia','lime', 'maroon', 'black', 'olive', 'yellow') lsize: fitted line width, defalut = 1 resolution: plotting resolution in dpi, default = 100 linefit: if 1, linear line is fitted, default: 0 connect: connected line size. if it is '0', no connected line Output: a png plot: outname """ # #--- set line color list # colorList = ('blue', 'red', 'green', 'aqua', 'fuchsia','lime', 'maroon', 'black', 'olive', 'yellow') # #--- clean up the plotting device # plt.close('all') # #---- set a few parameters # mpl.rcParams['font.size'] = fsize props = font_manager.FontProperties(size=9) plt.subplots_adjust(hspace=0.08) tot = len(entLabels) # #--- start plotting each data # for i in range(0, tot): axNam = 'ax' + str(i) # #--- setting the panel position # j = i + 1 if i == 0: line = str(tot) + '1' + str(j) else: line = str(tot) + '1' + str(j) + ', sharex=ax0' line = str(tot) + '1' + str(j) exec "%s = plt.subplot(%s)" % (axNam, line) exec "%s.set_autoscale_on(False)" % (axNam) #---- these three may not be needed for the new pylab, but exec "%s.set_xbound(xmin,xmax)" % (axNam) #---- they are necessary for the older version to set exec "%s.set_xlim(xmin=xmin, xmax=xmax, auto=False)" % (axNam) exec "%s.set_ylim(ymin=yMinSets[i], ymax=yMaxSets[i], auto=False)" % (axNam) xdata = xSets[i] ydata = ySets[i] # #---- actual data plotting # p, = plt.plot(xdata, ydata, color=colorList[pcolor], marker= marker, markersize=psize, lw =connect) if yerror != 0: p, = plt.errorbar(xdata, ydata, yerr=yerror[i], lw = 0, elinewidth=1) if linefit > 0: (sint, slope, serr) = robust.robust_fit(xdata, ydata) start = sint + slope * xmin stop = sint + slope * xmax p, = plt.plot([xmin, xmax],[start,stop], color=colorList[lcolor], lw =lsize) # #--- add legend # leg = legend([p], [entLabels[i]], prop=props, loc=2) leg.get_frame().set_alpha(0.5) exec "%s.set_ylabel(yname, size=fsize)" % (axNam) # #--- add x ticks label only on the last panel # for i in range(0, tot): ax = 'ax' + str(i) if i != tot-1: exec "line = %s.get_xticklabels()" % (ax) for label in line: label.set_visible(False) else: pass xlabel(xname) # #--- set the size of the plotting area in inch (width: 10.0in, height 2.08in x number of panels) # fig = matplotlib.pyplot.gcf() height = (2.00 + 0.08) * tot fig.set_size_inches(10.0, height) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plot_sim_temp_data(xmin, xmax, simtx, simtl, simth, mxs, mys, mxe, mye,\ delta, stime, angle, outname, yind, title,skip): """ plot sim trend, temp delta, and pitch angle trends Input: xmin --- starting date, either in year or yday xmax --- ending date, either in year or yday simtx --- time in either year or yday simtl --- starting sim temperature simth --- ending sim temperature mxs --- moving average time mys --- moving average of simtl mye --- moving average of simth delta --- <sim ending temp> - <sim starting temp> stime --- time in either year or yday of picthc angle data angle --- pitch angle outname --- output file name yind --- whether this is a full range plot or yearly plot title --- title of the plot skip --- if 1, skip moving averge plots Output: png plot named <outname> """ # #--- several setting for plotting # fsize = 14 #--- font size fweight = 'bold' #--- font weight psize = 3.0 #--- plotting point size marker = '.' #--- marker shape pcolor = 7 #--- color of the point lcolor = 4 #--- color of the fitted lin lsize = 3.0 #--- fitted line width resolution = 300 #--- the resolution of the plot # #--- close everything opened before # plt.close('all') # #--- set font size # mpl.rcParams['font.size'] = fsize mpl.rcParams['font.weight'] = fweight mpl.rcParams['axes.linewidth'] = 2 #set(0,'defaultlinelinewidth',8) props = font_manager.FontProperties(size=12) plt.subplots_adjust(hspace=0.05) # #--- set grid to 3 rows or height rtio 3:1:1 # gs = gridspec.GridSpec(3, 1, height_ratios=[3, 1, 1]) # # #--- first panel: moter temperature trend ------------------------------ # # #---- set ymin and ymax # ymin = -40 ymax = 60 # #--- set plotting range # #plt.subplot(311) ax0 = plt.subplot(gs[0]) line = ax0.get_xticklabels() for label in line: label.set_visible(False) xlim((xmin, xmax)) ylim((ymin, ymax)) # #--- plot data # plt.plot(simtx, simtl, color=colorList[0], marker=marker, markersize=psize, lw=0) plt.plot(simtx, simth, color=colorList[2], marker=marker, markersize=psize, lw=0) for i in range(0, len(simtx)): xset = [simtx[i], simtx[i]] yset = [simtl[i], simth[i]] plt.plot(xset, yset, color=colorList[3], marker='', markersize=0, lw=1) # #--- plot moving average # if skip == 0: plt.plot(mxs, mys, color=colorList[0], marker='', markersize=0, lw=2) plt.plot(mxe, mye, color=colorList[2], marker='', markersize=0, lw=2) # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.05 * xdiff ydiff = ymax - ymin ypos1 = ymax - 0.08 * ydiff ypos2 = ymax - 0.12 * ydiff xpos3 = xmax - 0.20 * xdiff ypos3 = ymax + 0.005 * ydiff xpos4 = xmin + 0.05 * xdiff val1 = numpy.mean(simtl) val1 = '%2.2f' % round(val1, 2) val2 = numpy.mean(simth) val2 = '%2.2f' % round(val2, 2) label = 'T Stop Average: ' + str(val2) + ' $^o$C' plt.text(xpos, ypos1, label, size=fsize, color=colorList[2]) label = 'T Start Average: ' + str(val1) + ' $^o$C' plt.text(xpos, ypos2, label, size=fsize, color=colorList[0]) plt.text(xpos3, ypos3, title, size=fsize) plt.text(xpos4, ypos3, 'SIM TSC Moter Temperatures', size=fsize) # #--- label axes # yname = 'TSC Motor Temp ($^o$C)' plt.ylabel(yname, size=fsize, weight=fweight) # # #--- second panel: temperature delta trend ----------------------------- # # #--- set plotting range # ymin = -20 ymax = 20 #plt.subplot(312) ax1 = plt.subplot(gs[1]) line = ax1.get_xticklabels() for label in line: label.set_visible(False) xlim((xmin, xmax)) ylim((ymin, ymax)) # #--- plot data # plt.plot(simtx, delta, color=colorList[0], marker=marker, markersize=3, lw=0) # #--- fit line --- use robust method # (sint, slope, serr) = robust.robust_fit(simtx, delta) lslope = '%2.3f' % (round(slope, 3)) # #--- plot fitted line # start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax], [start, stop], color=colorList[lcolor], lw=lsize) # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.1 * xdiff ydiff = ymax - ymin ypos = ymin + 0.10 * ydiff label = 'Slope: ' + str(lslope) plt.text(xpos, ypos, label, size=fsize) # #--- label axes # yname = '$\Delta$ T ($^o$C)' plt.ylabel(yname, size=fsize, weight=fweight) # #--- third panel: pitch angle trend ------------------------------- # #--- set plotting range # ymin = 0 ymax = 180 #plt.subplot(313) plt.subplot(gs[2]) xlim((xmin, xmax)) ylim((ymin, ymax)) # #--- plot data # plt.plot(stime, angle, color=colorList[4], marker='', markersize=0, lw=0.5) # #--- label axes # yname = 'Pitch Angle' plt.ylabel(yname, size=fsize, weight=fweight) # #--- label x axis # if yind == 0: xname = 'Time (year)' else: xname = 'Time (yday)' plt.xlabel(xname, size=fsize, weight=fweight) # # #--- set the size of the plotting area in inch (width: 10.0in, height 15 in) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 15.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plot_sim_temp_data(xmin, xmax, simtx, simtl, simth, mxs, mys, mxe, mye, delta, stime, angle, outname, yind, title,skip): """ plot sim trend, temp delta, and pitch angle trends Input: xmin --- starting date, either in year or yday xmax --- ending date, either in year or yday simtx --- time in either year or yday simtl --- starting sim temperature simth --- ending sim temperature mxs --- moving average time mys --- moving average of simtl mye --- moving average of simth delta --- <sim ending temp> - <sim starting temp> stime --- time in either year or yday of picthc angle data angle --- pitch angle outname --- output file name yind --- whether this is a full range plot or yearly plot title --- title of the plot skip --- if 1, skip moving averge plots Output: png plot named <outname> """ # #--- several setting for plotting # fsize = 14 #--- font size fweight = 'bold' #--- font weight psize = 3.0 #--- plotting point size marker = '.' #--- marker shape pcolor = 7 #--- color of the point lcolor = 4 #--- color of the fitted lin lsize = 3.0 #--- fitted line width resolution = 300 #--- the resolution of the plot colorList = ('blue', 'green', 'red', 'aqua', 'lime', 'fuchsia', 'maroon', 'black', 'yellow', 'olive') # #--- close everything opened before # plt.close('all') # #--- set font size # mpl.rcParams['font.size'] = fsize mpl.rcParams['font.weight'] = fweight mpl.rcParams['axes.linewidth'] = 2 #set(0,'defaultlinelinewidth',8) props = font_manager.FontProperties(size=12) plt.subplots_adjust(hspace=0.05) # #--- set grid to 3 rows or height rtio 3:1:1 # gs = gridspec.GridSpec(3,1,height_ratios=[3,1,1]) # # #--- first panel: moter temperature trend ------------------------------ # # #---- set ymin and ymax # ymin = -40 ymax = 60 # #--- set plotting range # #plt.subplot(311) ax0 = plt.subplot(gs[0]) line = ax0.get_xticklabels() for label in line: label.set_visible(False) xlim((xmin,xmax)) ylim((ymin,ymax)) # #--- plot data # plt.plot(simtx, simtl, color=colorList[0], marker=marker, markersize=psize, lw = 0) plt.plot(simtx, simth, color=colorList[2], marker=marker, markersize=psize, lw = 0) for i in range (0, len(simtx)): xset = [simtx[i], simtx[i]] yset = [simtl[i], simth[i]] plt.plot(xset, yset, color=colorList[3], marker='', markersize=0, lw = 1) # #--- plot moving average # if skip == 0: plt.plot(mxs, mys, color=colorList[0], marker='', markersize=0, lw=2) plt.plot(mxe, mye, color=colorList[2], marker='', markersize=0, lw=2) # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.05 * xdiff ydiff = ymax - ymin ypos1 = ymax - 0.08 * ydiff ypos2 = ymax - 0.12 * ydiff xpos3 = xmax - 0.20 * xdiff ypos3 = ymax + 0.005 * ydiff xpos4 = xmin + 0.05 * xdiff val1 = numpy.mean(simtl) val1 = '%2.2f' %round(val1, 2) val2 = numpy.mean(simth) val2 = '%2.2f' %round(val2, 2) label = 'T Stop Average: ' + str(val2) + ' $^o$C' plt.text(xpos, ypos1, label, size=fsize, color=colorList[2]) label = 'T Start Average: ' + str(val1) + ' $^o$C' plt.text(xpos, ypos2, label, size=fsize, color=colorList[0]) plt.text(xpos3, ypos3, title, size=fsize) plt.text(xpos4, ypos3, 'SIM TSC Moter Temperatures', size=fsize) # #--- label axes # yname = 'TSC Motor Temp ($^o$C)' plt.ylabel(yname, size=fsize, weight=fweight) # # #--- second panel: temperature delta trend ----------------------------- # # #--- set plotting range # ymin = -20 ymax = 20 #plt.subplot(312) ax1 = plt.subplot(gs[1]) line = ax1.get_xticklabels() for label in line: label.set_visible(False) xlim((xmin,xmax)) ylim((ymin,ymax)) # #--- plot data # plt.plot(simtx, delta, color=colorList[0], marker=marker, markersize=3, lw = 0) # #--- fit line --- use robust method # (sint, slope, serr) = robust.robust_fit(simtx, delta) lslope = '%2.3f' % (round(slope, 3)) # #--- plot fitted line # start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax], [start, stop], color=colorList[lcolor], lw = lsize) # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.1 * xdiff ydiff = ymax - ymin ypos = ymin + 0.10 * ydiff label = 'Slope: ' + str(lslope) plt.text(xpos, ypos, label, size=fsize) # #--- label axes # yname = '$\Delta$ T ($^o$C)' plt.ylabel(yname, size=fsize, weight=fweight) # #--- third panel: pitch angle trend ------------------------------- # #--- set plotting range # ymin = 0 ymax = 180 #plt.subplot(313) plt.subplot(gs[2]) xlim((xmin,xmax)) ylim((ymin,ymax)) # #--- plot data # plt.plot(stime, angle, color=colorList[4], marker='', markersize=0, lw = 0.5) # #--- label axes # yname = 'Pitch Angle' plt.ylabel(yname, size=fsize, weight=fweight) # #--- label x axis # if yind == 0: xname = 'Time (year)' else: xname = 'Time (yday)' plt.xlabel(xname, size=fsize, weight=fweight) # # #--- set the size of the plotting area in inch (width: 10.0in, height 15 in) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 15.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plot_data(sdata, mdata, year, msid, oname, ymin, ymax, ydrange, msid_list, lupdate): """ create two panel plots for tephin vs msid and its deviation input: sdata --- a list of tephin data mdata --- a list of msid data (mid/min/max) year --- year of the plot msid --- msid oname --- output name ymin --- y min of the first plot ymax --- y max of the first plot ydrange --- the range of the deviation y axis msid_list --- msid list lupdate --- if 1, update y plotting range. output: oname in png format """ plt.close('all') fig, ax = plt.subplots(1, figsize=(8, 6)) props = font_manager.FontProperties(size=14) mpl.rcParams['font.size'] = 14 mpl.rcParams['font.weight'] = 'bold' xmin = 260 xmax = 360 if ymax == -999: [ymin, ymax, ypos] = set_y_plot_range(mdata) # #--- since we want to all years to be in the same plotting range, this scripts adjust #--- the plotting range. you may need to run a couple of times for the full range to #--- adjust plotting range for the all plot # [ymin, ymax, ydev] = update_yrange(msid_list, msid, ymin=ymin, ymax=ymax, ydev=ydrange) else: ydiff = ymax - ymin ypos = ymax - 0.1 * ydiff ax1 = plt.subplot(211) ax1.set_xlim(xmin, xmax) ax1.set_ylim(ymin, ymax) ax1.set_xlabel("5ephin (K)") ax1.set_ylabel(msid.upper()) # #--- set the size of plot # fig.set_size_inches(10.0, 5.0) fig.tight_layout() # #---- trending plots # points = ax1.scatter(sdata, mdata, marker='o', s=4, lw=0) # #---- envelope # period = 1.0 [xmc, ymc, xmb, ymb, xmt, ymt] = create_trend_envelope(sdata, mdata, period) # #--- trending area # try: ax1.fill_between(xmc, ymb, ymt, facecolor='#00FFFF', alpha=0.3, interpolate=True) except: pass # #--- center moving average # ax1.plot(xmc, ymc, color='#E9967A', lw=4) plt.text(270, ypos, str(year)) # #---- derivative plot # [xd, yd, ad] = find_deriv(sdata, mdata, 'xxx', step=20) [dymin, dymax, dypos] = set_y_plot_range(yd) if lupdate == 1: if abs(dymin) > abs(dymax): dymax = abs(dymin) if dymax > ydrange: update_yrange(msid_list, msid, ymin=ymin, ymax=ymax, ydev=dymax) ydrange = dymax ymax = ydrange ymin = -1.0 * abs(ymax) # #--------------------------------------------------------- #---- temporarily set ymin <--> ymax value (Jan 24, 2018) # ymin = -50.0 ymax = 50.0 mc = re.search('sc', msid) if mc is not None: ymin = -300.0 ymax = 300.0 if msid in ['scp25gm', 'scp4gm', 'scp8gm']: ymin = -3000.0 ymax = 3000.0 elif msid in ['sce150', 'sce300']: ymin = -30000.0 ymax = 30000.0 #--------------------------------------------------------- ydiff = ymax - ymin ypos = ymax - 0.1 * ydiff ax2 = plt.subplot(212) ax2.set_xlim(xmin, xmax) ax2.set_ylim(ymin, ymax) ax2.set_xlabel("5ephin (K)") line = msid + '/ K' ax2.set_ylabel(line) points = ax2.scatter(xd, yd, marker='o', s=4, lw=0) if mc is not None: try: #[xs, ys] = remove_extreme_vals(xd, yd, 96) [xs, ys] = remove_extreme_vals(xd, yd, 75) [a, b, e] = rbl.robust_fit(xs, ys) except: [a, b, e] = least_sq(xd, yd, remove=1) else: try: [a, b, e] = rbl.robust_fit(xd, yd) except: [a, b, e] = least_sq(xd, yd) # #--- if b == 999, it means that it could not get the slope; so set it to 0 # if b == 999.0: a = 0 b = 0 ys = a + b * xmin ye = a + b * xmax ax2.plot([xmin, xmax], [ys, ye], color='green', lw=3) line = 'Slope: ' + "%3.3e" % (b) mpl.rcParams['font.size'] = 12 plt.title('dy / d(Temp)', loc='left') plt.text(270, ypos, line) # #--- set the size of plot # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 10.0) fig.tight_layout() plt.savefig(oname, format='png', dpi=100) plt.close('all')
def plot_step_delta(steps, delta, outname, title): """ plot a single data set on a single panel Input: steps --- translation steps delta --- sim temperature difference before and after translation outname --- the name of output file title --- title of the plot Output: png plot named <outname> """ fsize = 14 #--- font size fweight = 'bold' #--- font weight psize = 3.0 #--- plotting point size marker = '.' #--- marker shape pcolor = 7 #--- color of the point lcolor = 4 #--- color of the fitted lin lsize = 3.0 #--- fitted line width resolution = 300 #--- the resolution of the plot colorList = ('blue', 'green', 'red', 'aqua', 'lime', 'fuchsia', 'maroon', 'black', 'yellow', 'olive') # #--- close everything opened before # plt.close('all') # #--- set font size # mpl.rcParams['font.size'] = fsize mpl.rcParams['font.weight'] = fweight mpl.rcParams['axes.linewidth'] = 2 #set(0,'defaultlinelinewidth',8) props = font_manager.FontProperties(size=12) plt.subplots_adjust(hspace=0.05) [xmin, xmax, ymin, ymax ] = set_min_max(steps, delta) ymin = -5 ymax = 20 # #--- set plotting range # plt.subplot(111) xlim((xmin,xmax)) ylim((ymin,ymax)) # #--- plot data # plt.plot(steps, delta, color='fuchsia', marker='*', markersize=psize, lw = 0) # #--- fit line --- use robust method # (sint, slope, serr) = robust.robust_fit(steps, delta) lslope = '%2.3f' % (round(slope * 10000, 3)) # #--- plot fitted line # start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax], [start, stop], color=colorList[lcolor], lw = lsize) label = 'Slope: ' + str(lslope) + ' ($^o$C / 10$^4$ steps)' # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.05 * xdiff ydiff = ymax - ymin ypos = ymax - 0.10 * ydiff xpos3 = xmax - 0.20 * xdiff ypos3 = ymax + 0.005 * ydiff xpos4 = xmin + 0.05 * xdiff plt.text(xpos, ypos, label, size=fsize) # #--- label axes # yname = '$\Delta$ T ($^o$C)' plt.ylabel(yname, size=fsize, weight=fweight) # #--- label x axis # xname = 'Translation Steps' plt.xlabel(xname, size=fsize, weight=fweight) # #--- title # plt.text(xpos3, ypos3, title, size=fsize) plt.text(xpos4, ypos3, 'TSC Motor Temp vs. Move Distance', size=fsize) # # #--- set the size of the plotting area in inch (width: 10.0in, height 5 in) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 5.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)
def plot_panel(data, ylims, ylab, outname, lfit=0): """ plot sim twist and sim information trends input: data --- data: [time, dy, dz, dthera] for sim twist plots [time, sim_x, sim_y, sim_z, pithcamp, yawamp] for sim infor plot ylims --- a list of y limits [(ymin, ymax),....] ylab ----a list of ylabel outname --- output file name (without png) lfit --- the indicator to show whether to fit a line. 1: yes output: <web_dir>/Plots/<outname>.png """ # #--- data are separated into a few sections. here we set the end of the each #--- section in fractional year. the last closing data is set to year 4000 #--- see b_period for the starting date (in fractional year) # s_period = b_period r_len = len(b_period) e_period = [] for k in range(1, r_len): e_period.append(b_period[k]) e_period.append(4000.0) plt.close('all') dlen = len(ylab) mlen = dlen - 1 # #--- set panel parameters # plt.subplots_adjust(hspace=0.08) props = font_manager.FontProperties(size=9) # #--- set xrange # [atime, xmin, xmax, xlabel, xtext] = set_x_range(data[0]) xrange = [xmin, xmax] # #--- plot each panel # for k in range(0, dlen): j = k + 1 line = str(dlen) + '1' + str(j) exec("ax%s = plt.subplot(%s)" % (str(k), line)) exec("ax%s.set_xlim(left=xmin, right=xmax, auto=False)" % (str(k))) # #--- if ymin and ymax are given, use them # if len(ylims) == dlen: ymin = ylims[k][0] ymax = ylims[k][1] exec("ax%s.set_ylim(bottom=ymin, top=ymax, auto=False)" % (str(k))) ydata = data[k + 1] exec( "ax%s.plot(atime, ydata, color='blue', marker='.', markersize=1, lw=0)" % (str(k))) # #--- for the case that a fitting line is requested if lfit == 1: [a, b, e] = rfit.robust_fit(atime, ydata) y1 = a + b * xmin y2 = a + b * xmax yrange = [y1, y2] exec("ax%s.plot(xrange, yrange, color='red', lw=2)" % (str(k))) ydiff = ymax - ymin ytext = ymax - 0.1 * ydiff line = "Slope: " + '%3.3f' % (round(b, 3)) plt.text(xtext, ytext, line) elif lfit > 1: for m in range(0, r_len): [xrange, yrange, ytext, line] \ = fit_line_period(atime, ydata, b_period[m], e_period[m], m, ymin, ymax, bot=0) exec("ax%s.plot(xrange, yrange, color='red', lw=2)" % (str(k))) plt.text(xtext, ytext, line) # #--- y axis label # exec("ax%s.set_ylabel('%s')" % (str(k), ylab[k])) # #--- x axis label # exec('ax%s.set_xlabel("%s")' % (str(mlen), xlabel)) # #--- add x ticks label only on the last panel # for k in range(0, dlen): ax = 'ax' + str(k) if k != mlen: line = eval("%s.get_xticklabels()" % (ax)) for label in line: label.set_visible(False) else: pass # #--- save the plot in a file # fig = matplotlib.pyplot.gcf() height = 2.0 * dlen + 0.5 fig.set_size_inches(10.0, height) outfile = web_dir + 'Plots/' + outname plt.savefig(outfile, format='png', dpi=100.0)
def plotPanel(xmin, xmax, yMinSets, yMaxSets, xSets, ySets, xname, yname, entLabels, outname, psize=1.0): """ This function plots multiple data in separate panels Input: xmin, xmax, ymin, ymax: plotting area xSets: a list of lists containing x-axis data ySets: a list of lists containing y-axis data yMinSets: a list of ymin yMaxSets: a list of ymax entLabels: a list of the names of each data Output: a png plot: out.png """ # #--- set line color list # colorList = ('blue', 'green', 'red', 'aqua', 'lime', 'fuchsia', 'maroon', 'black', 'yellow', 'olive') # #--- clean up the plotting device # plt.close('all') # #---- set a few parameters # mpl.rcParams['font.size'] = 9 props = font_manager.FontProperties(size=9) plt.subplots_adjust(hspace=0.08) tot = len(entLabels) # #--- start plotting each data # for i in range(0, len(entLabels)): axNam = 'ax' + str(i) # #--- setting the panel position # j = i + 1 if i == 0: line = str(tot) + '1' + str(j) else: line = str(tot) + '1' + str(j) + ', sharex=ax0' line = str(tot) + '1' + str(j) exec "%s = plt.subplot(%s)" % (axNam, line) exec "%s.set_autoscale_on(False)" % (axNam) #---- these three may not be needed for the new pylab, but exec "%s.set_xbound(xmin,xmax)" % (axNam) #---- they are necessary for the older version to set exec "%s.set_xlim(xmin=xmin, xmax=xmax, auto=False)" % (axNam) exec "%s.set_ylim(ymin=yMinSets[i], ymax=yMaxSets[i], auto=False)" % (axNam) xdata = xSets[i] ydata = ySets[i] # #---- actual data plotting # p, = plt.plot(xdata, ydata, color=colorList[i], marker='.', markersize=psize, lw =0) # #---- compute fitting line # (intc, slope, berr) = robust.robust_fit(xdata, ydata) cslope = str('%.4f' % round(slope, 4)) ystart = intc + slope * xmin yend = intc + slope * xmax plt.plot([xmin, xmax], [ystart, yend], color=(colorList[i+2]), lw=1) # #--- add legend # tline = entLabels[i] + ' Slope: ' + cslope leg = legend([p], [tline], prop=props, loc=2) leg.get_frame().set_alpha(0.5) exec "%s.set_ylabel(yname, size=8)" % (axNam) # #--- add x ticks label only on the last panel # for i in range(0, tot): ax = 'ax' + str(i) if i != tot-1: exec "line = %s.get_xticklabels()" % (ax) for label in line: label.set_visible(False) else: pass xlabel(xname) # #--- set the size of the plotting area in inch (width: 10.0in, height 2.08in x number of panels) # fig = matplotlib.pyplot.gcf() height = (2.00 + 0.08) * tot fig.set_size_inches(10.0, height) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=100)
def fit_line_period(x, y, x_start, x_stop, m, ymin, ymax, bot=0): """ fit a line on the given data section and return the result input: x --- x data y --- y data x_start --- a list of the starting points x_stop --- a list of the stopping points m --- section position of the lists above ymin --- y min yax --- y max bot --- an indicator of where to print the text. bot =1 to bottom side output: xrange --- a list of start and stop position of the line in x yrange --- a list of start and stop position of the line in y ytext --- y position of the text line --- a line to be printed """ dlen = len(x) xn = [] yn = [] # #--- select data for the limited range # for k in range(0, dlen): if x[k] >= x_start and x[k] < x_stop: if y[k] >= ymin and y[k] <= ymax: xn.append(x[k]) yn.append(y[k]) try: # #--- compute the fitted line with robust method # [a, b, e] = rfit.robust_fit(list(xn), list(yn)) y1 = a + b * x_start y2 = a + b * x_stop xrange = [x_start, x_stop] yrange = [y1, y2] # #--- set the text postion and create the text to be printed # ydiff = ymax - ymin if bot == 0: ytext = ymax - 0.1 * ydiff * (m + 1) else: ytext = ymax - 0.1 * ydiff * (m + 1) - 0.5 * ydiff if x_start < 2000: line = "Slope (%4.1f < year): %3.3f" % (x_stop, round(b, 3)) elif x_stop > 3000: line = "Slope (year > %4.1f): %3.3f" % (x_start, round(b, 3)) else: line = "Slope (%4.1f < year < %4.1f): %3.3f" % (x_start, x_stop, round(b, 3)) # #--- for the case the fitting failed # except: ydiff = ymax - ymin if bot == 0: ytext = ymax - 0.1 * ydiff * (m + 1) else: ytext = ymax - 0.1 * ydiff * (m + 1) - 0.5 * ydiff xrange = [0, 0] yrange = [0, 0] line = 'NA' return [xrange, yrange, ytext, line]
def plot_single_panel(xmin, xmax, ymin, ymax, xdata, ydata, yerror, xname, yname, label, outname, resolution=100): """ plot a single data set on a single panel Input: xmin --- min x xmax --- max x ymin --- min y ymax --- max y xdata --- independent variable ydata --- dependent variable yerror --- error in y axis xname --- x axis label ynane --- y axis label label --- a text to indecate what is plotted outname --- the name of output file resolution-- the resolution of the plot in dpi Output: png plot named <outname> """ # #--- fit line --- use robust method # (sint, slope, serr) = robust.robust_fit(xdata, ydata) lslope = '%2.3f' % (round(slope, 3)) # #--- close everything opened before # plt.close('all') # #--- set font size # mpl.rcParams['font.size'] = 12 props = font_manager.FontProperties(size=9) # #--- set plotting range # ax = plt.subplot(111) ax.set_autoscale_on(False) ax.set_xbound(xmin,xmax) ax.set_xlim(xmin=xmin, xmax=xmax, auto=False) ax.set_ylim(ymin=ymin, ymax=ymax, auto=False) # #--- plot data # plt.plot(xdata, ydata, color='blue', marker='o', markersize=4.0, lw =0) # #--- plot error bar # plt.errorbar(xdata, ydata, yerr=yerror, lw = 0, elinewidth=1) # #--- plot fitted line # start = sint + slope * xmin stop = sint + slope * xmax plt.plot([xmin, xmax], [start, stop], color='red', lw = 2) # #--- label axes # plt.xlabel(xname) plt.ylabel(yname) # #--- add what is plotted on this plot # xdiff = xmax - xmin xpos = xmin + 0.5 * xdiff ydiff = ymax - ymin ypos = ymax - 0.08 * ydiff label = label + ': Slope: ' + str(lslope) plt.text(xpos, ypos, label) # #--- set the size of the plotting area in inch (width: 10.0in, height 2.08in x number of panels) # fig = matplotlib.pyplot.gcf() fig.set_size_inches(10.0, 5.0) # #--- save the plot in png format # plt.savefig(outname, format='png', dpi=resolution)