def add_year_xaxis(ax, xlim_min, xlim_max): ''' add year xaxis ''' if xlim_min.year == xlim_max.year: ax.set_xlabel(xlim_min.year, fontsize=11, fontproperties=FONT0) return newax = ax.twiny() newax.set_frame_on(True) newax.grid(False) newax.patch.set_visible(False) newax.xaxis.set_ticks_position('bottom') newax.xaxis.set_label_position('bottom') newax.set_xlim(xlim_min, xlim_max) newax.xaxis.set_major_locator(mdates.YearLocator()) newax.xaxis.set_major_formatter(mdates.DateFormatter('%Y')) newax.spines['bottom'].set_position(('outward', 20)) newax.spines['bottom'].set_linewidth(0.6) newax.tick_params(which='both', direction='in') set_tick_font(newax) newax.xaxis.set_tick_params(length=5)
def draw_histogram(filename, dvalues, titledict=None, tl_list=None, tr_list=None, bins=200, ranges=None, label='Hist'): """ 画直方图 """ plt.style.use(os.path.join(dvPath, 'dv_pub_regression.mplstyle')) alpha = 1 fig = plt.figure(figsize=(6, 4)) fig.subplots_adjust(top=0.92, bottom=0.13, left=0.11, right=0.96) ax = plt.gca() ax.grid(True) ax.hist(dvalues, bins, histtype='bar', color='blue', label=label, alpha=alpha) ax.legend(prop={'size': 10}) add_annotate(ax, tl_list, 'left') add_annotate(ax, tr_list, 'right') add_title(titledict) set_tick_font(ax) plt.savefig(filename) fig.clear() plt.close()
def plot_omb(date_D, a_D, b_D, picPath, title, date_s, date_e): ''' 画偏差时序彩色图 ''' if (np.isnan(a_D)).all(): Log.error('Everything is NaN: %s' % picPath) return ylim_min, ylim_max = 210, 330 y_res = 0.2 x_size = (date_e - date_s).days yy = np.arange(ylim_min, ylim_max, y_res) + y_res / 2. # 一列的y值 grid = np.ones(len(date_D)) * yy.reshape(-1, 1) aa = a_D * np.ones((len(grid), 1)) bb = b_D * np.ones((len(grid), 1)) grid = grid - np.divide((grid - bb), aa) zz = np.zeros((len(yy), x_size)) # 2D, 要画的值 j = 0 xx = [] # 一行的x值 for i in xrange(x_size): # 补充缺失数据的天 date_i = date_s + relativedelta(days=i) xx.append(date_i) if j < len(date_D) and date_D[j] == date_i: zz[:, i] = grid[:, j] j = j + 1 fig = plt.figure(figsize=(6, 4)) ax = fig.add_subplot(111) norm = mpl.colors.Normalize(vmin=-10.0, vmax=10.0) xx = np.array(xx) plt.pcolormesh(xx, yy, zz, cmap='jet', norm=norm, shading='gouraud', zorder=0) plt.grid(True, zorder=10) xlim_min = date_s xlim_max = date_e plt.xlim(xlim_min, xlim_max) plt.ylim(ylim_min, ylim_max) plt.ylabel('TB($K$)', fontsize=11, fontproperties=FONT0) # format the ticks setXLocator(ax, xlim_min, xlim_max) set_tick_font(ax) # title plt.title(title, fontsize=12, fontproperties=FONT0) plt.tight_layout() #-------------------- fig.subplots_adjust(bottom=0.25) # -------add colorbar --------- fig.canvas.draw() point_bl = ax.get_position().get_points()[0] # 左下 point_tr = ax.get_position().get_points()[1] # 右上 cbar_height = 0.05 colorbar_ax = fig.add_axes([ point_bl[0] - 0.05, 0.05, (point_tr[0] - point_bl[0]) / 2.2, cbar_height ]) mpl.colorbar.ColorbarBase(colorbar_ax, cmap='jet', norm=norm, orientation='horizontal') # ---font of colorbar----------- for l in colorbar_ax.xaxis.get_ticklabels(): l.set_fontproperties(FONT0) l.set_fontsize(9) # ------Time and ORG_NAME---------------- ymd_s, ymd_e = date_s.strftime('%Y%m%d'), date_e.strftime('%Y%m%d') if ymd_s != ymd_e: fig.text(0.52, 0.05, '%s-%s' % (ymd_s, ymd_e), fontproperties=FONT0) else: fig.text(0.52, 0.05, '%s' % ymd_s, fontproperties=FONT0) fig.text(0.82, 0.05, ORG_NAME, fontproperties=FONT0) #--------------- pb_io.make_sure_path_exists(os.path.dirname(picPath)) plt.savefig(picPath) fig.clear() plt.close()
def plot_abc(date_D, a_D, b_D, c_D, date_M, a_M, b_M, c_M, picPath, title, date_s, date_e, var): fig = plt.figure(figsize=(6, 6)) ax1 = plt.subplot(311) ax2 = plt.subplot(312, sharex=ax1) ax3 = plt.subplot(313, sharex=ax1) # format the Xticks xlim_min = pb_time.ymd2date('%04d%02d01' % (date_s.year, date_s.month)) xlim_max = date_e ax1.set_xlim(xlim_min, xlim_max) # format the Yticks\ if var == "tbb-tbb" or var == "ref-ref": ax1.set_ylim(0, 2) ax1.yaxis.set_major_locator(MultipleLocator(0.5)) ax1.yaxis.set_minor_locator(MultipleLocator(0.1)) elif var == "dn-ref": ax1.set_ylim(0.0002, 0.0007) ax1.yaxis.set_major_locator(MultipleLocator(0.0001)) ax1.yaxis.set_minor_locator(MultipleLocator(0.00005)) if var == "tbb-tbb": ax2.set_ylim(-30, 30) ax2.yaxis.set_major_locator(MultipleLocator(10)) ax2.yaxis.set_minor_locator(MultipleLocator(2)) elif var == "ref-ref": ax2.set_ylim(-0.1, 0.1) ax2.yaxis.set_major_locator(MultipleLocator(0.02)) ax2.yaxis.set_minor_locator(MultipleLocator(0.01)) elif var == "dn-ref": ax2.set_ylim(-0.08, 0.08) ax2.yaxis.set_major_locator(MultipleLocator(0.02)) ax2.yaxis.set_minor_locator(MultipleLocator(0.01)) ax3.set_ylim(0, 7) ax3.yaxis.set_major_locator(MultipleLocator(1)) ax3.yaxis.set_minor_locator(MultipleLocator(0.2)) # plot ax1 ------------------------------------------------- plt.sca(ax1) plt.plot(date_D, a_D, 'x', ms=5, markerfacecolor=None, markeredgecolor=BLUE, alpha=0.8, mew=0.3, label='Daily') plt.plot(date_M, a_M, 'o-', ms=4, lw=0.6, c=RED, mew=0, label='Monthly') plt.ylabel('Slope', fontsize=11, fontproperties=FONT0) plt.grid(True) plt.title(title, fontsize=12, fontproperties=FONT0) set_tick_font(ax1) plt.setp(ax1.get_xticklabels(), visible=False) # plot ax2 ------------------------------------------------- plt.sca(ax2) plt.plot(date_D, b_D, 'x', ms=5, markerfacecolor=None, markeredgecolor=BLUE, alpha=0.8, mew=0.3, label='Daily') plt.plot(date_M, b_M, 'o-', ms=4, lw=0.6, c=RED, mew=0, label='Monthly') plt.ylabel('Intercept', fontsize=11, fontproperties=FONT0) plt.grid(True) set_tick_font(ax2) plt.setp(ax2.get_xticklabels(), visible=False) # point number ------------------------------------------------- plt.sca(ax3) plt.fill_between(date_D, 0, c_D, edgecolor=BLUE, facecolor=BLUE, alpha=0.6) # plt.fill_between(date_M, 0, c_M, # edgecolor=RED, facecolor=RED, alpha=0.5) # plt.bar(date_M, c_M, width=1, align='edge', # "center", # color=RED, linewidth=0) plt.plot(date_M, c_M, 'o-', ms=4, lw=0.6, c=RED, mew=0, label='Monthly') plt.ylabel('Number of sample points\nlog (base = 10)', fontsize=11, fontproperties=FONT0) plt.grid(True) set_tick_font(ax3) setXLocator(ax3, xlim_min, xlim_max) # circle1 = mpatches.Circle((430, 563), 5, color=BLUE, ec=EDGE_GRAY, lw=0) # circle2 = mpatches.Circle((508, 563), 5, color=RED, ec=EDGE_GRAY, lw=0) # fig.patches.extend([circle1, circle2]) # # fig.text(0.74, 0.93, 'Daily', color=BLUE, fontproperties=FONT0) # fig.text(0.86, 0.93, 'Monthly', color=RED, fontproperties=FONT0) #--------------- plt.tight_layout() fig.subplots_adjust(bottom=0.14) circle1 = mpatches.Circle((74, 18), 6, color=BLUE, ec=EDGE_GRAY, lw=0) circle2 = mpatches.Circle((164, 18), 6, color=RED, ec=EDGE_GRAY, lw=0) fig.patches.extend([circle1, circle2]) fig.text(0.15, 0.02, 'Daily', color=BLUE, fontproperties=FONT0) fig.text(0.3, 0.02, 'Monthly', color=RED, fontproperties=FONT0) ymd_s, ymd_e = date_s.strftime('%Y%m%d'), date_e.strftime('%Y%m%d') if ymd_s != ymd_e: fig.text(0.50, 0.02, '%s-%s' % (ymd_s, ymd_e), fontproperties=FONT0) else: fig.text(0.50, 0.02, '%s' % ymd_s, fontproperties=FONT0) fig.text(0.8, 0.02, ORG_NAME, fontproperties=FONT0) #--------------- pb_io.make_sure_path_exists(os.path.dirname(picPath)) fig.savefig(picPath) plt.close() fig.clear
def plot_tbbias(date_D, bias_D, date_M, bias_M, picPath, title, date_s, date_e, satName): ''' 画偏差时序折线图 ''' fig = plt.figure(figsize=(6, 4)) # plt.subplots_adjust(left=0.13, right=0.95, bottom=0.11, top=0.97) if (np.isnan(bias_D)).all(): Log.error('Everything is NaN: %s' % picPath) return plt.plot(date_D, bias_D, 'x', ms=6, markerfacecolor=None, markeredgecolor=BLUE, alpha=0.8, mew=0.3, label='Daily') plt.plot(date_M, bias_M, 'o-', ms=5, lw=0.6, c=RED, mew=0, label='Monthly') plt.grid(True) plt.ylabel('DTB($K$)', fontsize=11, fontproperties=FONT0) xlim_min = pb_time.ymd2date('%04d%02d01' % (date_s.year, date_s.month)) xlim_max = date_e plt.xlim(xlim_min, xlim_max) if "FY2" in satName: plt.ylim(-8, 8) elif "FY3" in satName: plt.ylim(-8, 8) elif "FY4" in satName: plt.ylim(-2, 2) ax = plt.gca() # format the ticks setXLocator(ax, xlim_min, xlim_max) set_tick_font(ax) # title plt.title(title, fontsize=12, fontproperties=FONT0) plt.tight_layout() #-------------------- fig.subplots_adjust(bottom=0.2) circle1 = mpatches.Circle((74, 15), 6, color=BLUE, ec=EDGE_GRAY, lw=0) circle2 = mpatches.Circle((164, 15), 6, color=RED, ec=EDGE_GRAY, lw=0) fig.patches.extend([circle1, circle2]) fig.text(0.15, 0.02, 'Daily', color=BLUE, fontproperties=FONT0) fig.text(0.3, 0.02, 'Monthly', color=RED, fontproperties=FONT0) ymd_s, ymd_e = date_s.strftime('%Y%m%d'), date_e.strftime('%Y%m%d') if ymd_s != ymd_e: fig.text(0.50, 0.02, '%s-%s' % (ymd_s, ymd_e), fontproperties=FONT0) else: fig.text(0.50, 0.02, '%s' % ymd_s, fontproperties=FONT0) fig.text(0.8, 0.02, ORG_NAME, fontproperties=FONT0) #--------------- pb_io.make_sure_path_exists(os.path.dirname(picPath)) plt.savefig(picPath) fig.clear() plt.close()
def plot(x, y, picPath, part1, part2, chan, ym, DayOrNight, reference_list, xname, xname_l, xunit, xmin, xmax): ''' x: 参考卫星传感器数据 y: FY数据 ''' if xname_l == "TBB": xname_l = "TB" xlim_min = xmin xlim_max = xmax delta = y - x if xname == "tbb": step = 5 else: step = 0.1 T_seg, mean_seg, std_seg, sampleNums = get_bar_data( x, delta, xlim_min, xlim_max, step) RadCompare = G_reg1d(x, y) a, b = RadCompare[0], RadCompare[1] fig = plt.figure(figsize=(6, 5)) ax1 = plt.subplot(211) ax2 = plt.subplot(212, sharex=ax1) # format the Xticks ax1.set_xlim(xlim_min, xlim_max) # format the Yticks if xname == "tbb": ax1.set_ylim(-10, 10) ax1.yaxis.set_major_locator(MultipleLocator(5)) ax1.yaxis.set_minor_locator(MultipleLocator(1)) elif xname == "ref": ax1.set_ylim(-0.3, 0.3) ax1.yaxis.set_major_locator(MultipleLocator(0.1)) ax1.yaxis.set_minor_locator(MultipleLocator(0.02)) ax2.set_ylim(0, 7) ax2.yaxis.set_major_locator(MultipleLocator(1)) ax2.yaxis.set_minor_locator(MultipleLocator(0.2)) title = '%s Bias Monthly Statistics\n%s Minus %s %s %s' % \ (xname_l, part2, part1, chan, DayOrNight) # plot ax1 ------------------------------------------------- plt.sca(ax1) strlist = [[]] for ref_temp in reference_list: plt.axvline(x=ref_temp, color='#4cd964', lw=0.7) ax1.annotate(str(ref_temp) + xunit, (ref_temp, -7.5), va="top", ha="center", color=EDGE_GRAY, size=6, fontproperties=FONT_MONO) strlist[0].append( "%s Bias %s: %6.3f" % (xname_l, str(ref_temp) + xunit, ref_temp * a + b - ref_temp)) strlist[0].append('Total Number: %7d' % len(x)) plt.plot(x, delta, 'o', ms=1.5, markerfacecolor=BLUE, alpha=0.5, mew=0, zorder=10) plt.plot(T_seg, mean_seg, 'o-', ms=6, lw=0.6, c=RED, mew=0, zorder=50) plt.fill_between(T_seg, mean_seg - std_seg, mean_seg + std_seg, facecolor=RED, edgecolor=RED, interpolate=True, alpha=0.4, zorder=100) ylabel = 'D%s' % (xname_l) + ('($%s$)' % xunit if xunit != "" else "") plt.ylabel(ylabel, fontsize=11, fontproperties=FONT0) plt.grid(True) plt.title(title, fontsize=12, fontproperties=FONT0) set_tick_font(ax1) plt.setp(ax1.get_xticklabels(), visible=False) # point number ------------------------------------------------- plt.sca(ax2) if xname == "tbb": width = 3 elif xname == "ref": width = 0.07 plt.bar(T_seg, np.log10(sampleNums), width=width, align="center", color=BLUE, linewidth=0) for i, T in enumerate(T_seg): if sampleNums[i] > 0: plt.text(T, np.log10(sampleNums[i]) + 0.2, '%d' % int(sampleNums[i]), ha="center", fontsize=6, fontproperties=FONT_MONO) add_annotate(ax2, strlist, 'left', EDGE_GRAY, 9) plt.ylabel('Number of sample points\nlog (base = 10)', fontsize=11, fontproperties=FONT0) xlabel = '%s %s' % (part2, xname_l) + ('($%s$)' % xunit if xunit != "" else "") plt.xlabel(xlabel, fontsize=11, fontproperties=FONT0) plt.grid(True) set_tick_font(ax2) #--------------- plt.tight_layout() fig.subplots_adjust(bottom=0.16) # circle1 = mpatches.Circle((74, 18), 5, color=BLUE, ec=EDGE_GRAY, lw=0) # circle2 = mpatches.Circle((164, 18), 5, color=RED, ec=EDGE_GRAY, lw=0) # fig.patches.extend([circle1, circle2]) # # fig.text(0.15, 0.02, 'Daily', color=BLUE, fontproperties=FONT0) # fig.text(0.3, 0.02, 'Monthly', color=RED, fontproperties=FONT0) fig.text(0.6, 0.02, '%s' % ym, fontproperties=FONT0) fig.text(0.8, 0.02, ORG_NAME, fontproperties=FONT0) #--------------- pb_io.make_sure_path_exists(os.path.dirname(picPath)) fig.savefig(picPath) plt.close() fig.clear
def plot_tbbias(date_D, bias_D, date_M, bias_M, picPath, title, date_s, date_e): """ 画偏差时序折线图 """ plt.style.use(os.path.join(DV_PATH, 'dv_pub_timeseries.mplstyle')) fig = plt.figure(figsize=(6, 4)) # plt.subplots_adjust(left=0.13, right=0.95, bottom=0.11, top=0.97) if (np.isnan(bias_D)).all(): Log.error('Everything is NaN: %s' % picPath) return plt.plot(date_D, bias_D, 'x', ms=6, markerfacecolor=None, markeredgecolor=BLUE, alpha=0.8, mew=0.3, label='Daily') plt.plot(date_M, bias_M, 'o-', ms=5, lw=0.6, c=RED, mew=0, label='Monthly') plt.grid(True) plt.ylabel('DTB($K$)', fontsize=11, fontproperties=FONT0) xlim_min = date_s xlim_max = date_e plt.xlim(xlim_min, xlim_max) plt.ylim(-1, 1) # 画 y=0 线 plt.plot([xlim_min, xlim_max], [0, 0], color='#808080', linewidth=1.0) ax = plt.gca() # format the ticks setXLocator(ax, xlim_min, xlim_max) set_tick_font(ax) ax.yaxis.set_major_locator(MultipleLocator(0.25)) ax.yaxis.set_minor_locator(MultipleLocator(0.125)) # title plt.title(title, fontsize=12, fontproperties=FONT0) plt.tight_layout() #-------------------- fig.subplots_adjust(bottom=0.2) circle1 = mpatches.Circle((74, 15), 6, color=BLUE, ec=EDGE_GRAY, lw=0) circle2 = mpatches.Circle((164, 15), 6, color=RED, ec=EDGE_GRAY, lw=0) fig.patches.extend([circle1, circle2]) fig.text(0.15, 0.02, 'Daily', color=BLUE, fontproperties=FONT0) fig.text(0.3, 0.02, 'Monthly', color=RED, fontproperties=FONT0) ymd_s, ymd_e = date_s.strftime('%Y%m%d'), date_e.strftime('%Y%m%d') if ymd_s != ymd_e: fig.text(0.50, 0.02, '%s-%s' % (ymd_s, ymd_e), fontproperties=FONT0) else: fig.text(0.50, 0.02, '%s' % ymd_s, fontproperties=FONT0) fig.text(0.8, 0.02, ORG_NAME, fontproperties=FONT0) #--------------- pb_io.make_sure_path_exists(os.path.dirname(picPath)) plt.savefig(picPath) fig.clear() plt.close()
def plot_bias(date_D, bias_D, date_M, bias_M, picPath, title, date_s, date_e, each, date_type, ylim_min, ylim_max): """ 画偏差时序折线图 """ plt.style.use(os.path.join(dvPath, 'dv_pub_timeseries.mplstyle')) fig = plt.figure(figsize=(6, 4)) # plt.subplots_adjust(left=0.13, right=0.95, bottom=0.11, top=0.97) plt.plot(date_D, bias_D, 'x', ms=6, markerfacecolor=None, markeredgecolor=BLUE, alpha=0.8, mew=0.3, label='Daily') plt.plot(date_M, bias_M, 'o-', ms=5, lw=0.6, c=RED, mew=0, label='Monthly') plt.grid(True) plt.ylabel('%s %s' % (each, date_type), fontsize=11, fontproperties=FONT0) xlim_min = pb_time.ymd2date('%04d%02d01' % (date_s.year, date_s.month)) xlim_max = date_e plt.plot([xlim_min, xlim_max], [0, 0], '#808080') # 在 y=0 绘制一条深灰色直线 plt.xlim(xlim_min, xlim_max) plt.ylim(ylim_min, ylim_max) ax = plt.gca() # format the ticks interval = (ylim_max - ylim_min) / 8 # 8 个间隔 minibar = interval / 2. setXLocator(ax, xlim_min, xlim_max) set_tick_font(ax) # 如果范围为浮点数,需要进行一次格式化,否则图像不会显示最后一个刻度 if isinstance(interval, float): interval = float('%.5f' % interval) minibar = float('%.5f' % minibar) ax.yaxis.set_major_locator(MultipleLocator(interval)) ax.yaxis.set_minor_locator(MultipleLocator(minibar)) # title plt.title(title, fontsize=12, fontproperties=FONT0) plt.tight_layout() # -------------------- fig.subplots_adjust(bottom=0.2) circle1 = mpatches.Circle((74, 15), 6, color=BLUE, ec=EDGE_GRAY, lw=0) circle2 = mpatches.Circle((164, 15), 6, color=RED, ec=EDGE_GRAY, lw=0) fig.patches.extend([circle1, circle2]) fig.text(0.15, 0.02, 'Daily', color=BLUE, fontproperties=FONT0) fig.text(0.3, 0.02, 'Monthly', color=RED, fontproperties=FONT0) ymd_s, ymd_e = date_s.strftime('%Y%m%d'), date_e.strftime('%Y%m%d') if ymd_s != ymd_e: fig.text(0.50, 0.02, '%s-%s' % (ymd_s, ymd_e), fontproperties=FONT0) else: fig.text(0.50, 0.02, '%s' % ymd_s, fontproperties=FONT0) fig.text(0.8, 0.02, ORG_NAME, fontproperties=FONT0) # --------------- pb_io.make_sure_path_exists(os.path.dirname(picPath)) plt.savefig(picPath) fig.clear() plt.close()