輕松玩轉(zhuǎn)Python,五個(gè)步驟打造驚艷的折線圖
1、簡(jiǎn)介
圖片
最富有的5個(gè)國(guó)家的GDP隨時(shí)間的演變
Matplotlib可以快速輕松地使用現(xiàn)成的函數(shù)繪制圖表,但是微調(diào)步驟需要花費(fèi)更多精力。今天就來介紹如何使用Matplotlib繪制吸引人的圖表。
本文詳細(xì)介紹如何從下面這張圖↓
圖片
優(yōu)化為下面這張圖↓
圖片
2、數(shù)據(jù)
為了說明方法,本文使用了包含過去50年各國(guó)GDP信息的公開數(shù)據(jù)集:
來源:世界銀行國(guó)民賬戶數(shù)據(jù)和OECD(經(jīng)濟(jì)合作與發(fā)展組織)國(guó)民賬戶數(shù)據(jù)文件。
許可證URL:https://datacatalog.worldbank.org/public-licenses#cc-by
導(dǎo)入必要的軟件包、讀取數(shù)據(jù)、繪制圖表,對(duì)2022年的GDP前20個(gè)國(guó)家進(jìn)行篩選:
import pandas as pd
import matplotlib.pyplot as plt
from datetime import timedelta
# 讀取數(shù)據(jù)
df = pd.read_csv('88a1e584-0a94-4e73-b650-749332831ef4_Data.csv', sep=',')
df.drop(['Series Name', 'Series Code', 'Country Code'], axis=1, inplace=True)
df = df.dropna(subset=['Country Name'])
# 對(duì) 2022 年最富有的 20 個(gè)國(guó)家進(jìn)行篩選
top_20_countries = df[df['Year'] == '2022-01-01'].sort_values('GDP', ascending = False).head(20)['Country Name'].tolist()
df = df[df['Country Name'].isin(top_20_countries)].reset_index(drop = True)
df.head()
3、基本圖
首先,只需四行代碼就足以創(chuàng)建圖形,并循環(huán)遍歷各國(guó)以繪制它們各自的折線:
# 創(chuàng)建圖形和坐標(biāo)軸對(duì)象,指定尺寸和DPI
fig, ax = plt.subplots(figsize=(13.33,7.5), dpi = 96)
# 繪制折線
for country in top_20_countries:
data = df[df['Country Name'] == country]
line = ax.plot(data['Year'], data['GDP'], label=country)
最基本的Matplotlib折線圖
4、基本要素
接下來向圖表中添加一些關(guān)鍵內(nèi)容,使其更易于觀眾閱讀。
- 網(wǎng)格
為了提高圖表的可讀性,網(wǎng)格是必不可少的。將網(wǎng)格的透明度設(shè)置為0.5,這樣它們就不會(huì)對(duì)數(shù)據(jù)點(diǎn)造成太大干擾。
- X軸和Y軸重新格式化
為了更全面地了解微調(diào)的可能性,本文故意添加了更多的參數(shù)。例如,X軸不需要major_formatter 和major_locator對(duì)象,因?yàn)楸疚闹伙@示年份,但如果讀者的X軸包含其他數(shù)字,這就會(huì)派上用場(chǎng)。
- 圖例
由于要顯示很多條線,因此添加標(biāo)簽和圖例非常重要,這樣讀者就能知道哪條線是哪條線。
# 添加圖例
ax.legend(loc="best", fnotallow=8)
# 創(chuàng)建網(wǎng)格
ax.grid(which="major", axis='x', color='#DAD8D7', alpha=0.5, zorder=1)
ax.grid(which="major", axis='y', color='#DAD8D7', alpha=0.5, zorder=1)
# 重新格式化x軸標(biāo)簽和刻度線標(biāo)簽
ax.set_xlabel('', fnotallow=12, labelpad=10) # 不需要軸標(biāo)簽
ax.xaxis.set_label_position("bottom")
#ax.xaxis.set_major_formatter(lambda s, i : f'{s:,.0f}') #以防萬一我們需要額外的格式設(shè)置
#ax.xaxis.set_major_locator(MaxNLocator(integer=True)) #以防我們需要額外的格式化
ax.xaxis.set_tick_params(pad=2, labelbottom=True, bottom=True, labelsize=12, labelrotatinotallow=0)
# 重新格式化y軸
ax.set_ylabel('GDP (Billions USD)', fnotallow=12, labelpad=10)
ax.yaxis.set_label_position("left")
ax.yaxis.set_major_formatter(lambda s, i : f'{s*10**-9:,.0f}')
#ax.yaxis.set_major_locator(MaxNLocator(integer=True)) #以防我們需要額外的格式化
ax.yaxis.set_tick_params(pad=2, labeltop=False, labelbottom=True, bottom=False, labelsize=12)
圖片
為本文的圖表添加一些必要的功能
5、突出重點(diǎn)
接下來,突出顯示最富有的五個(gè)國(guó)家,并跟蹤其GDP隨時(shí)間的變化。在字典中定義了特定的顏色和線條樣式,并對(duì)代碼稍作修改,以單獨(dú)繪制它們。
# 顏色和線條樣式
colors_dict = {'United States': '#014f86', 'China': '#DC0000', 'Japan': '#ff4d6d', 'Germany': '#403d39', 'India': '#6a994e'}
line_styles_dict = {'United States': '-', 'China': '-', 'Japan': '-', 'Germany': '-', 'India': '-'}
# 繪制前5條線
for country in top_20_countries[:5]:
color = colors_dict.get(country, 'grey') # 從字典中獲取顏色,如果找不到,默認(rèn)為灰色
line_style = line_styles_dict.get(country, '-') # 從字典中獲取線條樣式,如果未找到,默認(rèn)為實(shí)線
data = df[df['Country Name'] == country]
line = ax.plot(data['Year'], data['GDP'], color=color, linestyle=line_style, zorder=2, label=country)
# 添加圖例
ax.legend(loc="best", fnotallow=8)
# 繪制剩余部分
for country in top_20_countries[5:]:
data = df[df['Country Name'] == country]
line = ax.plot(data['Year'], data['GDP'], color='grey', linestyle=':', linewidth=0.5, zorder=2)
圖片
仍然是相同的折線圖,但故事更清晰了
6、修改外觀
為本文的圖表添加一些功能,可以使其看起來更加專業(yè)。它們將位于所有圖表的頂部,并且與本文中使用的數(shù)據(jù)無關(guān)。
通過下面的代碼片段,這些調(diào)整將很容易實(shí)現(xiàn)。
讀者可以根據(jù)自己的需求對(duì)其進(jìn)行調(diào)整,以創(chuàng)建自己的視覺風(fēng)格。
- 邊框
邊框是圖表周圍可見的框。除了左邊的邊框會(huì)設(shè)置得稍微粗一些外,其余的邊框都將被移除。
- 頂部的紅線和矩形
在標(biāo)題上方添加一條紅線和一個(gè)矩形,以便將圖表與上方的文本很好地隔離開來。
- 標(biāo)題和副標(biāo)題
添加標(biāo)題來介紹圖表,副標(biāo)題可以用來進(jìn)一步解釋內(nèi)容,甚至呈現(xiàn)初步的結(jié)論。
- 來源
在所有制作的圖表中都必不可少的一項(xiàng)。
- 調(diào)整邊距
調(diào)整圖表區(qū)域周圍的邊距,以確保充分利用所有可用空間。
- 設(shè)置白色背景
將背景設(shè)置為白色(默認(rèn)為透明)在通過電子郵件、Teams或任何其他工具發(fā)送圖表時(shí)非常有用,因?yàn)橥该鞅尘翱赡軙?huì)造成問題。
# 移除邊框
ax.spines[['top','right','bottom']].set_visible(False)
# 加粗左側(cè)邊框
ax.spines['left'].set_linewidth(1.1)
# 在頂部添加紅線和矩形
ax.plot([0.05, .9], [.98, .98], transform=fig.transFigure, clip_notallow=False, color='#E3120B', linewidth=.6)
ax.add_patch(plt.Rectangle((0.05,.98), 0.04, -0.02, facecolor='#E3120B', transform=fig.transFigure, clip_notallow=False, linewidth = 0))
# 添加標(biāo)題和副標(biāo)題
ax.text(x=0.05, y=.93, s="Evolution of the 20 Richest Countries GDP over the Past 50 Years", transform=fig.transFigure, ha='left', fnotallow=14, weight='bold', alpha=.8)
ax.text(x=0.05, y=.90, s="Focus on the current 5 richest countries from 1973 to 2022", transform=fig.transFigure, ha='left', fnotallow=12, alpha=.8)
# 設(shè)置來源文本
ax.text(x=0.05, y=0.12, s="Source: World Bank - https://databank.worldbank.org/", transform=fig.transFigure, ha='left', fnotallow=10, alpha=.7)
# 調(diào)整繪圖區(qū)域周圍的邊距
plt.subplots_adjust(left=None, bottom=0.2, right=None, top=0.85, wspace=None, hspace=None)
# 設(shè)置白色背景
fig.patch.set_facecolor('white')
本文的視覺風(fēng)格應(yīng)用于圖表,使其更加整潔
7、點(diǎn)睛之筆
為了得到在文章開頭介紹的最終結(jié)果,剩下要做的就是實(shí)現(xiàn)這幾個(gè)額外的組件:
- 終點(diǎn)標(biāo)記
這些元素純粹是為了美觀,但能為本文的折線圖增添一份亮點(diǎn)。用標(biāo)記突出顯示每條折線的最后一個(gè)點(diǎn),使其更加醒目。
- 注釋
借助annotate方法,可以突出顯示圖表中的特定點(diǎn),并在其上直接添加注釋。
# 繪制前5條線
for country in top_20_countries[:5]:
color = colors_dict.get(country, 'grey') # 從字典中獲取顏色,如果找不到,默認(rèn)為黑色
line_style = line_styles_dict.get(country, '-') # 從字典中獲取線條樣式,如果找不到,默認(rèn)為實(shí)線
data = df[df['Country Name'] == country]
line = ax.plot(data['Year'], data['GDP'], color=color, linestyle=line_style, zorder=2, label = country)
ax.plot(data['Year'].iloc[-1], data['GDP'].iloc[-1], 'o', color=color, markersize=10, alpha=0.3)
ax.plot(data['Year'].iloc[-1], data['GDP'].iloc[-1], 'o', color=color, markersize=5)
# 在圖表上添加一些文字
ax.annotate('During the 2000s,\nChina began experiencing rapid economic growth,\noutpacing all other countries.',
(data['Year'].iloc[-18], 2000000000000),
xytext=(data['Year'].iloc[-28]-timedelta(days=500), 18000000000000),
ha='left', fnotallow=9, arrowprops=dict(arrowstyle='-|>', facecolor='k', cnotallow="arc3,rad=-0.15"))
圖片
最終成果:這個(gè)折線圖清晰易讀
8、結(jié)語(yǔ)
本文分享了使用Matplotlib繪制折線圖的知識(shí),同時(shí)提供了實(shí)用可重復(fù)使用的代碼片段。