Search code examples
pythoncolorsdata-visualizationscatter-plotscatter3d

Python 3D Scatter Plot - Using categorical variable to adjust color


I am trying to make a 3D Scatter Plot for the movie dataset. I want to adjust the color according to the genre of the movie. I have 5 genres and I want to make Comedy red, Action blue, Adventure green etc.

With this code, I can only make yellow and red. How can I make all of them?

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

Solution

  • Define a dictionary where the keys are genres and the values are the colors, and then make colors= [dict[i] for i in list(movies['genre'])]