添加數(shù)據(jù)維度,使用Python繪制5D散點(diǎn)圖
散點(diǎn)圖通常用于比較2個(gè)不同特征以確定它們之間的關(guān)系。散點(diǎn)圖也可以添加更多的維度來反映數(shù)據(jù),例如使用顏色、氣泡大小等。在本文中,將介紹如何繪制一個(gè)五維的散點(diǎn)圖。
數(shù)據(jù)集:https://github.com/checkming00/Medium_datasets/blob/main/WH%20Report_preprocessed.csv
讓我們從二維開始,簡單地看一下Healthy_life_expectancy_at_birth和Log_GDP_per_capita的圖:
df.plot.scatter('Healthy_life_expectancy_at_birth', 'Log_GDP_per_capita')
我們可以看到這2個(gè)特征具有很強(qiáng)的正相關(guān)關(guān)系。然后我們可以將year作為我們的三維視覺效果添加到繪圖中:
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(15, 8))
years = np.sort(df.year.unique())
for i, year in enumerate(years):
BM = df.year == year
X = df[BM]['Healthy_life_expectancy_at_birth']
Y = df[BM]['Log_GDP_per_capita']
plt.subplot(2, 5, i+1) # 2X5 structure of subplots, at i+1 position
plt.scatter(X, Y)
plt.title(year)
plt.xlim([30, 80]) # x axis range
plt.ylim([6, 12]) # y axis range
plt.show()
plt.tight_layout()
它顯示了多年來Healthy_life_expectancy_at_birth和Log_GDP_per_capita之間的關(guān)系。
另一方面,我們可以讓它具有交互性:
def plotyear(year):
BM = df.year == year
X = df[BM]['Healthy_life_expectancy_at_birth']
Y = df[BM]['Log_GDP_per_capita']
plt.scatter(X, Y)
plt.xlabel('Healthy_life_expectancy_at_birth')
plt.ylabel('Log_GDP_per_capita')
plt.xlim([30, 80])
plt.ylim([6, 12])
plt.show()
from ipywidgets import interact, widgets
min_year=df.year.min()
max_year=df.year.max()
interact(plotyear,
year=widgets.IntSlider(min=min_year,
max=max_year, step=1, value=min_year))
然后我們可以拖動(dòng)頂部的控制條來更改年份。
現(xiàn)在讓我們把第四個(gè)維度Continent作為圖例放入:
continents = df.Continent.unique()
con_colors = dict(zip(continents, ['b', 'g', 'r', 'c', 'm', 'y' ,'k']))
import seaborn as sns
def plotyear_continent(year):
BM = df.year == year
sns.scatterplot(data=df[BM], x='Healthy_life_expectancy_at_birth',
y='Log_GDP_per_capita', hue='Continent', palette=con_colors)
plt.xlabel('Healthy_life_expectancy_at_birth')
plt.ylabel('Log_GDP_per_capita')
plt.xlim([30, 80])
plt.ylim([6, 12])
plt.legend()
plt.show()
interact(plotyear_continent,
year=widgets.IntSlider(min=min_year,
max=max_year, step=1,
value=round(df.year.mean(),0)))
它顯示了不同大洲之間的關(guān)系。此時(shí),將默認(rèn)年份設(shè)置為2014年(value=round(df.year.mean(),0))。
我們可以在視覺上做得更多的是氣泡的大小。所以我們可以把population作為第五維:
continents = df.Continent.unique()
con_colors = dict(zip(continents, ['b', 'g', 'r', 'c', 'm', 'y' ,'k']))
min_size=df['population'].min()/1000000 # Scale bubble minimum size
max_size=df['population'].max()/1000000 # Scale bubble maximum size
def plotyear_continent_pop(year):
BM = df.year == year
sns.scatterplot(data=df[BM], x='Healthy_life_expectancy_at_birth',
y='Log_GDP_per_capita', hue='Continent',
palette=con_colors, size='population',
sizes=(min_size, max_size))
plt.xlabel('Healthy_life_expectancy_at_birth')
plt.ylabel('Log_GDP_per_capita')
plt.xlim([30, 80])
plt.ylim([6, 12])
plt.legend()
plt.show()
interact(plotyear_continent_pop,
year=widgets.IntSlider(min=min_year,
max=max_year, step=1,
value=round(df.year.mean(),0)))
它顯示了各大洲與氣泡大小作為人口的關(guān)系。
這就是我們制作5D散點(diǎn)圖的方式。它可以盡可能在同一圖像中告訴人們所需要的信息。