def plot_cal_gender(target_data_m, gender): ''' :param: target_data_m :type: pd.Dataframe create pie plot show CA death rate at each gender ''' assert isinstance(target_data_m, pd.DataFrame) assert isinstance(gender, str) labels = list(target_data_m.name) sizes = list(target_data_m.d) cs = cm.Set3(np.arange(len(sizes)) / len(sizes)) explode = np.ones((len(sizes))) explode *= 0.1 explode[5] = 0 # Plot f, ax = plt.subplots(figsize=(20, 10)) plt.pie(sizes, explode=explode, labels=labels, colors=cs, pctdistance=0.8, autopct='%1.1f%%', shadow=True, startangle=150, textprops={'fontsize': 18}) ax.set_title(f'Top 10 Death Causes ({gender}) in California, 2017', fontsize=22) ax.title.set_position([.5, 1.05]) plt.axis('equal') plt.show()
def model_dictionary(): models = ['ACCESS1-0', 'ACCESS1-3', 'BNU-ESM','CCSM4', 'CESM1-BGC',\ 'CESM1-CAM5', 'CESM1-FASTCHEM', 'CESM1-WACCM', 'CMCC-CESM',\ 'CMCC-CM','CMCC-CMS', 'CNRM-CM5', 'CNRM-CM5','CNRM-CM5-2', 'CSIRO-Mk3-6-0', 'CanESM2',\ 'FGOALS-g2', 'FGOALS-s2', 'FIO-ESM', 'GFDL-CM3', 'GFDL-ESM2G',\ 'GFDL-ESM2M', 'GFDL-HIRAM-C180','GFDL-HIRAM-C360','GISS-E2-H*p1', 'GISS-E2-H*p3', 'GISS-E2-H-CC',\ 'GISS-E2-R*p1', 'GISS-E2-R*p3', 'GISS-E2-R-CC', 'HadCM3',\ 'HadGEM2-AO', 'HadGEM2-CC', 'HadGEM2-ES', 'IPSL-CM5A-LR',\ 'IPSL-CM5A-MR', 'IPSL-CM5B-LR', 'MIROC-ESM', 'MIROC-ESM-CHEM',\ 'MIROC4h', 'MIROC5', 'MPI-ESM-LR', 'MPI-ESM-MR', 'MPI-ESM-P','MRI-CGCM3',\ 'NorESM1-M', 'NorESM1-ME', 'bcc-csm1-1', 'bcc-csm1-1-m', 'fio-esm',\ 'inmcm4'] markers =["o","v","^","<",">","8","s","p","*","h","H","D","d"]#,"P","X"] Lm = len(markers) d={} i=0 colors = [cm.Set1(i/9.) for i in range(9)]+[cm.Set2(i/9.) for i in range(9)]+[cm.Set3(i/9.) for i in range(9)] Lc = len(colors) for i in range(len(models)): model =models[i] d[model]= {} d[model]["color"]=colors[np.mod(i,Lc)] d[model]["marker"]=markers[np.mod(i,Lm)] d["Can*"]=d["CanESM2"] d["CanAM4"]=d["CanESM2"] d["HadGEM2-A*"]=d["HadGEM2-AO"] d["HadGEM2-A"]=d["HadGEM2-AO"] return d
def pieChart(): f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18,6)) labels1= list(OrderedDict(sorted(Counter(global_data.attacktype1_txt).items(), key=lambda x: x[1], reverse=True)).keys()) sizes1 = list(OrderedDict(sorted(Counter(global_data.attacktype1_txt).items(), key=lambda x: x[1], reverse=True)).values()) patches1, texts1 = ax1.pie(sizes1, colors=cm.Set3(np.linspace(0, 1, len(sizes1))), startangle=90) ax1.legend(patches1, labels1, loc="right", fontsize=14) ax1.set_title('Global', fontsize=15, fontweight='bold') labels2= list(OrderedDict(sorted(Counter(us_internal_data.attacktype1_txt).items(), key=lambda x: x[1], reverse=True)).keys()) sizes2 = list(OrderedDict(sorted(Counter(us_internal_data.attacktype1_txt).items(), key=lambda x: x[1], reverse=True)).values()) patches2, texts2 = ax2.pie(sizes2, colors=cm.Set3(np.linspace(0, 1, len(sizes2))), startangle=90) ax2.legend(patches2, labels2, loc="right", fontsize=14) ax2.set_title('Internal', fontsize=15, fontweight='bold') labels3= list(OrderedDict(sorted(Counter(us_external_data.attacktype1_txt).items(), key=lambda x: x[1], reverse=True)).keys()) sizes3 = list(OrderedDict(sorted(Counter(us_external_data.attacktype1_txt).items(), key=lambda x: x[1], reverse=True)).values()) patches3, texts3 = ax3.pie(sizes3, colors=cm.Set3(np.linspace(0, 1, len(sizes2))), startangle=90) ax3.legend(patches2, labels3, loc="right", fontsize=14) ax3.set_title('External', fontsize=15, fontweight='bold')
def draw_pie(title, labels, sizes, explode=None, output_path='./a.jpg', shadow=False, show_result=True): ''' 本函数用于绘制饼图 [labels](list): 标签列表 [sizes](list): 每个标签所对应的大小所组成的列表,与标签列表对应位置元素相对应(无需提供相对大小) [explode](tuple): 强调块元组,被强调的元素对应的位置为非零值(建议为0.05),其他元素对应位置为0,无需强调时不传入此参数 [output_path](str): 输出的文件路径(含文件名) [shadow](bool): 是否有阴影(默认为无)(阴影真的很难看) [show_result](bool): 是否产生直接输出(默认为是) ''' if explode: if not (len(labels) == len(sizes)) and (len(sizes) == len(explode)): raise("Error: labels, sizes, explode must be of the same size") else: if not (len(labels) == len(sizes)): raise("Error: labels and sizes must be of the same size") #用来正常显示中文标签 proptease = fm.FontProperties() proptease.set_size(size=13) #调节图形大小,宽,高 fig = plt.figure(figsize=(9,9),dpi=400) ax = fig.add_subplot(111) ax.set_position([0.1,0.1,0.6,0.85]) #定义饼状图的标签,标签是列表 # labels = [name for name in people.keys()] #每个标签占多大,会自动去算百分比 # sizes = [people[name]['time'] for name in people.keys()] #将某部分爆炸出来, 使用括号,将第一块分割出来,数值的大小是分割出来的与其他两块的间隙 # explode = (0.05,0,0) # cmap=plt.get_cmap('Pastel1') colors = cm.Set3(X=range(len(labels)),alpha=0.7) patches,l_text,p_text = plt.pie(sizes,explode=explode,labels=labels,colors=colors, labeldistance = 1.1,autopct = '%3.1f%%',shadow = shadow, startangle = 90,pctdistance = 0.6) #labeldistance,文本的位置离远点有多远,1.1指1.1倍半径的位置 #autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数 #shadow,饼是否有阴影 #startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看 #pctdistance,百分比的text离圆心的距离 #patches, l_texts, p_texts,为了得到饼图的返回值,p_texts饼图内部文本的,l_texts饼图外label的文本 #改变文本的大小 #方法是把每一个text遍历。调用set_size方法设置它的属性 for t in l_text: t.set_size=(30) for t in p_text: t.set_size=(20) # 设置x,y轴刻度一致,这样饼图才能是圆的 plt.axis('equal') plt.legend(bbox_to_anchor=(1.32,0.2), loc="lower right") plt.title(title, fontsize=20, fontproperties=proptease) if output_path: plt.savefig(output_path) if show_result: plt.show()
def pieChart(labels, sizes): fig, ax = plt.subplots(figsize=(8, 8)) patches, texts = plt.pie(sizes, colors=cm.Set3(np.linspace(0, 1, len(sizes))), startangle=90) plt.legend(patches, labels, loc="right", fontsize=15) plt.axis('equal') plt.tight_layout() plt.show()
def PlotPie(self): """活跃时间饼图""" fig, ax = plt.subplots(figsize=(8, 6)) data, otherdata, mergeIdx, splitIdx = \ MergeDataOfBelowThreshold(self.data_of_pie) # process color and label colors = np.array([ cm.Set3(i / len(self.work_states)) for i in range(len(self.work_states)) ]) work_states = np.array(self.work_states) if otherdata > 0: colors = list(colors[splitIdx]) + [cm.hsv(0)] ohter_labels = '\n'.join(work_states[mergeIdx]) work_states = list(work_states[splitIdx]) + ['others'] else: colors = list(colors[splitIdx]) ohter_labels = 'null' work_states = list(work_states[splitIdx]) data = data[:-1] # Segment the one with the largest percentage explode = [0] * data.shape[0] explode[np.argmax(data)] = 0.1 # plot pie patches, l_text, p_text = \ plt.pie(x=data, explode=explode, labels=work_states, labeldistance=1.1, colors=colors, autopct='%.0f%%', shadow=True, startangle=90, pctdistance=0.6) # params fontsize = 20 list(map(lambda t: t.set_size(fontsize), l_text)) list(map(lambda t: t.set_size(fontsize), p_text)) plt.title(u"近一个月投入时间", fontsize=fontsize + 5) plt.text(x=1.8, y=-1.2, s="*others(individual < 10%):\n" + ohter_labels, fontsize=fontsize + 5) # drop the redundant of labels via `dict.keys()` handles, labels = plt.gca().get_legend_handles_labels() by_label = OrderedDict(zip(labels, handles)) plt.legend(by_label.values(), by_label.keys(), bbox_to_anchor=(1.4, 1), loc='upper right', ncol=1, fontsize=fontsize + 5) plt.axis('equal') plt.savefig(self.fig_path, dpi=150, bbox_inches='tight') plt.close()
def draw_buffer(self): self.buff_win = pg.GraphicsLayoutWidget() self.buff_win.setWindowTitle('Buffer Status') self.buff_win.resize(800, 700) self.total_peers = self.number_of_monitors + self.number_of_peers + self.number_of_malicious self.p4 = self.buff_win.addPlot() self.p4.showGrid( x=True, y=True, alpha=100) # To show grid lines across x axis and y axis leftaxis = self.p4.getAxis('left') # get left axis i.e y axis leftaxis.setTickSpacing( 5, 1) # to set ticks at a interval of 5 and grid lines at 1 space # Get different colors using matplotlib library if self.total_peers < 8: colors = cm.Set2(np.linspace(0, 1, 8)) elif self.total_peers < 12: colors = cm.Set3(np.linspace(0, 1, 12)) else: colors = cm.rainbow(np.linspace(0, 1, self.total_peers + 1)) self.QColors = [ pg.hsvColor(color[0], color[1], color[2], color[3]) for color in colors ] # Create QtColors, each color would represent a peer self.Data = [ ] # To represent buffer out i.e outgoing data from buffer self.OutData = [] # To represent buffer in i.e incoming data in buffer # a single line would reperesent a single color or peer, hence we would not need to pass a list of brushes self.lineIN = [None] * self.total_peers for ix in range(self.total_peers): self.lineIN[ix] = self.p4.plot(pen=(None), symbolBrush=self.QColors[ix], name='IN', symbol='o', clear=False) self.Data.append(set()) self.OutData.append(set()) # similiarly one line per peer to represent outgoinf data from buffer self.lineOUT = self.p4.plot(pen=(None), symbolBrush=mkColor('#CCCCCC'), name='OUT', symbol='o', clear=False) self.p4.setRange(xRange=[0, self.total_peers], yRange=[0, self.get_buffer_size()]) self.buff_win.show() # To actually show create window self.buffer_order = {} self.buffer_index = 0 self.buffer_labels = [] self.lastUpdate = pg.ptime.time() self.avgFps = 0.0
def PlotBrokenBarh(self): """活跃时间间断图""" task_id = np.arange(len(self.work_states)) states_work_dic = dict(zip(task_id, self.work_states)) color_map = dict(zip(self.work_states, task_id)) n_day = len(self.activate_data) fig, ax = plt.subplots(figsize=(8, 6)) high, y_loc = 0.5, 0.7 for x in range(n_day): x_day_data = self.activate_data[x] if len(x_day_data) > 1: idx = x_day_data[-1] # draw per work for work in range(len(idx)): label = states_work_dic[idx[work]] ax.broken_barh([x_day_data[0][work]], [x + y_loc, high], color=cm.Set3(color_map[label] / len(self.work_states)), label=label) # draw inactivate ax.broken_barh(self.inactivate_data[x], [x + y_loc, high], color=cm.hsv(0), label='inactivate') # params interval = 1 fontsize = 20 ax.set_xticks(list(range(0, 3600 * 24 + 1, 3600 * interval))) ax.set_xticklabels(list(range(0, 24 + 1, interval)), fontsize=fontsize - 5) ax.set_yticks(list(range(1, n_day + 1))) ax.set_yticklabels(GetNDayList(self.today_dt, 7), fontsize=fontsize) plt.xlabel(u"小时", fontsize=fontsize) plt.ylabel(u"日期", fontsize=fontsize) plt.title(u"近一周投入时间", fontsize=fontsize + 5) plt.grid(linestyle="--", alpha=0.15) # drop the redundant of labels via `dict.keys()` handles, labels = plt.gca().get_legend_handles_labels() by_label = OrderedDict(zip(labels, handles)) plt.legend(by_label.values(), by_label.keys(), bbox_to_anchor=(0.1, -0.25), loc='center left', ncol=3, fontsize=fontsize - 5) plt.savefig(self.fig_path, dpi=150, bbox_inches='tight') plt.close()
def PlotStackBarForInformation(self): """信息柱形堆叠图""" quality_list = self.stacked_bar_data.quality.values columns = list(self.stacked_bar_data.columns) columns.remove('quality') colors = [cm.Set3(i / len(columns)) for i in range(len(columns))] del self.stacked_bar_data['quality'] stacked_bar_data = self.stacked_bar_data.values stacked_bar_data[np.isnan(stacked_bar_data)] = 0 # plot by stacking val = stacked_bar_data[:, 0] p1 = plt.bar(np.arange(len(quality_list)), val, width=0.5, color=colors[0], tick_label=quality_list) sub_matrix = stacked_bar_data[:, 0:1] csum = np.cumsum(sub_matrix, axis=1)[:, -1] csum = csum.flatten() for i in range(1, len(columns)): sub_matrix = stacked_bar_data[:, 0:i] csum = np.cumsum(sub_matrix, axis=1)[:, -1] csum = csum.flatten() val = stacked_bar_data[:, i] p3 = plt.bar(np.arange(len(quality_list)), val, width=0.5, bottom=csum, color=colors[i]) sub_matrix = stacked_bar_data[:, :] csum = np.cumsum(sub_matrix, axis=1)[:, -1] csum = csum.flatten() # params fontsize = 20 plt.legend(fontsize=fontsize - 2, loc='upper right', labels=columns) plt.xlabel(u"信息质量", fontsize=fontsize) plt.ylabel(u"时长(分钟)", fontsize=fontsize) plt.xticks(fontsize=fontsize) plt.yticks(fontsize=fontsize) plt.ylim([0, max(csum) * 1.2]) plt.title("今日信息摄入情况", fontsize=fontsize + 5) plt.savefig(self.fig_path, dpi=150, bbox_inches='tight') plt.close()
def _create_categorical_colors(n_categories: int): if n_categories > 51: raise ValueError( f"Maximum number of colors (51) exceeded: `{n_categories}`.") colors = [cm.Set1(i) for i in range(cm.Set1.N)][:n_categories] colors += [cm.Set2(i) for i in range(cm.Set2.N)][:n_categories - len(colors)] colors += [cm.Set3(i) for i in range(cm.Set3.N)][:n_categories - len(colors)] colors += [cm.tab10(i) for i in range(cm.tab10.N)][:n_categories - len(colors)] colors += [cm.Paired(i) for i in range(cm.Paired.N)][:n_categories - len(colors)] return _convert_to_hex_colors(colors)
def cal_youth(data_file): ''' :param: data_file :type: str calculate CA death rate at 15-34 range and return a pie plot ''' assert isinstance(data_file, str) data = pd.read_csv(data_file) data = data[data.state == 'California'] data = data[data['age'].isin(['15_24', '25_34'])] data = data[data.gender == 'both'] data = data.groupby('name')['d'].sum().to_frame().reset_index() data = data.sort_values(by='d', ascending=False).reset_index() data = data.drop(data.columns[[0]], axis=1) target_data = data[['name', 'd']].iloc[:10] other_data = data[['d']].iloc[10:].sum() target_data.loc[10] = data[['name', 'd']].iloc[10] target_data.at[10, 'name'] = 'Others' target_data.at[10, 'd'] = other_data labels = list(target_data.name) sizes = list(target_data.d) cs = cm.Set3(np.arange(len(sizes)) / len(sizes)) explode = np.ones((len(sizes))) explode[-1] = 0 explode[:-1] *= 0.1 # Plot f, ax = plt.subplots(figsize=(20, 10)) plt.pie(sizes, explode=explode, labels=labels, colors=cs, pctdistance=0.8, autopct='%1.1f%%', shadow=True, startangle=50, textprops={'fontsize': 18}) ax.set_title( 'Top 10 Death Causes (Male & Female, 15-34) in California, 2017', fontsize=22) ax.title.set_position([.5, 1.05]) plt.axis('equal') plt.show()
def plot_internal(kmeans_cluster, k_start, k_stop): """Plot the internal validation results """ fig = plt.figure(figsize=(16, 8)) gs = gridspec.GridSpec(4, 2, hspace=0.4) ax0 = plt.subplot(gs[:, 1]) ax1 = plt.subplot(gs[0, 0]) ax2 = plt.subplot(gs[1, 0]) ax3 = plt.subplot(gs[2, 0]) ax4 = plt.subplot(gs[3, 0]) ax1.plot(range(k_start, k_stop + 1), kmeans_cluster['inertias'], 'o--') ax1.set_title('inertias') ax1.grid(which='both') ax2.plot(range(k_start, k_stop + 1), kmeans_cluster['iidrs'], 'o--') ax2.set_title('intra-to-inter') ax2.grid(which='both') ax3.plot(range(k_start, k_stop + 1), kmeans_cluster['chs'], 'o--') ax3.set_title('calinski-harabaz score') ax3.grid(which='both') ax4.plot(range(k_start, k_stop + 1), kmeans_cluster['scs'], 'o--') ax4.set_xlabel('k') ax4.set_title('silhouette score') ax4.grid(which='both') sizes = [] for i in range(k_stop - k_start + 1): sizes.append( list(np.unique(kmeans_cluster['ys'][i], return_counts=True)[1])) colors = cm.Set3(range(len(sizes[-1]))) for i in range(len(sizes)): j_prev = 0 n = 0 for j in sizes[i]: ax0.bar(i + k_start, j, width=0.6, bottom=j_prev, color=colors[n]) j_prev += j n += 1 ax0.set_title('cluster sizes') ax0.set_xlabel('k') return gs
def cal_age(data_file): ''' :param: data_file :type: str calculate CA death rate at each age range and return a pie plot ''' assert isinstance(data_file, str) data = pd.read_csv(data_file) data = data[data.state == 'California'] data = data[data.gender == 'both'] data = data[data.age != 'all'] tmp = data.groupby('age')['d'].sum().to_frame() tmp['p'] = data.groupby('age')['p'].sum().to_frame() tmp = tmp.reset_index(level='age') labels = list(tmp.age) sizes = list(tmp.d) cs = cm.Set3(np.arange(len(sizes)) / len(sizes)) explode = np.ones((len(sizes))) explode[-1] = 0 explode[:-1] *= 0.1 # Plot f, ax = plt.subplots(figsize=(20, 10)) plt.pie(sizes, explode=explode, labels=labels, colors=cs, pctdistance=0.8, autopct='%1.1f%%', shadow=True, startangle=40, textprops={'fontsize': 20}) ax.set_title('Deaths (Ages) in California, 2017', fontsize=20) plt.axis('equal') plt.show()
def make_paint_out_img(img, canny, verify=False): _, _, contours, _ = cv2.connectedComponentsWithStats(canny) contours = sorted([c for c in contours if c[4] > con_size_th], key=lambda x: -x[4]) if not contours: return # 最外殻(ページ画像そのもの)を取り出しておく orig = contours.pop(0) orig_width, orig_height = orig[2], orig[3] # 最外殻以外で大きすぎるものを除外 contours = [ c for c in contours if not (c[2] > orig_width * 0.9 and c[3] > orig_height * 0.9) ] if not contours: return for idx, con in enumerate(contours): x, y, w, h, size = con if verify: if idx < 8: color = cm.Set1(idx % 8) elif idx < 16: color = cm.Set2(idx % 8) else: color = cm.Set3(idx % 8) color = [int(c * 255) for c in color[:3]] else: color = (192, 192, 192) # GAのカラー画像、棺担ぎのクロが抜けるようになった img_p = cv2.rectangle(img, (x, y), (x + w, y + h), color, -1) if verify: img_p[veryfy_pix * idx:veryfy_pix * idx + veryfy_pix, :veryfy_pix] = color return img_p
def PlotPieForInformation(self): """信息饼图""" fig, ax = plt.subplots(figsize=(8, 6)) # process color and label colors = [ cm.Set3(i / len(self.labels)) for i in range(len(self.labels)) ] # Segment the one with the largest percentage explode = [0] * self.data.shape[0] explode[np.argmax(self.data)] = 0.1 # plot pie patches, l_text, p_text = \ plt.pie(x=self.data, explode=explode, labels=self.labels, labeldistance=1.1, colors=colors, autopct='%.0f%%', shadow=True, startangle=90, pctdistance=0.6) # params fontsize = 20 list(map(lambda t: t.set_size(fontsize), l_text)) list(map(lambda t: t.set_size(fontsize), p_text)) plt.title(u"今日信息摄入质量情况", fontsize=fontsize + 5) # drop the redundant of labels via `dict.keys()` handles, labels = plt.gca().get_legend_handles_labels() by_label = OrderedDict(zip(labels, handles)) plt.legend(by_label.values(), by_label.keys(), bbox_to_anchor=(1.4, 1), loc='upper right', ncol=1, fontsize=fontsize + 5) plt.axis('equal') plt.savefig(self.fig_path, dpi=150, bbox_inches='tight') plt.close()
from matplotlib_venn import venn3, venn3_circles from matplotlib_venn import venn2, venn2_circles import json from PIL import Image import webbrowser from wordcloud import WordCloud, STOPWORDS import subprocess from datetime import datetime import networkx as nx import nxviz as nxv ##### from matplotlib import cm words = ['Biological Process', 'Molecular Function', 'Cellular Component'] pie_colors = { 'Set3': cm.Set3(np.arange(12) / 12.), 'Set2': cm.Set2(np.arange(8) / 8.), 'Set1': cm.Set1(np.arange(9) / 9.), 'Pastel2': cm.Pastel2(np.arange(8) / 8.), 'Pastel1': cm.Pastel1(np.arange(9) / 9.), 'Dark2': cm.Dark2(np.arange(8) / 8.), 'Paired': cm.Paired(np.arange(12) / 12.), 'Accent': cm.Accent(np.arange(8) / 8.), 'Spectral': cm.Spectral(np.arange(11) / 11.) } colors = { '#8DD3C7': pie_colors['Set3'][0:1], '#FFFFB3': pie_colors['Set3'][1:2], '#BEBADA': pie_colors['Set3'][2:3], '#FB8072': pie_colors['Set3'][3:4], '#80B1D3': pie_colors['Set3'][4:5],
def predict(_run, _log): cfg = edict(_run.config) torch.manual_seed(cfg.seed) np.random.seed(cfg.seed) random.seed(cfg.seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # build network network = UNet(cfg.model) if not (cfg.resume_dir == 'None'): model_dict = torch.load(cfg.resume_dir, map_location=lambda storage, loc: storage) network.load_state_dict(model_dict) # load nets into gpu if cfg.num_gpus > 1 and torch.cuda.is_available(): network = torch.nn.DataParallel(network) network.to(device) network.eval() transforms = tf.Compose([ tf.ToTensor(), tf.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) bin_mean_shift = Bin_Mean_Shift(device=device) k_inv_dot_xy1 = get_coordinate_map(device) instance_parameter_loss = InstanceParameterLoss(k_inv_dot_xy1) h, w = 192, 256 with torch.no_grad(): image = cv2.imread(cfg.image_path) # the network is trained with 192*256 and the intrinsic parameter is set as ScanNet image = cv2.resize(image, (w, h)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = Image.fromarray(image) image = transforms(image) image = image.to(device).unsqueeze(0) # forward pass logit, embedding, _, _, param = network(image) prob = torch.sigmoid(logit[0]) # infer per pixel depth using per pixel plane parameter, currently Q_loss need a dummy gt_depth as input _, _, per_pixel_depth = Q_loss(param, k_inv_dot_xy1, torch.ones_like(logit)) # fast mean shift segmentation, sampled_segmentation, sample_param = bin_mean_shift.test_forward( prob, embedding[0], param, mask_threshold=0.1) # since GT plane segmentation is somewhat noise, the boundary of plane in GT is not well aligned, # we thus use avg_pool_2d to smooth the segmentation results b = segmentation.t().view(1, -1, h, w) pooling_b = torch.nn.functional.avg_pool2d(b, (7, 7), stride=1, padding=(3, 3)) b = pooling_b.view(-1, h * w).t() segmentation = b # infer instance depth instance_loss, instance_depth, instance_abs_disntace, instance_parameter = instance_parameter_loss( segmentation, sampled_segmentation, sample_param, torch.ones_like(logit), torch.ones_like(logit), False) # infer instance normal _, _, manhattan_norm, instance_norm = surface_normal_loss( param, torch.ones_like(image), None) # return cluster results predict_segmentation = segmentation.cpu().numpy().argmax(axis=1) # mask out non planar region predict_segmentation[prob.cpu().numpy().reshape(-1) <= 0.1] = 20 predict_segmentation = predict_segmentation.reshape(h, w) # visualization and evaluation image = tensor_to_image(image.cpu()[0]) mask = (prob > 0.1).float().cpu().numpy().reshape(h, w) depth = instance_depth.cpu().numpy()[0, 0].reshape(h, w) per_pixel_depth = per_pixel_depth.cpu().numpy()[0, 0].reshape(h, w) manhattan_normal_2d = manhattan_norm.cpu().numpy().reshape( h, w, 3) * np.expand_dims((predict_segmentation != 20), -1) instance_normal_2d = instance_norm.cpu().numpy().reshape( h, w, 3) * np.expand_dims((predict_segmentation != 20), -1) pcd = o3d.geometry.PointCloud() norm_colors = cm.Set3(predict_segmentation.reshape(w * h)) pcd.points = o3d.utility.Vector3dVector( np.reshape(manhattan_normal_2d, (w * h, 3))) pcd.colors = o3d.utility.Vector3dVector(norm_colors[:, 0:3]) o3d.io.write_point_cloud('./manhattan_sphere.ply', pcd) pcd.points = o3d.utility.Vector3dVector( np.reshape(instance_normal_2d, (w * h, 3))) o3d.io.write_point_cloud('./instance_sphere.ply', pcd) normal_plot = cv2.resize( ((manhattan_normal_2d + 1) * 128).astype(np.uint8), (w, h)) # use per pixel depth for non planar region depth = depth * (predict_segmentation != 20) + per_pixel_depth * (predict_segmentation == 20) # change non planar to zero, so non planar region use the black color predict_segmentation += 1 predict_segmentation[predict_segmentation == 21] = 0 pred_seg = cv2.resize( np.stack([ colors[predict_segmentation, 0], colors[predict_segmentation, 1], colors[predict_segmentation, 2] ], axis=2), (w, h)) # blend image blend_pred = (pred_seg * 0.7 + image * 0.3).astype(np.uint8) mask = cv2.resize((mask * 255).astype(np.uint8), (w, h)) mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) # visualize depth map as PlaneNet depth_real = cv2.cvtColor(cv2.resize(depth, (w, h)), cv2.COLOR_GRAY2BGR) depth = 255 - np.clip(depth / 5 * 255, 0, 255).astype(np.uint8) depth = cv2.cvtColor(cv2.resize(depth, (w, h)), cv2.COLOR_GRAY2BGR) Camera_fx = 481.2 Camera_fy = 480.0 Camera_cx = -319.5 Camera_cy = 239.50 ## # Camera_fx = 535.4 # Camera_fy = 539.2 # Camera_cx = 320.1 # Camera_cy = 247.6 # correct parameters Camera_fx = 518.8 Camera_fy = 518.8 Camera_cx = 320 Camera_cy = 240 # cabin parrameters Camera_fx = 440.66 Camera_fy = 531.72 Camera_cx = 361.77 Camera_cy = 215.79 points = [] points_instance = [] ratio_x = 640 / 256.0 ratio_y = 480 / 192.0 scalingFactor = 1.0 for v in range(h): for u in range(w): color = image[v, u] color_instance = pred_seg[v, u] Z = (depth_real[v, u] / scalingFactor)[0] if Z == 0: continue X = (u - Camera_cx / ratio_x) * Z / Camera_fx * ratio_x Y = (v - Camera_cy / ratio_y) * Z / Camera_fy * ratio_y # points.append("%f %f %f %d %d %d 0\n" % (X, Y, Z, color[2], color[1], color[0])) points_instance.append("%f %f %f %d %d %d 0\n" % (X, Y, Z, color_instance[2], color_instance[1], color_instance[0])) file1 = open('./pointCloud_instance.ply', "w") file1.write('''ply format ascii 1.0 element vertex %d property float x property float y property float z property uchar red property uchar green property uchar blue property uchar alpha end_header %s ''' % (len(points_instance), "".join(points_instance))) file1.close() image = np.concatenate( (image, pred_seg, blend_pred, mask, depth, normal_plot), axis=1) cv2.imshow('image', image) cv2.waitKey(0)
def generate__colors(colorType="default", nColors=10, seaborn=True, howto=False): if (howto == True): print( "[generate__colors.py] generate__colors( colorType=, nColors=, seaborn=T/F, howto=T/F )" ) print( "[generate__colors.py] colorType = [ \ \n"\ "default, bright, deep, muted, colorblind, pastel, \n"\ "jet, tab10, tab20, hsv, accent, pastel1, pastel2, set1, set2, set3 \n"\ " ] ") return () # ------------------------------------------------- # # --- [1] generate colors --- # # ------------------------------------------------- # if (not (seaborn)): if (colorType.lower() == "jet"): colors = [cm.jet(ik / float(nColors)) for ik in range(nColors)] elif (colorType.lower() == "tab10"): colors = [cm.tab10(ik / float(nColors)) for ik in range(nColors)] elif (colorType.lower() == "tab20"): colors = [cm.tab20(ik / float(nColors)) for ik in range(nColors)] elif (colorType.lower() == "hsv"): colors = [cm.hsv(ik / float(nColors)) for ik in range(nColors)] elif (colorType.lower() == "accent"): colors = [cm.Accent(ik / float(nColors)) for ik in range(nColors)] elif (colorType.lower() == "pastel1"): colors = [cm.Pastel1(ik / float(nColors)) for ik in range(nColors)] elif (colorType.lower() == "pastel2"): colors = [cm.Pastel2(ik / float(nColors)) for ik in range(nColors)] elif (colorType.lower() == "set1"): colors = [cm.Set1(ik / float(nColors)) for ik in range(nColors)] elif (colorType.lower() == "set2"): colors = [cm.Set2(ik / float(nColors)) for ik in range(nColors)] elif (colorType.lower() == "set3"): colors = [cm.Set3(ik / float(nColors)) for ik in range(nColors)] else: print("[generate__colors.py] colorType == ?? ") sys.exit() else: if (colorType.lower() == "jet"): print("[generate__colors.py] no jet palette for seaborn") sys.exit() elif (colorType.lower() == "default"): colors = sns.color_palette(n_colors=nColors) elif (colorType.lower() == "deep"): colors = sns.color_palette(palette="deep", n_colors=nColors) elif (colorType.lower() == "colorblind"): colors = sns.color_palette(palette="colorblind", n_colors=nColors) elif (colorType.lower() == "dark"): colors = sns.color_palette(palette="dark", n_colors=nColors) elif (colorType.lower() == "bright"): colors = sns.color_palette(palette="bright", n_colors=nColors) elif (colorType.lower() == "muted"): colors = sns.color_palette(palette="muted", n_colors=nColors) elif (colorType.lower() == "pastel"): colors = sns.color_palette(palette="pastel", n_colors=nColors) elif (colorType.lower() == "hsv"): colors = sns.color_palette(palette="hsv", n_colors=nColors) elif (colorType.lower() == "accent"): colors = sns.color_palette(palette="Accent", n_colors=nColors) elif (colorType.lower() == "pastel1"): colors = sns.color_palette(palette="Pastel1", n_colors=nColors) elif (colorType.lower() == "pastel2"): colors = sns.color_palette(palette="Pastel2", n_colors=nColors) elif (colorType.lower() == "tab10"): colors = sns.color_palette(palette="tab10", n_colors=nColors) elif (colorType.lower() == "tab20"): colors = sns.color_palette(palette="tab20", n_colors=nColors) elif (colorType.lower() == "set1"): colors = sns.color_palette(palette="Set1", n_colors=nColors) elif (colorType.lower() == "set2"): colors = sns.color_palette(palette="Set2", n_colors=nColors) elif (colorType.lower() == "set3"): colors = sns.color_palette(palette="Set3", n_colors=nColors) else: colors = sns.color_palette(palette=colorType, n_colors=nColors) # ------------------------------------------------- # # --- [2] return --- # # ------------------------------------------------- # return (colors)
import matplotlib.pyplot as plt from matplotlib_venn import venn3, venn3_circles, venn3_unweighted from matplotlib_venn import venn2, venn2_circles, venn2_unweighted import json from PIL import Image import webbrowser #from wordcloud import WordCloud, STOPWORDS import subprocess from datetime import datetime import networkx as nx import nxviz as nxv ##### from matplotlib import cm words = ['Biological Process', 'Molecular Function', 'Cellular Component'] pie_colors = {'Set3':cm.Set3(np.arange(12)/12.), 'Set2':cm.Set2(np.arange(8)/8.), 'Set1':cm.Set1(np.arange(9)/9.), 'Pastel2':cm.Pastel2(np.arange(8)/8.), 'Pastel1':cm.Pastel1(np.arange(9)/9.), 'Dark2':cm.Dark2(np.arange(8)/8.), 'Paired':cm.Paired(np.arange(12)/12.), 'Accent':cm.Accent(np.arange(8)/8.), 'Spectral':cm.Spectral(np.arange(11)/11.), 'tab20':cm.tab20(np.arange(20)/20.), 'tab20b':cm.tab20b(np.arange(20)/20.), 'tab20c':cm.tab20c(np.arange(20)/20.)} colors = {'#8DD3C7':pie_colors['Set3'][0:1], '#FFFFB3':pie_colors['Set3'][1:2], '#BEBADA':pie_colors['Set3'][2:3], '#FB8072':pie_colors['Set3'][3:4], '#80B1D3':pie_colors['Set3'][4:5], '#FDB462':pie_colors['Set3'][5:6], '#B3DE69':pie_colors['Set3'][6:7], '#FCCDE5':pie_colors['Set3'][7:8],
def df_input(dfUniprot=DataFrame([])): num_phylum = len(dfUniprot.PHYLUM.drop_duplicates()) from matplotlib import cm set3 = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.Set3(np.arange(12) / 12.) ] set2 = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.Set2(np.arange(8) / 8.) ] set1 = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.Set1(np.arange(9) / 9.) ] pastel2 = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.Pastel2(np.arange(8) / 8.) ] pastel1 = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.Pastel1(np.arange(9) / 9.) ] dark2 = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.Dark2(np.arange(8) / 8.) ] paired = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.Paired(np.arange(12) / 12.) ] accent = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.Accent(np.arange(8) / 8.) ] spectral = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.Spectral(np.arange(11) / 11.) ] tab20 = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.tab20(np.arange(20) / 20.) ] tab20b = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.tab20b(np.arange(20) / 20.) ] tab20c = [ matplotlib.colors.rgb2hex(tuple(i)) for i in cm.tab20c(np.arange(20) / 20.) ] Colors1 = set2 + set1 + dark2 + paired + accent + spectral + tab20 + tab20b + tab20c Colors2 = accent + spectral + tab20 + tab20b + tab20c + set1 + set2 + dark2 + paired Colors3 = dark2 + paired + accent + spectral + tab20 + tab20b + tab20c + set1 + set2 Colors4 = tab20b + tab20c + set1 + set2 + dark2 + paired + accent + spectral + tab20 Colors5 = spectral + tab20 + tab20b + tab20c + set1 + set2 + dark2 + paired + accent pie_colors = { 'Set3': cm.Set3(np.arange(12) / 12.), 'Set2': cm.Set2(np.arange(8) / 8.), 'Set1': cm.Set1(np.arange(9) / 9.), 'Pastel2': cm.Pastel2(np.arange(8) / 8.), 'Pastel1': cm.Pastel1(np.arange(9) / 9.), 'Dark2': cm.Dark2(np.arange(8) / 8.), 'Paired': cm.Paired(np.arange(12) / 12.), 'Accent': cm.Accent(np.arange(8) / 8.), 'Spectral': cm.Spectral(np.arange(11) / 11.), 'tab20': cm.tab20(np.arange(20) / 20.), 'tab20b': cm.tab20b(np.arange(20) / 20.), 'tab20c': cm.tab20c(np.arange(20) / 20.) } circle_colors = { 'Colors1': Colors1[0:num_phylum], 'Colors2': Colors2[0:num_phylum], 'Colors3': Colors3[0:num_phylum], 'Colors4': Colors4[0:num_phylum], 'Colors5': Colors5[0:num_phylum] } def tax_colors(color_list=circle_colors['Colors1'], taxx=dfUniprot): tax_cols = [ 'Entry', 'Tax_ID', 'KINGDOM', 'PHYLUM', 'CLASS', 'ORDER', 'FAMILY', 'GENUS', 'SPECIES', 'Organism' ] new2 = taxx[tax_cols].drop_duplicates() #>>>>>>>>>>>>>>>>>>>>>>>>>>> phylum0 = new2.groupby(['PHYLUM' ]).Entry.count().reset_index().sort_values( by='Entry', ascending=False).reset_index(drop=True) asign_color = {} for i, j in zip(phylum0.PHYLUM, color_list): if i == 'NA': asign_color[i] = 'black' else: asign_color[i] = j phylum0['phy_col'] = list(asign_color.values()) # distribución de Class phylum1 = new2.groupby(['PHYLUM', 'CLASS']).Entry.count().reset_index() class0 = [] class0_colors = [] for i in phylum0.PHYLUM: for j in phylum1.PHYLUM: if i == j: class0_colors.append(asign_color[j]) class0.append(phylum1[phylum1.PHYLUM == i].sort_values( by='Entry', ascending=False).reset_index(drop=True)) else: pass class1 = pd.concat(class0).drop_duplicates() class1['class_col'] = class0_colors class0_colors_corregido = [] for index, row in class1.iterrows(): if row.PHYLUM == 'NA': if row.CLASS == 'NA': class0_colors_corregido.append(row.class_col) else: class0_colors_corregido.append('grey') else: if row.CLASS == 'NA': class0_colors_corregido.append('black') else: class0_colors_corregido.append(row.class_col) class1['class_col'] = class0_colors_corregido class11 = class1.groupby(['CLASS' ]).Entry.sum().reset_index().sort_values( by='Entry', ascending=False).reset_index(drop=True) class11 = class11.merge(class1[['CLASS', 'class_col']].drop_duplicates(), on='CLASS', how='left') # distribución de Order phylum2 = new2.groupby(['PHYLUM', 'CLASS', 'ORDER']).Entry.count().reset_index() order0 = [] order0_colors = [] for i in phylum0.PHYLUM: for j in phylum2.PHYLUM: if i == j: order0_colors.append(asign_color[j]) order0.append(phylum2[phylum2.PHYLUM == i].sort_values( by='Entry', ascending=False).reset_index(drop=True)) else: pass order1 = pd.concat(order0).drop_duplicates() order1['order_col'] = order0_colors order0_colors_corregido = [] for index, row in order1.iterrows(): if row.PHYLUM == 'NA': if row.ORDER == 'NA': order0_colors_corregido.append(row.order_col) else: order0_colors_corregido.append('grey') else: if row.ORDER == 'NA': order0_colors_corregido.append('black') else: order0_colors_corregido.append(row.order_col) order1['order_col'] = order0_colors_corregido order11 = order1.groupby(['ORDER' ]).Entry.sum().reset_index().sort_values( by='Entry', ascending=False).reset_index(drop=True) order11 = order11.merge(order1[['ORDER', 'order_col']].drop_duplicates(), on='ORDER', how='left') # distribución de Genus phylum3 = new2.groupby(['PHYLUM', 'CLASS', 'ORDER', 'GENUS']).Entry.count().reset_index() genus0 = [] genus0_colors = [] for i in phylum0.PHYLUM: for j in phylum3.PHYLUM: if i == j: genus0_colors.append(asign_color[j]) genus0.append(phylum3[phylum3.PHYLUM == i].sort_values( by='Entry', ascending=False).reset_index(drop=True)) else: pass genus1 = pd.concat(genus0).drop_duplicates() genus1['genus_col'] = genus0_colors genus0_colors_corregido = [] for index, row in genus1.iterrows(): if row.PHYLUM == 'NA': if row.GENUS == 'NA': genus0_colors_corregido.append(row.genus_col) else: genus0_colors_corregido.append('grey') else: if row.GENUS == 'NA': genus0_colors_corregido.append('black') else: genus0_colors_corregido.append(row.genus_col) genus1['genus_col'] = genus0_colors_corregido genus11 = genus1.groupby(['GENUS' ]).Entry.sum().reset_index().sort_values( by='Entry', ascending=False).reset_index(drop=True) genus11 = genus11.merge(genus1[['GENUS', 'genus_col']].drop_duplicates(), on='GENUS', how='left') # distribución de Organism phylum4 = new2.groupby( ['PHYLUM', 'CLASS', 'ORDER', 'GENUS', 'Organism']).Entry.count().reset_index() org0 = [] org0_colors = [] for i in phylum0.PHYLUM: for j in phylum4.PHYLUM: if i == j: org0_colors.append(asign_color[j]) org0.append(phylum4[phylum4.PHYLUM == i].sort_values( by='Entry', ascending=False).reset_index(drop=True)) else: pass org1 = pd.concat(org0).drop_duplicates() org1['org_col'] = org0_colors org0_colors_corregido = [] for index, row in org1.iterrows(): if row.PHYLUM == 'NA': if row.Organism == 'NA': org0_colors_corregido.append(row.org_col) else: org0_colors_corregido.append('grey') else: if row.Organism == 'NA': org0_colors_corregido.append('black') else: org0_colors_corregido.append(row.org_col) org1['org_col'] = org0_colors_corregido org11 = org1.groupby(['Organism' ]).Entry.sum().reset_index().sort_values( by='Entry', ascending=False).reset_index(drop=True) org11 = org11.merge(org1[['Organism', 'org_col']].drop_duplicates(), on='Organism', how='left') os.makedirs('tax', exist_ok=True) return phylum0.to_csv('tax/phylum0.tsv', sep = '\t', index = None),\ class1.to_csv('tax/class1.tsv', sep = '\t', index = None),\ class11.to_csv('tax/class11.tsv', sep = '\t', index = None),\ order1.to_csv('tax/order1.tsv', sep = '\t', index = None),\ order11.to_csv('tax/order11.tsv', sep = '\t', index = None),\ genus1.to_csv('tax/genus1.tsv', sep = '\t', index = None),\ genus11.to_csv('tax/genus11.tsv', sep = '\t', index = None),\ org1.to_csv('tax/org1.tsv', sep = '\t', index = None),\ org11.to_csv('tax/org11.tsv', sep = '\t', index = None) alfas = { 'Lineage*': [1, 1, 1, 1, 1], 'Phylum': [1, 0.3, 0.3, 0.3, 0.3], 'Class': [0.3, 1, 0.3, 0.3, 0.3], 'Order': [0.3, 0.3, 1, 0.3, 0.3], 'Genus': [0.3, 0.3, 0.3, 1, 0.3], 'Species': [0.3, 0.3, 0.3, 0.3, 1], 'Gradient1*': [1, 0.85, 0.7, 0.55, 0.4], 'Gradient2*': [0.4, 0.55, 0.7, 0.85, 1], 'Attenuate*': [0.3, 0.3, 0.3, 0.3, 0.3], 'None*': [0, 0, 0, 0, 0] } def circle_lineage(alphas=alfas['Phylum']): #fig, ax = plt.subplots(111, facecolor= 'white') #fig, ax = plt.subplot(111) phylum0 = pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA') class1 = pd.read_csv('tax/class1.tsv', sep='\t').fillna('NA') order1 = pd.read_csv('tax/order1.tsv', sep='\t').fillna('NA') genus1 = pd.read_csv('tax/genus1.tsv', sep='\t').fillna('NA') org1 = pd.read_csv('tax/org1.tsv', sep='\t').fillna('NA') radio = 0.5 linaje = [phylum0, class1, order1, genus1, org1] #colores = [list(asign_color.values()), class0_colors, order0_colors, genus0_colors, org0_colors] colores = ['phy_col', 'class_col', 'order_col', 'genus_col', 'org_col'] pat = [] size = -.205 for i, j, k in zip(linaje, colores, alphas): size += .205 patches, texts = plt.pie( i.Entry, radius=radio + size, labels=None, labeldistance=0.8, rotatelabels=True, colors= i[j], # new_colors(valor = len(i.Entry), col = 'nipy_spectral'), wedgeprops=dict(width=0.2, edgecolor='white', alpha=k), textprops=dict(size=10)) pat.append(patches) #plt.legend(pat[0], df_phylum.PHYLUM, loc=2,fontsize=13,labelspacing = 0.4, # bbox_to_anchor=(1.05, 1),frameon=False) plt.gca().set(aspect='equal') plt.title('Root', fontsize=10, x=0.5, y=0.465) plt.text(-1.8, 1.35, 'Lineage', fontsize=15, ha='left', va='center', color='black') #plt.title('Lineage',fontsize=20, fontweight='bold', x = -0.17, y = 1) #plt.text(1.1, 1.35, linaje_seleccionado, fontsize = 15, ha='left', va='center', # color='black') #>>>>>>>>>>>>>>>>>>>>>>> #### insetplot #ax2 = plt.axes([0.1, 0.66, 0.13, 0.14]) ax2 = plt.axes([-0.07, 1.71, 0.17, 0.18]) logo = [20, 20, 20, 20, 20, 20, 20, 20] logo_col = [ 'white', 'white', 'black', 'white', 'white', 'white', 'white', 'white' ] logo_col1 = [ 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black' ] radio = 0.5 linaje = [logo, logo, logo, logo, logo] colores = [logo_col1, logo_col, logo_col, logo_col, logo_col] name_linaje = ['Phylum', 'Class', 'Order', 'Genus', 'Species'] pat = [] size = -.44 pos = -.18 for i, j, k, l in zip(linaje, colores, name_linaje, alphas): pos += .47 size += .44 ax2.pie(i, radius=radio + size, labels=None, colors=j, wedgeprops=dict(width=0.35, edgecolor='white', alpha=l), textprops=dict(size=10)) ax2.text(0.1, pos, k, fontsize=9, ha='left', va='center', fontweight='bold', alpha=l) #color='black' def barras_tax(df=DataFrame([]), column=0, dim=111, title='', row_num=10, color=['#ff7f0e'], size_x=8, size_y=10, ylabel_text='', xlabel=10, ylabel=10, size_title=15, size_bartxt=10, sep=1.2): if len(df) == 0: print('Data frame sin datos') else: #plt.subplot(dim, facecolor= 'white') barWidth = 0.9 if row_num == len(df): ejey = list(df.iloc[0:len(df), 1]) val = max(ejey) ejex = list(df.iloc[0:len(df), column]) colores = list(df.iloc[0:len(df), 2]) borde = list( np.repeat('white', len(df.iloc[0:row_num, column]))) linea = list(np.repeat(0, len(df.iloc[0:row_num, column]))) if row_num < len(df): ejey = list(df.iloc[0:row_num, 1]) + [df.iloc[row_num:len(df), 1].sum()] val = max(ejey) ejex = list(df.iloc[0:row_num, column]) + ['Others'] borde = list( np.repeat('white', len(df.iloc[0:row_num, column]))) + ['black'] colores = list(df.iloc[0:row_num, 2]) + ['linen'] linea = list(np.repeat(0, len(df.iloc[0:row_num, column]))) + [1] if row_num > len(df): ejey = list(df.iloc[0:len(df), 1]) val = max(ejey) ejex = list(df.iloc[0:len(df), column]) borde = list( np.repeat('white', len(df.iloc[0:row_num, column]))) colores = list(df.iloc[0:len(df), 2]) linea = list(np.repeat(0, len(df.iloc[0:row_num, column]))) for i, j, k, l, m in zip(ejex, ejey, borde, colores, linea): plt.barh(i, j, color=l, align='center', height=0.7, linewidth=m, alpha=1, edgecolor=k) plt.gca().spines['right'].set_visible(False) plt.gca().spines['top'].set_visible(False) plt.gca().spines['bottom'].set_position(('data', -0.6)) plt.gca().spines['left'].set_visible(False) plt.title(title, size=size_title, loc='left') plt.tick_params(axis="y", color="gray") plt.yticks(size=size_y) v1 = -50 v2 = 0 v3 = 0 for i in range(10000): v1 += 50 v2 += 50 v3 += 10 if v1 <= max(list(ejey)) < v2: #print(v3, v1, val, v2) escala = v3 plt.xticks(range(0, val, escala), size=size_x) #fontweight='bold' plt.ylabel(ylabel_text, size=ylabel) plt.xlabel("Number of Proteins", size=xlabel) #plt.tick_params(top = 'on', bottom = 'on', right = 'on', left = 'on') #plt.tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False) for j, k in zip(ejey, range(0, len(ejey))): plt.text(j + sep, k - 0.2, j, size=size_bartxt, ha='left', color='black') import ipywidgets as widgets from ipywidgets import interact, interactive, fixed, interact_manual, Button, HBox, VBox, IntSlider, Label, IntRangeSlider from ipywidgets import Checkbox, RadioButtons from ipywidgets import Button, Layout alfas = { 'Lineage*': [1, 1, 1, 1, 1], 'Phylum': [1, 0.3, 0.3, 0.3, 0.3], 'Class': [0.3, 1, 0.3, 0.3, 0.3], 'Order': [0.3, 0.3, 1, 0.3, 0.3], 'Genus': [0.3, 0.3, 0.3, 1, 0.3], 'Species': [0.3, 0.3, 0.3, 0.3, 1], 'Gradient1*': [1, 0.85, 0.7, 0.55, 0.4], 'Gradient2*': [0.4, 0.55, 0.7, 0.85, 1], 'Attenuate*': [0.3, 0.3, 0.3, 0.3, 0.3], 'None*': [0, 0, 0, 0, 0] } plotss = ['Phylum', 'Class', 'Order', 'Genus', 'Species'] posicion_subplots = [] n = 0.9 while n < 2: n += 0.1 posicion_subplots.append(np.around(n, 1)) color_a6 = widgets.Dropdown(options=list(circle_colors.keys()), value='Colors1', description='Colors:', disabled=False, button_style='', layout=Layout(width='20%', height='25px')) a6 = widgets.Dropdown(options=list(alfas.keys()), description='Chart 1:', value='Phylum', disabled=False, button_style='', layout=Layout(width='20%', height='25px')) a61 = widgets.Dropdown(options=plotss, description='Chart 2:', disabled=False, button_style='', layout=Layout(width='20%', height='25px')) pos_sub1 = widgets.Dropdown(options=posicion_subplots, value=1.3, description='xloc1:', disabled=False, layout=Layout(width='15%', height='25px')) pos_sub2 = widgets.Dropdown(options=posicion_subplots, value=1.3, description='xloc2:', disabled=False, layout=Layout(width='15%', height='25px')) b6 = widgets.Dropdown(options=list(range(0, 101)), value=10, description='rows1:', disabled=False, layout=Layout(width='15%', height='25px')) c6 = widgets.Dropdown(options=list(range(0, 101)), value=10, description='rows2:', disabled=False, layout=Layout(width='15%', height='25px')) z6 = widgets.ToggleButton(value=False, description='Save Chart', disabled=False, button_style='', tooltip='Description') o6 = widgets.Dropdown(options=[0, 0.25, 0.5, 0.75] + list(range(0, 201)), value=3, description='sep1:', disabled=False, layout=Layout(width='15%', height='25px')) o61 = widgets.Dropdown(options=[0, 0.25, 0.5, 0.75] + list(range(0, 201)), value=3, description='sep2:', disabled=False, layout=Layout(width='15%', height='25px')) d6 = widgets.Dropdown(options=list(range(0, 51)), value=8, description='size_y1:', disabled=False, layout=Layout(width='15%', height='25px')) d61 = widgets.Dropdown(options=list(range(0, 51)), value=8, description='size_y2:', disabled=False, layout=Layout(width='15%', height='25px')) g6 = widgets.Dropdown(options=list(range(0, 51)), value=8, description='bartxt1:', disabled=False, layout=Layout(width='15%', height='25px')) g61 = widgets.Dropdown(options=list(range(0, 51)), value=8, description='bartxt2:', disabled=False, layout=Layout(width='15%', height='25px')) xxx = Button(layout=Layout(width='5%', height='25px'), disabled=True) xxx.style.button_color = 'white' yyy = Button(layout=Layout(width='94%', height='5px'), disabled=True) yyy.style.button_color = 'red' ww = widgets.HBox([color_a6, xxx, z6]) w6 = widgets.HBox([ a6, b6, o6, d6, g6, pos_sub1, ]) w7 = widgets.HBox([ a61, c6, o61, d61, g61, pos_sub2, ]) w8 = widgets.VBox([w6, w7, yyy]) ###### def col(color_a6): tax_colors(color_list=circle_colors[color_a6], taxx=dfUniprot) out7 = widgets.interactive_output(col, {'color_a6': color_a6}) def box1(a6, a61, pos_sub1, pos_sub2, b6, c6, z6, o6, o61, d6, d61, g6, g61): yetiquetas_plot1 = { 'Lineage*': 'Phylum', 'Phylum': 'Phylum', 'Class': 'Class', 'Order': 'Order', 'Genus': 'Genus', 'Species': 'Species', 'Gradient1*': 'Phylum', 'Gradient2*': 'Phylum', 'Attenuate*': 'Phylum', 'None*': 'Phylum' } plots1 = { 'Lineage*': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA'), 'Phylum': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA'), 'Class': pd.read_csv('tax/class11.tsv', sep='\t').fillna('NA'), 'Order': pd.read_csv('tax/order11.tsv', sep='\t').fillna('NA'), 'Genus': pd.read_csv('tax/genus11.tsv', sep='\t').fillna('NA'), 'Species': pd.read_csv('tax/org11.tsv', sep='\t').fillna('NA'), 'Gradient1*': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA'), 'Gradient2*': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA'), 'Attenuate*': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA'), 'None*': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA') } plots2 = { 'Phylum': pd.read_csv('tax/phylum0.tsv', sep='\t').fillna('NA'), 'Class': pd.read_csv('tax/class11.tsv', sep='\t').fillna('NA'), 'Order': pd.read_csv('tax/order11.tsv', sep='\t').fillna('NA'), 'Genus': pd.read_csv('tax/genus11.tsv', sep='\t').fillna('NA'), 'Species': pd.read_csv('tax/org11.tsv', sep='\t').fillna('NA') } ax3 = plt.axes([pos_sub2, .97, .3, 0.55]) ##>>>>>>>>>>> grafico circular ax = plt.axes([0, 1, 0.9, 1]) circle_lineage(alphas=alfas[a6]) ##>>>>>>>>>>> grafico 1 #ax2 = plt.axes([pos_sub1, 1.51, .3, 0.55]) ax2 = plt.axes([pos_sub1, 1.63, .3, 0.4]) #>>>>>>>>>> barras_tax( plots1[a6], #barras_tax(tax_colors(color_list = circle_colors['Spectral'])[0], row_num=b6, color=plots1[a6].iloc[0:b6, 2], sep=o6, size_y=d6, size_bartxt=g6, ylabel_text=yetiquetas_plot1[a6], ylabel=10) ##>>>>>>>>>>> grafico 2 ax3 = plt.axes([pos_sub2, .97, .3, 0.55]) barras_tax( plots2[a61], #barras_tax(tax_colors(color_list = circle_colors['Spectral'])[0], row_num=c6, color=plots2[a61].iloc[0:b6, 2], sep=o61, size_y=d61, size_bartxt=g61, ylabel_text=yetiquetas_plot1[a61], ylabel=10) ##>>>>>>>>>>>> save if z6 == True: import datetime plt.savefig('img/Lineage' + datetime.datetime.now().strftime('%d.%B.%Y_%I-%M%p') + '.png', dpi=900, bbox_inches='tight') else: pass out6 = widgets.interactive_output( box1, { 'a6': a6, 'a61': a61, 'pos_sub1': pos_sub1, 'pos_sub2': pos_sub2, 'b6': b6, 'c6': c6, 'z6': z6, 'o6': o6, 'o61': o61, 'd6': d6, 'd61': d61, 'g6': g6, 'g61': g61 }) import warnings warnings.filterwarnings("ignore") return display(VBox([yyy, ww, w8, out6]))
def get_color(n): """ https://matplotlib.org/tutorials/colors/colormaps.html """ # return cm.tab20c(n*2) return cm.Set3(n)
def PlotMonthStackBarAndCurveForInformation(self): """近一个月的信息柱状堆叠图+曲线图(双坐标)""" date_list = self.stacked_bar_data['date'].tolist() columns = self.stacked_bar_data.columns.tolist() columns.remove('date') colors = [cm.Set3(i / len(columns)) for i in range(len(columns))] fig, ax = plt.subplots(figsize=(8, 6)) # plot by stacking stacked_bar_data = self.stacked_bar_data[columns].values val = stacked_bar_data[:, 0] p1 = ax.bar(np.arange(len(date_list)), val, width=0.5, color=colors[0], label=columns[0]) sub_matrix = stacked_bar_data[:, 0:1] csum = np.cumsum(sub_matrix, axis=1)[:, -1] csum = csum.flatten() for i in range(1, len(columns)): sub_matrix = stacked_bar_data[:, 0:i] csum = np.cumsum(sub_matrix, axis=1)[:, -1] csum = csum.flatten() val = stacked_bar_data[:, i] p3 = ax.bar( np.arange(len(date_list)), val, width=0.5, bottom=csum, color=colors[i], label=columns[i], ) # tick_label=list(map(ymd2md, date_list))) sub_matrix = stacked_bar_data[:, :] csum = np.cumsum(sub_matrix, axis=1)[:, -1] csum = csum.flatten() # curve ax1 = ax.twinx() res_sum = np.sum(stacked_bar_data, axis=1) res_sum[res_sum == 0] = 1e-7 high_rate = stacked_bar_data[:, 0] / res_sum ax1.plot(np.arange(len(date_list)), high_rate, color='red', label='rate of high', marker='o', markersize=5) # params fontsize = 20 plt.xlabel(u"日期", fontsize=fontsize) for tick in ax.get_xticklabels(): tick.set_rotation(30) fig.legend(loc="upper right", bbox_to_anchor=(1, 1), bbox_transform=ax.transAxes, fontsize=fontsize) plt.title(u"近一个月信息摄入情况", fontsize=fontsize + 5) plt.grid(linestyle='--', alpha=0.9) ax.set_label(columns) ax.tick_params(labelsize=fontsize) ax.set_ylabel(u"时长(分钟)", fontsize=fontsize) ax.set_ylim([0, max(csum) * 1.2]) md_list = list(map(ymd2md, date_list)) ax.set_xticks(range(0, 30, 4)) ax.set_xticklabels(md_list[::4]) ax1.set_label("high rate") ax1.tick_params(labelsize=fontsize) ax1.set_ylabel("%", fontsize=fontsize) ax1.set_ylim([0, 1.2]) plt.savefig(self.fig_path, dpi=150, bbox_inches='tight') plt.close()
if gene not in info: info[gene] = {} info[gene]['names'] = [] info[gene]['counts'] = [] info[gene]['names'] += [line[0]] info[gene]['counts'] += [float(line[1])] xaxis_pos = 1 for gene in info: plt.figure(figsize=(3, 3)) panel1 = plt.axes([0.15, 0.15, 2.25 / 3, 2.25 / 3]) counts = info[gene]['counts'] cm_subsection = np.linspace(0.0, 1.0, len(counts)) colors = [cm.Set3(x) for x in cm_subsection] plt.pie([c/float(sum(counts)) for c in counts], labels=info[gene]['names'], autopct='%1.1f%%', \ shadow=False, colors=colors, pctdistance=0.8) panel1.tick_params(axis='both',which='both', \ bottom='off', labelbottom='off', \ left='off', labelleft='off', \ right='off', labelright='off', \ top='off', labeltop='off') plt.savefig(outfilename + '.' + gene + '.png') # panel1.set_xlim(0,xlim) # panel1.set_xlabel('cat') # fm/ul # panel1.set_xticks(np.arange(1, xlim)) # panel1.set_xticklabels(cats)
data.append(dict_nodal_strenght_region[k][j]) print k plt.subplot(1, 5, i + 1) if (i == 0): plt.ylabel('nodal strength', fontsize=15) ax = sns.boxplot(data=data, palette="Set3", showfliers=False) ## to hide outliers # plt.xticks(plt.xticks()[0],labels,fontsize=10,rotation=40) plt.xticks([]) plt.title('region %s' % (j), fontsize=15) plt.ylim((0, .5)) from matplotlib import cm lab = ['Highs: rh', 'Highs: brh', 'Lows: rh', 'Lows: brh'] colors = [cm.Set3(0), cm.Set3(128), cm.Set3(192), cm.Set3(64)] p = [] for i in range(4): patch = mpatches.Patch(color=colors[i], label=lab[i]) p.append(patch) plt.legend(handles=p) plt.suptitle('Hyp HL dataset Nodal strength by regions', fontsize=20) # plt.savefig('Hyp_HL_nodal_strength_by_region_with_zeros1_legend.pdf') plt.show() ## FIGURE 3 ############ PAPER ENRICA (2nd) plot distance, same than above but colours in node 6,25 (H), 22,30 (L) ############ plt.style.use('default') for label, x, y in zip(channels, [d[0] for d in distance_rr_brr], [d[1] for d in distance_rr_brr]):
def plot(self, text=True): '''Plot visualization of the alignment results using matplotlib. :param text: Control whether text is plotted on top of sequence bars. :type text: bool ''' try: from matplotlib import pyplot from matplotlib import cm except ImportError: raise ImportError('Optional dependency matplotlib not installed.') # Calculations # Reference bar information reference_x = 0 reference_y = 14.25 reference_width = len(self.alignments[0][0]) reference_height = 1 # Bin the features so they don't overlap when plotted features = self._reference.features feature_ranges = [(feature.start, feature.stop) for feature in features] feature_bins = disjoint_bins(feature_ranges) feature_nbin = len(feature_bins) # Bin the alignments so they don't overlap when plotted alignment_bins = disjoint_bins(self.coverage) # Calculate discrepancy coordinates discrepancy_coords = [[], [], []] differences = [self.mismatches, self.insertions, self.deletions] for i, result_bin in enumerate(alignment_bins): for index in result_bin: for j, discrepancy_type in enumerate(differences): for discrepancy in discrepancy_type[index]: coord_y = i coord_x = (discrepancy[0] + discrepancy[1]) // 2 coords = (coord_x, coord_y) discrepancy_coords[j].append(coords) # Plotting # Plot calculations # Controls spacing between bars, size of bars, etc size = 10 # Matplotlib commands # Plot a black reference bar at the bottom fig = pyplot.figure(figsize=(12, 9), dpi=90) sub1 = fig.add_subplot(111) sub1.broken_barh([(reference_x, reference_width)], (reference_y, reference_height), facecolors='black', edgecolors='none') dotted_x = (0, len(self.alignments[0][0])) height = (feature_nbin + 1.5) * size dotted_y = (height, height) sub1.plot(dotted_x, dotted_y, linestyle='dotted') # Plot the reference features on top of the bar # Plot the features by bin: for i, feature_bin in enumerate(feature_bins): for index in feature_bin: feature = features[index] name = feature.name width = feature.stop - feature.start height = size - 1 y_index = (i + 1) * size mid = (feature.start + feature.stop) // 2 pos = float(i) / len(features) sub1.broken_barh([(feature.start, width)], (y_index, height), facecolors=cm.Set3(pos), edgecolors='black') if text: sub1.text(mid, y_index + size / 2, name, rotation=90, size='smaller') # Plot sequencing results by bin for i, result_bin in enumerate(alignment_bins): for index in result_bin: start, stop = self.coverage[index] name = self.names[index] width = stop - start height = size - 1 y_index = (i + feature_nbin + 2) * size text_x = start + (stop - start) // 8 sub1.broken_barh([(start, width)], (y_index, height), facecolors='pink', edgecolors='black') if text: sub1.text(text_x, y_index + size // 3, wrap_name(name), rotation=0, size='smaller') # Plot mismatches, insertions, deletions sub1.plot(1000, 25) shapes = ['o', 'x', 'v'] labels = ['mismatch', 'insertion', 'deletion'] for coords, shape, label in zip(discrepancy_coords, shapes, labels): x_coords = [x[0] for x in coords] y_coords = [(x[1] + feature_nbin + 2) * size + 2 for x in coords] sub1.scatter(x_coords, y_coords, marker=shape, color='k', label=label) sub1.legend() # Plot labeling, etc sub1.set_xlim(0, reference_width) sub1.set_xlabel('Base pairs from origin') sub1.set_yticks([15, 15 + size * (feature_nbin + 1)]) sub1.set_yticklabels(['Reference', 'Results']) sub1.grid(True) sub1.xaxis.grid(False) sub1.yaxis.grid(False) pyplot.title('Alignment gap summary', fontstyle='normal') pyplot.show()
#print("write solution file time %.2f"%(endtime-starttime)) # In[ ]: GRB.Attr #%% # ### Plotting #%% plt.figure(figsize=(10, 8)) #define colors cm.get_cmap('Set3') clr = itertools.cycle(cm.Set3(np.linspace(0, 1, 11))) #plot assignments for j in District_List: #location of district court j d_lon = District_Dict[j][1] d_lat = District_Dict[j][2] if (VarVal(openD_IP[j]) == 1): s_lon = [] s_lat = [] #find settlements assigned to district court j for i in All_S_List: # if (VarVal(d_IP[i][j]) == 1): if (d[i][j] == 1):
def graph_draw(G_p, save=False, show=False, node_colors=[], title="", color='grey'): labels = { 'CW': r"$\medblackcircle$\xspace", 'CS': r"$\medblacktriangleright$\xspace", 'CH': r"$\medblacksquare$\xspace", 'TW': r"$\medcircle$\xspace", 'TS': r"$\medtriangleright$\xspace", 'TH': r"$\medsquare$\xspace" } nucs = ['A', 'U', 'C', 'G', 'L', 'N'] nt_color = {n:i for i,n in enumerate(nucs)} make_label = lambda s: labels[s[:2]] + labels[s[0::2]] if len(set(s[1:])) == 2\ else labels[s[:2]] #place loops in sequence order print(G_p.nodes.data()) loop_labels = {} G_p = nx.relabel_nodes(G_p, loop_labels) G = nx.DiGraph() G.add_edges_from([(*sorted((u,v), key=lambda x:x[1]), d) for u,v,d in G_p.edges.data()]) G = nx.relabel_nodes(G, loop_labels) edge_labels = {(e1, e2): make_label(d['label']) if d['label'] != 'B53' else '' for e1, e2, d in G.edges.data()} pos = circular_layout(G) # pos = nx.spring_layout(G) # if len(node_colors) > 0: # nx.draw_networkx(G, pos, node_size=800, width=2, node_color=node_colors) # else: # nx.draw(G, pos, node_size=800, width=2, node_color='grey') bb = [(u,v) for u,v in G.edges if G[u][v]['label'] == 'B53'] bp = [(u,v) for u,v in G.edges if G[u][v]['label'] != 'B53'] # lr = [(u,v) for u,v in bp if G[u][v]['long_range'] == True] # sr = [(u,v) for u,v in bp if G[u][v]['long_range'] == False] nx.draw_networkx_nodes(G, pos, node_size=500, width=2, node_color=[cm.Set3(nt_color[G_p.nodes.data()[n]['nt']]) for n in G.nodes]) nx.draw_networkx_edges(G, pos, edgelist=bb, arrows=True, width=1.5, arrowsize=25) nx.draw_networkx_edges(G, pos, edgelist=bp, arrows=False, width=2) # nx.draw_networkx_edges(G, pos, edgelist=lr, arrows=False, width=2) nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=25) # nx.draw_networkx_labels(G, pos, labels={n:f"{n[0]}{n[1]}-{G_p.nodes.data()[n]['nt']}" for n in G.nodes()}) nx.draw_networkx_labels(G, pos, labels={n:f"{G_p.nodes.data()[n]['nt']}" for n in G.nodes()}, font_size=20) plt.axis('off') plt.title(title) if save: fig = plt.gcf() fig.set_size_inches(10, 10) plt.savefig(save, format='pdf') # plt.savefig(save.replace('pdf', 'png'), format='png', dpi=300) if show: plt.show() plt.clf()
from sklearn.pipeline import make_union, make_pipeline pipeline = make_pipeline( PolynomialFeatures(degree=2), PCA(n_components=2), ) pipeline.fit(df_train[feature_cols].values, df_train[target_col].values) X_pipeline0 = pipeline.transform(df_valid[df_valid[target_col] == 0][feature_cols].values) X_pipeline1 = pipeline.transform(df_valid[df_valid[target_col] == 1][feature_cols].values) import matplotlib.cm as cm fig, ax = plt.subplots(figsize=(24, 24)) ax.scatter(X_pipeline0[:,0], X_pipeline0[:,1], c=cm.Set3(np.zeros_like(X_pipeline0[:,0])), s=24, lw=0, alpha=0.8, marker='o', label='0') ax.scatter(X_pipeline1[:,0], X_pipeline1[:,1], c=cm.Set3(np.ones_like(X_pipeline0[:,0])), s=24, lw=0, alpha=0.8, marker='o', label='1') ax.legend() fig.savefig(os.path.join(os.getenv('STORING'), 'figure5.png')) tsne_data = np.load(os.path.join(os.getenv('STORING'), 'tsne_2d_5p.npz')) fig, ax = plt.subplots(figsize=(25, 25)) plt.scatter(tsne_data['train'][:,0], tsne_data['train'][:,1], c=df_train['target'], cmap='Set3', alpha=0.8, s=4, lw=0) fig.savefig(os.path.join(os.getenv('STORING'), 'figure6.png')) from sklearn.manifold import Isomap from sklearn.pipeline import make_union, make_pipeline
def plot_top10_barchart( year, df, values, year_var, color_var, names_var, title='', label='', ax=None, **kwargs, ): """ Plots a barchart of the top 10 values colored by group. :param year: year to be plotted (int) :param df: dataset (pandas DataFrame) :param values: sorting variable name (string) :param year_var: year variable name (string) :param color_var: color group variable name (string) :param names_var: bar labels variable name (string) :param title: title (string) :param label: label and unit of the values (string) :param ax: graph axes Gif example of this graph: .. raw:: html <img src="example_top10.gif" height="620px" width="100%"> """ if not isinstance(year, int): raise TypeError(f"year must be an int, not {type(year)}") if not isinstance(df, pd.core.frame.DataFrame): raise TypeError(f"data must be a DataFrame not {type(df)}.") if len(df.shape) != 2: raise ValueError(f"data must be a matrix but shape is {df.shape}") for var in [values, year_var, color_var, names_var]: if var not in df.columns: raise ValueError(f"{var} is not a valid column name.") if not isinstance(title, str): raise TypeError(f"title must be a string not {type(title)}.") if not isinstance(label, str): raise TypeError(f"label must be a string not {type(label)}.") for var in [values, year_var]: if df[var].dtype not in [ 'int16', 'int32', 'int64', 'float16', 'float32', 'float64', ]: raise ValueError(f"{var} must contain numeric values") if df[df[year_var] == year].shape[0] == 0: raise ValueError("404 year not found in dataframe") if len(df[color_var].unique()) > 25: warnings.warn(f"Having too many groups will result in repeated colors") dff = (df[df[year_var] == year].sort_values(by=values, ascending=True).tail(10)) if ax is None: ax = plt.gca() ax.clear() ll = list(dict.fromkeys(df[color_var])) if len(ll) < 8: colors = dict(zip(ll, [cm.Set2(x) for x in linspace(0, 0.87, len(ll))])) else: colors = dict( zip( ll, [cm.Set2(x) for x in linspace(0, 0.87, 8)] + [cm.Set3(x) for x in linspace(0.09, 1, len(ll) - 8)], )) bars = ax.barh( dff[names_var], dff[values], color=[colors[x] for x in dff[color_var]], alpha=0.73, edgecolor='#998D8F', linewidth=1.5, **kwargs, ) dx = dff[values].max() / 200 for i, (value, name) in enumerate(zip(dff[values], dff[names_var])): if len(name) > 0.43 * value / dx: name = name[:int(0.43 * value / dx)] + '..' ax.text(value - dx, i, name, size=16, ha='right', va='center') ax.text(value + dx, i, f'{value:,.0f}', size=14, ha='left', va='center') ax.text( 0.97, 0.4, year, transform=ax.transAxes, color='#FFECEF', size=46, ha='right', weight=800, path_effects=[ path_effects.Stroke(linewidth=3, foreground='black'), path_effects.Normal(), ], ) ax.text(0, 1.06, label, transform=ax.transAxes, size=12, color='#777777') ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}')) ax.xaxis.set_ticks_position('top') ax.tick_params(axis='x', colors='#777777', labelsize=12) ax.set_yticks([]) ax.margins(0, 0.01) ax.grid(which='major', axis='x', linestyle='-') ax.set_axisbelow(True) ax.text(0, 1.1, title, transform=ax.transAxes, size=22, weight=600, ha='left') plt.box(False) color_var_uniq = list(dict.fromkeys(dff[color_var][::-1])) lgd = ax.legend(bars, color_var_uniq, loc=(0.82, 0.03)) for i, j in enumerate(lgd.legendHandles): j.set_color([colors[x] for x in color_var_uniq][i])
def draw_bar(title, labels, first_sizes, other_sizes=[], sub_labels=[], output_path='', show_result=True, y_value=True): ''' 本函数用于绘制柱状图 [title](str): 图标题 [labels](list): X轴主标签列表 [first_sizes](list): 每个主标签的第一个子标签的值组成的列表(不可缺省) [other_sizes](list[list]): 每个主标签的所有其他子标签的值组成的列表(列表的列表)(就算忘了变成列表的列表也不会出bug)(缺省则只画一个子标签) [sub_labels](list): 子标签列表 [output_path](str): 输出的文件路径(含文件名) [show_result](bool): 是否产生直接输出(默认为是) [y_value](bool): 是否显示y值(默认为是) ''' if other_sizes: if type(other_sizes[0]) != type([]): other_sizes = [other_sizes] fig = plt.figure(figsize=(9,6),dpi=400) ax = fig.add_subplot(111) n = len(labels) if len(other_sizes) >=2: div_size = int(len(other_sizes)/1.5) X = np.arange(n*div_size,step=div_size)+1 else: div_size = 1 X = np.arange(n)+1 # print(X) #X是1,2,3,4,5,6,7,8,柱的个数 # numpy.random.uniform(low=0.0, high=1.0, size=None), normal #uniform均匀分布的随机数,normal是正态分布的随机数,0.5-1均匀分布的数,一共有n个 #用来正常显示中文标签 proptease = fm.FontProperties() proptease.set_size(size=13) plt.title(title, fontsize=20, fontproperties=proptease) # plt.box(on=None) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(True) ax.spines['left'].set_visible(False) ax.yaxis.grid() ax.yaxis.set_ticks([]) # ax.yaxis.grid() # axes.set_yticks([]) colors = cm.Set3(X=range(len(other_sizes)+1),alpha=0.7) #print(first_sizes) if len(other_sizes) <= 1: p1 = plt.bar(X,first_sizes,width = 0.35,facecolor = colors[0], edgecolor = 'white',tick_label = labels) tickle_label_place = 0 else: p1 = plt.bar(X,first_sizes,width = 0.35,facecolor = colors[0], edgecolor = 'white') tickle_label_place = int(len(other_sizes)/2) if y_value: for a,b in zip(X,first_sizes): plt.text(a, b+0.1, '%.0f' % b, ha='center', va= 'bottom',fontsize=11/div_size) p = [] #width:柱的宽度 i = 1 for sizes in other_sizes: if i == tickle_label_place: p.append(plt.bar(X+0.35*i,sizes,width = 0.35,facecolor = colors[i],edgecolor = 'white',tick_label = labels)) print(1) else: p.append(plt.bar(X+0.35*i,sizes,width = 0.35,facecolor = colors[i],edgecolor = 'white')) if y_value: for a,b in zip(X+0.35*i,sizes): plt.text(a, b+0.1, '%.0f' % b, ha='center', va= 'bottom',fontsize=11/div_size) i+=1 if sub_labels: plt.legend([p1]+p, labels=sub_labels, bbox_to_anchor=(1.15,1.2), loc="upper right") #水平柱状图plt.barh,属性中宽度width变成了高度height #打两组数据时用+ #facecolor柱状图里填充的颜色 #edgecolor是边框的颜色 #想把一组数据打到下边,在数据前使用负号 #plt.bar(X, -Y2, width=width, facecolor='#ff9999', edgecolor='white') #给图加text n=0 # plt.bar(x, num_list1, width=width, label='girl',tick_label = name_list,fc = 'r') if output_path: plt.savefig(output_path) if show_result: plt.show()