python - Python 3D 散点图 - 使用分类变量调整颜色
问题描述
我正在尝试为电影数据集制作 3D 散点图。我想根据电影的类型调整颜色。我有 5 种类型,我想制作喜剧红色、动作蓝色、冒险绿色等。
使用此代码,我只能制作黄色和红色。我怎样才能制作所有这些?
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib as mpl
import numpy as np
import seaborn as sns
movies = pd.read_csv(r'C:\...\movies.csv', nrows = 400)
fig = plt.figure(figsize=(8, 6))
t = fig.suptitle('Wine Residual Sugar - Alcohol Content - Acidity - Type', fontsize=14)
ax = fig.add_subplot(111, projection='3d')
xs = list(movies['gross'])
ys = list(movies['budget'])
zs = list(movies['score'])
data_points = [(x, y, z) for x, y, z in zip(xs, ys, zs)]
colors = ['red' if gt == 'Comedy' else 'yellow' for gt in list(movies['genre'])]
for data, color in zip(data_points, colors):
x, y, z = data
ax.scatter(x, y, z, alpha=0.4, c=color, edgecolors='none', s=30)
ax.set_xlabel('gross')
ax.set_ylabel('budget')
ax.set_zlabel('score')
解决方案
定义一个字典,其中键是流派,值是颜色,然后制作colors= [dict[i] for i in list(movies['genre'])]
推荐阅读
- c++ - 将 UInt32 / UInt8[16] 转换为 SOCKADDR_STORAGE
- php - 如何使用 implode() 在 PHP 中使用准备好的语句将多个整数插入 IN 语句?
- apache-spark - SQL 查询在 azure databricks 中花费的时间太长
- javascript - 带有 jQuery 和 D3.js 的圆环图插件在事件中需要帮助
- java - 从命令行执行时,Maven 测试未运行
- .net-core - 为什么 System.Text.Json 位于 Microsoft.EntityFrameworkCore.Sqlite?删除后出错 我收到一个错误
- python - 使用 cv2.TM_CCOEFF_NORMED 获得具有多个实例的对象的模板匹配的信心
- c# - 如何用字符串替换 TextBox 中的选定文本?
- android - 键入时 TextInputEditText 未显示键入的文本/数字
- php - 在函数内部调用 PDO 失败