def drawHeatmap(df, **kwargs): try: import seaborn as sns heatmap = sns.heatmap(df, annot=kwargs.get('annot', True), annot_kws={'family': 'Times New Roman', 'weight': 'normal', 'size': kwargs.get('annot_size', 12)}, vmin=kwargs.get("vmin"), vmax=kwargs.get("vmax"), square=kwargs.get("square", True), linewidths=kwargs.get("linewidths", 0.05), cmap=kwargs.get('cmap', 'YlGnBu'), xticklabels=kwargs.get("xticklabels", True), cbar=kwargs.get('cbar', False), fmt='.{0}g'.format(kwargs.get("annotDigit", 3)), ) if not kwargs.get('cbar', False): cb = heatmap.figure.colorbar(heatmap.collections[0]) # 显示colorbar cb.ax.tick_params(labelsize=kwargs.get("labelsize", 12)) # 设置colorbar刻度字体大小。 if kwargs.get("cbar_label"): font = {'family': 'Times New Roman', 'weight': 'normal', 'size': kwargs.get("cbar_label_font", 16), } cb.set_label(kwargs.get("cbar_label"), fontdict=font) except Exception as errMsg: raise BusinessException(EmBusinessError.FIGURE_AXES_FAIL, errMsg)
def drawArrow(cls, start, end, **kwargs): """ 箭头起始位置(A[0],A[1])和向量(B[0],B[1]) length_includes_head = True:表示增加的长度包含箭头部分 head_width:箭头的宽度 head_length:箭头的长度 fc:filling color(箭头填充的颜色) ec:edge color(边框颜色) arrowtype: "simple", "->" """ try: from matplotlib.pyplot import gca arrowstyle = '{2},head_width={0},head_length={1}'.format(kwargs.get('head_width', 0.75), kwargs.get('head_length', 0.75), kwargs.get("arrow_type", "->")) if kwargs.get("tail_width"): arrowstyle += ",tail_width={0}".format(kwargs.get("tail_width", 0.4)) opt = dict(color=kwargs.get('color', 'red'), alpha=kwargs.get("alpha"), arrowstyle=arrowstyle, connectionstyle='arc3,rad=0', fc=kwargs.get("fc")) arrow = gca().annotate(kwargs.get("annotate", ""), xy=end, xycoords='data', xytext=start, textcoords='data', arrowprops=opt, ) cls.handles.append(arrow) return arrow except Exception as errMsg: raise BusinessException(EmBusinessError.FIGURE_AXES_FAIL, errMsg)
def figureShow(cls): try: plt.tight_layout() if cls.DEBUG: cls.fig.show() plt.pause(3) plt.close() except Exception as errMsg: raise BusinessException(EmBusinessError.FIGURE_POSTPROCESSING_FAIL, errMsg)
def figureSave(cls, **kwargs): try: import os save_name = kwargs.get("saveName") save_path = kwargs.get('savePath', PATH.get("out/pdf")) cls.fig.savefig( os.path.join(save_path, '{0}.pdf'.format(save_name) if kwargs.get("saveAsPDF", True) else save_name), dpi=500, bbox_inches='tight') except Exception as errMsg: raise BusinessException(EmBusinessError.FIGURE_POSTPROCESSING_FAIL, errMsg)
def setLim(cls, **kwargs): # 设置图像有效范围(lim) try: if kwargs.get("xlim"): cls.axes.set_xlim(kwargs.get("xlim")) if kwargs.get("ylim"): cls.axes.set_ylim(kwargs.get("ylim")) if kwargs.get("zlim"): cls.axes.set_zlim(kwargs.get("zlim")) except Exception as errMsg: raise BusinessException(EmBusinessError.NETWORK_CUSTOM_METRIC_ERROR, errMsg)
def drawBar(cls, x, y, **kwargs): try: bar = cls.axes.bar(x, y, width=kwargs.get("width", 0.8), color=kwargs.get("color"), facecolor=kwargs.get("facecolor"), edgecolor=kwargs.get("edgecolor"), alpha=kwargs.get("alpha", 0.5), label=kwargs.get("label")) cls.handles.append(bar) return bar except Exception as errMsg: raise BusinessException(EmBusinessError.FIGURE_AXES_FAIL, errMsg)
def drawScatter(cls, x, y, **kwargs): try: scatter = cls.axes.scatter(x, y, c=kwargs.get("color"), s=kwargs.get("size"), alpha=kwargs.get("alpha"), label=kwargs.get("label"), marker=kwargs.get('marker'), ) cls.handles.append(scatter) return scatter except Exception as errMsg: raise BusinessException(EmBusinessError.FIGURE_AXES_FAIL, errMsg)
def drawLine(cls, x, y, **kwargs): try: line, = cls.axes.plot(x, y, c=kwargs.get('color'), marker=kwargs.get('marker'), markersize=kwargs.get('markersize', 4), markerfacecolor=kwargs.get('markerfacecolor'), alpha=kwargs.get('alpha', 1), linewidth=kwargs.get('linewidth', 2), linestyle=kwargs.get("linestyle", 'dashed'), label=kwargs.get('label'), ) cls.handles.append(line) return line except Exception as errMsg: raise BusinessException(EmBusinessError.FIGURE_AXES_FAIL, errMsg)
def drawHist(cls, x, bins, **kwargs): try: n, bins, patches = cls.axes.hist( x=x, bins=bins, range=kwargs.get("range"), density=kwargs.get("density", False), histtype=kwargs.get("histtype", "bar"), cumulative=kwargs.get("cumulative"), label=kwargs.get("label"), edgecolor=kwargs.get("edgecolor", "black"), color=kwargs.get("color", "blue"), alpha=kwargs.get("alpah", 0.4) ) # cls.handles.append((n, bins, patches)) return n, bins, patches except Exception as errMsg: raise BusinessException(EmBusinessError.FIGURE_AXES_FAIL, errMsg)
def drawBoxplot(cls, x, **kwargs): try: bp = cls.axes.boxplot( x, labels=kwargs.get("labels"), showmeans=kwargs.get("showmeans"), meanline=kwargs.get("meanline"), showbox=kwargs.get("showbox", True), showcaps=kwargs.get("showcaps", True), notch=kwargs.get("notch"), bootstrap=kwargs.get("bootstrap"), showfliers=kwargs.get("showfliers", True), boxprops=kwargs.get("boxprops"), flierprops=kwargs.get("flierprops"), medianprops=kwargs.get("medianprops"), meanprops=kwargs.get("meanpointprops", kwargs.get("meanlineprops")), ) cls.handles.append(bp) return bp except Exception as errMsg: raise BusinessException(EmBusinessError.FIGURE_AXES_FAIL, errMsg)