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[1]
共勉~
[1]
Selling Sunset personality visualization: https://python-graph-gallery.com/web-scatterplot-with-images-in-circles/