前言本文的文字及圖片來(lái)源于網(wǎng)絡(luò),僅供學(xué)習(xí),、交流使用,不具有任何商業(yè)用途,版權(quán)歸原作者所有,如有問(wèn)題請(qǐng)及時(shí)聯(lián)系我們以作處理 以下文章來(lái)源于Lin王發(fā)林,,作者:WangFalin Python數(shù)據(jù)分析:零基礎(chǔ)入門教學(xué)(講解+實(shí)戰(zhàn)) https://www.bilibili.com/video/BV18f4y1i7q9/ 前言上個(gè)月的時(shí)候看到一張很漂亮的商業(yè)圖,很喜歡,,然后就忘了,。剛好前兩天看到一篇文章來(lái)臨摹此圖,,于是學(xué)習(xí)了一下其思路和代碼,,然后拿來(lái)實(shí)踐了一下,效果還可以,,特此紀(jì)念,,以后應(yīng)該還有用得上的地方。那商業(yè)圖我就不放了,,然后把我的圖放在這里↓ 數(shù)據(jù)準(zhǔn)備可以看到,,圖中主要有4列數(shù)據(jù)組成,分別是公司logo,、公司名稱,、所屬工具和市值增長(zhǎng)值。于是先準(zhǔn)備數(shù)據(jù),,就是我常用的軟件工具列舉了一下,,共20個(gè),然后數(shù)值是使用率吧,,Type是用來(lái)標(biāo)記顏色的,,最后一列分類是次軟件的主要作用,&符號(hào)連接兩個(gè)或多個(gè)主要用途,。如下↓
設(shè)置中文字體正常顯示 mycolor = { 'Green': '#8ABD25', 'Pink': '#F57FEF', 'Yellow': '#EBE639', 'Red': '#EB3939', 'Orange': '#EBAF39', 'Blue': '#39A4EB', 'Black': '#4D6E83', 'Gray': '#A3A4A5',} 自定義顏色,,后面直接根據(jù)Type類型進(jìn)行調(diào)用就行了
一些影響的參數(shù),,因?yàn)樯婕岸嗵?,所以提出?lái)統(tǒng)一修改了,還有一些參數(shù)需要里面改,。 def create_fill_area(row): # 初始化包圍填充區(qū)域的上下線條y坐標(biāo) line1, line2 = [1 - ratio*row, 1 - ratio*row], [1- ratio*(row+1), 1- ratio*(row+1)] # 追加陰影段y坐標(biāo) line1.append(ratio4 + (right_height - row) * (ratio2 - ratio4) / right_height) line2.append(ratio4 + (right_height - row - 1) * (ratio2 - ratio4) / right_height) # 追加最后一段平行段y坐標(biāo) line1.append(ratio4 + (right_height - row) * (ratio2 - ratio4) / right_height) line2.append(ratio4 + (right_height - row - 1) * (ratio2 - ratio4) / right_height) return line1, line2 為了創(chuàng)建出不同條帶,,配合matplotlib中的fill_between。為了處理好左側(cè)與右側(cè)的豎直方向等分區(qū)域,,我們可以在對(duì)原數(shù)據(jù)每一行循環(huán)的過(guò)程中,,自定義下列函數(shù)來(lái)計(jì)算區(qū)域范圍↓
下面是繪圖代碼,,都有注釋說(shuō)明,讀一遍應(yīng)該都能讀懂,,只是一些巧妙計(jì)算的邏輯需要理一下,。這里沒(méi)有繪制標(biāo)題,,可以借助PS添加一個(gè)完美的標(biāo)題,。 fig, ax = plt.subplots(figsize=(4.8, 6))ax.set_xlim(0, 1.11)ax.set_ylim(0, 1)for row in range(mydata.shape[0]): # 定義區(qū)域填充對(duì)應(yīng)的x坐標(biāo) x = [0, 0.15, 0.215, 0.6+mydata.at[row, 'Values'] / 1000] # 生成區(qū)域填充對(duì)應(yīng)的y坐標(biāo) line1, line2 = create_fill_area(row) # 對(duì)指定區(qū)域進(jìn)行填充 ax.fill_between(x, line1, line2, color=mycolor[mydata.at[row, 'Type']], edgecolor='none') # 從logo文件夾下讀取對(duì)應(yīng)logo圖片 try: logo = plt.imread(f'logo/{mydata.at[row, 'Tools']}.png') except FileNotFoundError: logo = plt.imread(f'logo/{mydata.at[row, 'Tools']}.jpg') # 插入軟件logo ax_logo = ax.inset_axes((0.05, 1 - ratio*(row+1)+0.005, 0.08, logosize)) ax_logo.imshow(logo) ax_logo.axis('off') ax_logo.set_facecolor(mycolor[mydata.at[row, 'Type']]) # 處理單個(gè)及多個(gè)功能情況下的繪制 for idx, Category in enumerate(mydata.at[row, 'Category'].split('&')[::-1]): # 讀取對(duì)應(yīng)功能圖片 flag = plt.imread(f'flag/{Category}.png') # 插入功能子圖 ax_flag = ax.inset_axes((0.545-idx*0.06, 0.013+(right_height - row - 1)*((ratio2 - ratio3) / right_height), 0.1, 0.025)) ax_flag.imshow(flag) ax_flag.axis('off') ax_flag.set_facecolor(mycolor[mydata.at[row, 'Type']]) # 繪制排名 ax.text(0.025, (1 - ratio*row + 1 - ratio*(row+1)) / 2, str(row+1), ha='center', va='center', fontsize=9, color='black') # 繪制軟件名稱 ax.text(0.215+ratio5, 0.5 * (ratio5 + (right_height - row - 1) * (ratio2 - ratio5) / right_height + ratio5 + (right_height - row) * (ratio2 - ratio5) / right_height), mydata.at[row, 'Tools'], ha='left', va='center', fontsize=9, color='#FFFFFF', weight='bold') # 處理第一名文字在填充區(qū)域內(nèi)部,其余文字在填充區(qū)域外的情況 if mydata.at[row, 'Tools'] == 'Exce1l': ax.text(1, 0.5 * (ratio3 + (right_height - row) * (ratio2 - ratio3) / right_height + ratio3 + (right_height - row - 1) * (ratio2 - ratio3) / right_height)-0.0025, ''+str(mydata.at[row, 'Values']/4)+'%', color='white', fontsize=10, ha='right', va='center', weight='bold') else: # 配合歸一化對(duì)字體進(jìn)行大小映射 ax.text(0.6+mydata.at[row, 'Values'] / 1000 + ratio3, 0.5 * (ratio3 + (right_height - row) * (ratio2 - ratio3) / right_height + ratio3 + (right_height - row - 1) * (ratio2 - ratio3) / right_height)-0.0025, ''+str(int(mydata.at[row, 'Values']/4))+'%', color=mycolor[mydata.at[row, 'Type']], fontsize=7+((mydata.at[row, 'Values'] - mydata['Values'].min()) / (mydata['Values'].max() - mydata['Values'].min())) * 5, ha='left', va='center', weight='bold')# 對(duì)指定區(qū)域進(jìn)行帶透明度的黑色蒙版,,以達(dá)到陰影效果ax.fill_between([0.15, 0.215], [0, ratio4], [1, ratio2], color='black', alpha=0.2, # 設(shè)置透明度 edgecolor='none')# 補(bǔ)充其余文字標(biāo)注ax.text(0.215+ratio5, 0.805, '軟件名稱', color='#565555', fontsize=6, ha='left')ax.text(0.67, 0.805, '軟件類型', color='#565555', fontsize=6, ha='center')#補(bǔ)充上方數(shù)值刻度ax.text(0.6, 0.825, '0', color='#a9a8a8', fontsize=8, ha='center') for i in range(1, 5): print(i) ax.text(0.6+0.1*i, 0.825, f'{i*25}%', color='#a9a8a8', fontsize=9, ha='center') ax.vlines(0.6+0.1*i, 0.01, 0.82, color='#dcdcdb', linewidth=0.2)ax.set_xticks([])ax.set_yticks([])ax.spines['left'].set_color('none')ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')ax.spines['bottom'].set_color('none')# 補(bǔ)充下排圖例ax_bar1 = ax.inset_axes((0.215, 0.88, 0.57, 0.02), transform=ax.transAxes)ax_bar1.set_xlim(-0.45, 3.6)ax_bar1.bar(range(4), height=1, width=0.8, color=['#8ABD25', '#F57FEF', '#EBE639', '#EB3939'])ax_bar1.set_xticks(range(4))ax_bar1.set_xticklabels(['綠色系', '紫色系', '黃色系', '紅色系'], fontsize=7, color='#4f4e4e', weight='bold')ax_bar1.set_yticks([])ax_bar1.spines['left'].set_color('none')ax_bar1.spines['right'].set_color('none')ax_bar1.spines['top'].set_color('none')ax_bar1.spines['bottom'].set_color('none')ax_bar1.tick_params(color='none', pad=-2)ax_bar1.set_facecolor('#f8f8f8')# 補(bǔ)充上排圖例ax_bar2 = ax.inset_axes((0.215, 0.98, 0.57, 0.02), transform=ax.transAxes)ax_bar2.set_xlim(-0.45, 3.6)ax_bar2.bar(range(4), height=1, width=0.8, color=['#EBAF39', '#39A4EB', '#4D6E83', '#A3A4A5'])ax_bar2.set_xticks(range(4))ax_bar2.set_xticklabels(['橙色系', '藍(lán)色系', '黑色系', '灰色系'], fontsize=7, color='#4f4e4e', weight='bold')ax_bar2.set_yticks([])ax_bar2.spines['left'].set_color('none')ax_bar2.spines['right'].set_color('none')ax_bar2.spines['top'].set_color('none')ax_bar2.spines['bottom'].set_color('none')ax_bar2.tick_params(color='none', pad=-2)ax_bar2.set_facecolor('#f8f8f8')ax.set_facecolor('#f8f8f8')fig.set_facecolor('#f8f8f8')fig.savefig('輸出結(jié)果.png', dpi=800, bbox_inches='tight') 這個(gè)顏色有點(diǎn)艷麗,,可以調(diào)整一下顏色就行了,然后用PS加個(gè)標(biāo)題,,底部再加點(diǎn)看不懂的小文字顯得高端,,然后基本上就大功告成了。 |
|
來(lái)自: 新用戶18985384 > 《Python》