本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
以下文章來源于DataCharm,作者 寧海濤
轉載地址
https://www.zhihu.com/people/qi-shi-huan-hao-la-14/posts
繪制圖示介紹
這篇推文是關于中美貿易的文章配圖(具體我們這里我們不多做介紹,我們只關注配圖的美觀設計),文章的插圖如下:
配圖是真的沒話說,特別是配色(這種配色我后面會做一個Excel的配圖顏色系會比這個多幾個色系,大家如果需求多,我會分享出來的哦)
Python matplotlib
繪圖資料
這里的資料處理步驟暫且不過多介紹,繪圖資料如下
可視化
我們直接上代碼,大家不會的可以詳細看代碼中的注釋:
china_color = ['#3D71A0','#B70050','#FD9717','#B6CBDF'] china_text = ['Aluminum waste and scrap','Pork','Fruits and nuts','Other'] us_color = ['#3D71A0','#B70050','#FD9717','#B6CBDF','#3DAE5E','#133831'] us_list = ['64%', '12%', '10%', '8%', '6%', ''] us_text = ['Aluminum','Long','Pipe/tube','Stainless','Flat',""] barWidth = .6 lefts_china = 0 lefts_us = 0 fig,ax = plt.subplots(figsize=(8,4.5),dpi=200) for bars, col,text,text2 in zip(china['2017 exports, in actual dollars'], china_color,china['percent%'],china_text): ax.barh(1, bars, left=lefts_china, color=col, height=barWidth,align='center') lefts_china += bars #大家注意這里:可以詳細看一下 if lefts_china == china['2017 exports, in actual dollars'][0]: ax.text(bars/2,1,text,color="k",fontweight='extra bold',size=13,ha='center',va='center') ax.text(bars/2,1.4,text2,color="k",fontweight='light',size=8,ha='center',va='center') else: ax.text(lefts_china-bars/2,1,text,color="k",fontweight='extra bold',size=13,ha='center',va='center') ax.text(lefts_china-bars/2,1.4,text2,color="k",fontweight='light',size=8,ha='center',va='center') #添加暗黑色柱形圖 ax.barh(.6,china['2017 exports, in actual dollars'].sum(),color='#172A39',height=.35,zorder=0) for bars, col,text,text2 in zip(us['2017 imports, in actual dollars2'], us_color,us_list,us_text): ax.barh(3, bars, left=lefts_us, color=col, height=barWidth,align='center') lefts_us += bars if lefts_us == us['2017 imports, in actual dollars2'][0]: ax.text(bars/2,3,text,color="k",fontweight='extra bold',size=13,ha='center',va='center') ax.text(bars/2,3.4,text2,color="k",fontweight='light',size=8,ha='center',va='center') else: ax.text(lefts_us-bars/2,3,text,color="k",fontweight='extra bold',size=13,ha='center',va='center') ax.text(lefts_us-bars/2,3.4,text2,color="k",fontweight='light',size=8,ha='center',va='center') ax.barh(2.6,us['2017 imports, in actual dollars2'].sum(),color='#172A39',height=.35,zorder=0) #定制化設定 ax.axis('off') ax.axvline(x=0,ymin=.05,color='k',lw=.8) ax.set_ylim(bottom=0,top=4) #添加文本資訊 ax.text(.02,.95,"Chinese exports covered by US Section 232 tariffs", transform = ax.transAxes,color='k',ha='left',va='center',size=9,fontweight='semibold') ax.text(.3,.64,"$2.8 BILLION EXPORT VALUE,2017", transform = ax.transAxes,color='white',ha='left',va='center',size=8.5,fontweight='semibold') #添加文本資訊 ax.text(.02,.45,"US exports covered by Chinese retaliatory tariffs for US Section 232 tariffs", transform = ax.transAxes,color='k',ha='left',va='center',size=9,fontweight='semibold') ax.text(.2,.14,"$2.4 BILLION EXPORT VALUE,2017", transform = ax.transAxes,color='white',ha='left',va='center',size=8.5,fontweight='semibold') #再單獨添加 semi-finished 描述部分 ax.text(us['2017 imports, in actual dollars2'].sum(),3,"<1%",ha='left',va='center',size=13, fontweight='extra bold') ax.text(us['2017 imports, in actual dollars2'].sum(),3.4,"Semi-finished",ha='left',va='center',size=8, fontweight='light') #添加小橫線 ax.plot([us['2017 imports, in actual dollars2'][:1].sum(),us['2017 imports, in actual dollars2'].sum()],[3.55,3.55], color='k',lw=.6) text_loc = us['2017 imports, in actual dollars2'][:3].sum() ax.text(text_loc,3.64,"STEEL PRODUCTS",ha='center',va='center') #添加刻度文本 ax.text(-.02,.2,"CHINESE\nACTION IN\nEFFECT\nAPRIL 2",ha='right',va='center',fontweight='medium',size=11, transform = ax.transAxes) ax.text(-.02,.7,"US ACTION\nINEFFECT\nMARCH 23",ha='right',va='center',fontweight='medium',size=11, transform = ax.transAxes) #添加標題 ax.text(-.1,1.15,"How Is China Retaliating for US Nation\nSecurity Tariffs on Steel and Aluminum?", transform = ax.transAxes,color='k',ha='left',va='center',size=20,fontweight='extra bold') ax.text(.91,.05,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 7,color='black') plt.savefig(r'F:\DataCharm\商業藝術圖表仿制\PIEE_Charts\China US\PIEE_china_us02.png',width=8,height=4, dpi=900,bbox_inches='tight',facecolor='white') #ax.set_axisbelow(True) plt.show()
最終我們繪制的效果如下:
知識點
- 橫向堆積柱形圖繪制 這里舉一個小栗子大家看下就可以:
from matplotlib import pyplot as plt import numpy as np N = 5 r = range(N) bars1 = np.random.binomial(20, .7, N) bars2 = np.random.binomial(20, .5, N) bars3 = np.random.binomial(20, .4, N) colors = ['#7f6d5f', '#557f2d', '#2d7f5e'] labels = ["Lasso", "Random Forest", "Decision Tree"] barWidth = 1 lefts = 0 fig,ax = plt.subplots(figsize=(8,4.5),dpi=200) for bars, col, label in zip([bars1, bars2, bars3], colors, labels): plt.barh(r, bars, left=lefts, color=col, edgecolor='white', height=barWidth, label=label) lefts += bars plt.legend() plt.ylim(-0.5, len(bars) - 0.5) ax.text(.91,-.08,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 7,color='black') plt.savefig(r'F:\DataCharm\商業藝術圖表仿制\PIEE_Charts\China US\PIEE_china_example.png',width=8,height=4, dpi=900,bbox_inches='tight') plt.show()
結果如下:
- 文本自動添加 雖然代碼中有明確解釋,但這里我們還是單獨進行展示:
for bars, col,text,text2 in zip(us['2017 imports, in actual dollars2'], us_color,us_list,us_text): ax.barh(3, bars, left=lefts_us, color=col, height=barWidth,align='center') lefts_us += bars if lefts_us == us['2017 imports, in actual dollars2'][0]: ax.text(bars/2,3,text,color="k",fontweight='extra bold',size=13,ha='center',va='center') ax.text(bars/2,3.4,text2,color="k",fontweight='light',size=8,ha='center',va='center') else: ax.text(lefts_us-bars/2,3,text,color="k",fontweight='extra bold',size=13,ha='center',va='center') ax.text(lefts_us-bars/2,3.4,text2,color="k",fontweight='light',size=8,ha='center',va='center')
注意里面的if條件陳述句設定哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/159840.html
標籤:Python
上一篇:教你使用Python爬蟲獲取電子書資源實戰!喜歡學習的小伙伴過來看啦!
下一篇:Python能做什么作業?
