利用散点图探索Netflix的Selling Sunset系列的人物性格

利用散点图探索Netflix的Selling Sunset系列的人物性格

from PIL import Image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import requests
from io import BytesIO

数据探索

以下数据如果有需要的同学可关注公众号HsuHeinrich,回复【数据可视化】自动获取~

df = pd.read_csv("https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/selling_sunset.csv")

# 数据展示
df.head()

image-20240129172017384

Netflix的"Selling Sunset"系列的人物数据:

name:姓名

x/y:手动创建的位置,使代码易于阅读

图片预处理

def open_image(image_name, path):
    '''
    打开指定路径的图片,并将其转为numpy数组存储
    '''
    path_to_image = path + image_name + '?raw=true'
    response = requests.get(path_to_image)
    image = Image.open(BytesIO(response.content))
    image_array = np.array(image)
    
    return image_array
# 将图片存为字典备用
path = 'https://github.com/holtzy/The-Python-Graph-Gallery/blob/master/static/graph_assets/'
image_dict = {
    'Amanza': open_image('Amanza.png', path),
    'Brett': open_image('Brett.png', path),
    'Chelsea': open_image('Chelsea.png', path),
    'Chrishell': open_image('Chrishell.png', path),
    'Christine': open_image('Christine.png', path),
    'Davina': open_image('Davina.png', path),
    'Emma': open_image('Emma.png', path),
    'Heather': open_image('Heather.png', path),
    'Jason': open_image('Jason.png', path),
    'Mary': open_image('Mary.png', path),
    'Maya': open_image('Maya.png', path),
    'Vanessa': open_image('Vanessa.png', path)
}

绘制散点图

# 初始化布局
fig, ax = plt.subplots(figsize=(4, 4))
fig.patch.set_facecolor('black')
ax.set_facecolor('black')

# 迭代每个图片
for key, value in image_dict.items():

    # 图片的x轴与y轴位置
    x_axis = df.loc[df['name']==key, 'x']
    x_axis = float(x_axis.iloc[0]) # 转为float型可以避免出现TypeError
    
    y_axis = df.loc[df['name']==key, 'y']
    y_axis = float(y_axis.iloc[0]) # 转为float型可以避免出现TypeError
    
    # 添加图片
    positions = [x_axis, 
                 y_axis, 
                 0.16, 0.16] # 自定义图片的宽和高
    ax_image = fig.add_axes(positions)

    # 展示图片
    image = image_dict[key]
    ax_image.imshow(image)
    ax_image.axis('off')  # 删除图片的轴
    
    # 给名字加上白色边框
    name = key
    bbox_props = dict(boxstyle="square,pad=0.4", edgecolor="white", facecolor="none")
    ax_image.annotate(name, xy=(0.5, -0.3), xycoords='axes fraction', color='white',
                      fontsize=10, ha="center", bbox=bbox_props)

plt.show()

output_10_0

为图表增加更丰富的信息

# 初始化布局
fig, ax = plt.subplots(figsize=(4, 4))
fig.patch.set_facecolor('black')
ax.set_facecolor('black')

# 绘制水平线
ax.annotate('', xy=(0, -1.3), xycoords='axes fraction', xytext=(0, 1.3),
            arrowprops=dict(arrowstyle='-', color='pink', linewidth=2))

# 绘制垂直线
ax.annotate('', xy=(-1.3, 0), xycoords='axes fraction', xytext=(1.3, 0),
            arrowprops=dict(arrowstyle='-', color='pink', linewidth=2))

# 迭代每个图片
for key, value in image_dict.items():

    # 图片的x轴与y轴位置
    x_axis = df.loc[df['name']==key, 'x']
    x_axis = float(x_axis.iloc[0]) # 转为float型可以避免出现TypeError
    
    y_axis = df.loc[df['name']==key, 'y']
    y_axis = float(y_axis.iloc[0]) # 转为float型可以避免出现TypeError
    
    # 添加图片
    positions = [x_axis, 
                 y_axis, 
                 0.16, 0.16] # 自定义图片的宽和高
    ax_image = fig.add_axes(positions)

    # 展示图片
    image = image_dict[key]
    ax_image.imshow(image)
    ax_image.axis('off')  # 删除图片的轴
    
    # 给名字加上白色边框
    name = key
    bbox_props = dict(boxstyle="square,pad=0.4", edgecolor="white", facecolor="none")
    ax_image.annotate(name, xy=(0.5, -0.3), xycoords='axes fraction', color='white',
                      fontsize=10, ha="center", bbox=bbox_props)

# 给坐标轴增加标签(轴各顶点处)
ax.text(0, 1.4,
         'CONFIDENT',
         fontsize=16, color='white', weight='bold',
         ha='center', va='center')
ax.text(0, -1.4,
         'ANXIOUS',
         fontsize=16, color='white', weight='bold',
         ha='center', va='center')
ax.text(-1.4, 0,
         'BENEVOLENT',
         fontsize=16, color='white', weight='bold',
         ha='center', va='center', rotation=90)
ax.text(1.4, 0,
         'MALICIOUS',
         fontsize=16, color='white', weight='bold',
         ha='center', va='center', rotation=270)

# 添加标题和描述
ax.text(0, 2,
         'Selling Sunset Vibes', # 标题
         fontsize=16, color='white', weight='bold',
         ha='center', va='center')
ax.text(0, 1.8,
         "Peronality analysis of stars from Netflix's Selling Sunset\nBased on chart by @bananapeele", # 描述
         fontsize=14, color='white',
         ha='center', va='center') 

# 添加著作信息
ax.text(1.2, -1.8,
         "@tanya_shapiro",
         fontsize=12, color='white',
         ha='center', va='center')
    
plt.show()

output_12_0

参考:Selling Sunset personality visualization

共勉~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值