日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
輕松玩轉(zhuǎn)Python,五個步驟打造驚艷的折線圖

1、簡介

圖片

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的威寧網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

最富有的5個國家的GDP隨時間的演變

Matplotlib可以快速輕松地使用現(xiàn)成的函數(shù)繪制圖表,但是微調(diào)步驟需要花費更多精力。今天就來介紹如何使用Matplotlib繪制吸引人的圖表。

本文詳細介紹如何從下面這張圖↓

圖片

優(yōu)化為下面這張圖↓

圖片

2、數(shù)據(jù)

為了說明方法,本文使用了包含過去50年各國GDP信息的公開數(shù)據(jù)集:

來源:世界銀行國民賬戶數(shù)據(jù)和OECD(經(jīng)濟合作與發(fā)展組織)國民賬戶數(shù)據(jù)文件。

許可證URL:https://datacatalog.worldbank.org/public-licenses#cc-by

導入必要的軟件包、讀取數(shù)據(jù)、繪制圖表,對2022年的GDP前20個國家進行篩選:

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'])

# 對 2022 年最富有的 20 個國家進行篩選
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)遍歷各國以繪制它們各自的折線:

# 創(chuàng)建圖形和坐標軸對象,指定尺寸和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,這樣它們就不會對數(shù)據(jù)點造成太大干擾。

  • X軸和Y軸重新格式化

為了更全面地了解微調(diào)的可能性,本文故意添加了更多的參數(shù)。例如,X軸不需要major_formatter 和major_locator對象,因為本文只顯示年份,但如果讀者的X軸包含其他數(shù)字,這就會派上用場。

  • 圖例

由于要顯示很多條線,因此添加標簽和圖例非常重要,這樣讀者就能知道哪條線是哪條線。

# 添加圖例
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軸標簽和刻度線標簽
ax.set_xlabel('', fnotallow=12, labelpad=10) # 不需要軸標簽
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、突出重點

接下來,突出顯示最富有的五個國家,并跟蹤其GDP隨時間的變化。在字典中定義了特定的顏色和線條樣式,并對代碼稍作修改,以單獨繪制它們。

# 顏色和線條樣式
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')  # 從字典中獲取顏色,如果找不到,默認為灰色
    line_style = line_styles_dict.get(country, '-')  # 從字典中獲取線條樣式,如果未找到,默認為實線
    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)整將很容易實現(xiàn)。

讀者可以根據(jù)自己的需求對其進行調(diào)整,以創(chuàng)建自己的視覺風格。

  • 邊框

邊框是圖表周圍可見的框。除了左邊的邊框會設(shè)置得稍微粗一些外,其余的邊框都將被移除。

  • 頂部的紅線和矩形

在標題上方添加一條紅線和一個矩形,以便將圖表與上方的文本很好地隔離開來。

  • 標題和副標題

添加標題來介紹圖表,副標題可以用來進一步解釋內(nèi)容,甚至呈現(xiàn)初步的結(jié)論。

  • 來源

在所有制作的圖表中都必不可少的一項。

  • 調(diào)整邊距

調(diào)整圖表區(qū)域周圍的邊距,以確保充分利用所有可用空間。

  • 設(shè)置白色背景

將背景設(shè)置為白色(默認為透明)在通過電子郵件、Teams或任何其他工具發(fā)送圖表時非常有用,因為透明背景可能會造成問題。

# 移除邊框
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))

# 添加標題和副標題
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')

本文的視覺風格應(yīng)用于圖表,使其更加整潔

7、點睛之筆

為了得到在文章開頭介紹的最終結(jié)果,剩下要做的就是實現(xiàn)這幾個額外的組件:

  • 終點標記

這些元素純粹是為了美觀,但能為本文的折線圖增添一份亮點。用標記突出顯示每條折線的最后一個點,使其更加醒目。

  • 注釋

借助annotate方法,可以突出顯示圖表中的特定點,并在其上直接添加注釋。

# 繪制前5條線
for country in top_20_countries[:5]:
    color = colors_dict.get(country, 'grey')  # 從字典中獲取顏色,如果找不到,默認為黑色
    line_style = line_styles_dict.get(country, '-')  # 從字典中獲取線條樣式,如果找不到,默認為實線
    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"))

圖片

最終成果:這個折線圖清晰易讀

8、結(jié)語

本文分享了使用Matplotlib繪制折線圖的知識,同時提供了實用可重復(fù)使用的代碼片段。


網(wǎng)頁標題:輕松玩轉(zhuǎn)Python,五個步驟打造驚艷的折線圖
本文鏈接:http://www.5511xx.com/article/ccsojdj.html