0

Pretty simple problem really, but I have spent hours with different approaches that do not work. What I have is a 3D scatterplot that I want to colour code according to a 4th column in matplotlib.

Tried many 2D options posted online.

import pandas as pd
import numpy as np
from pandas import DataFrame
import matplotlib.pyplot as plt
df = pd.read_csv('./scatterplot_hhld.csv')
print(df.head())

  Variable  hhld premium  Amenity value  hhld balance
0     RCP0      0.445913       0.598477      0.718463
1    RCP26      0.416538       0.558254      0.716983
2    RCP45      0.414974       0.527492      0.721607
3     RCP6      0.422314       0.534424      0.716631
4    RCP85      0.419975       0.521947      0.718388

threedee = plt.figure().gca(projection='3d')

colours = np.where(df['Variable']=='RCP0','g','-')
colours[df['Variable']=='RCP26'] = 'b'
colours[df['Variable']=='RCP45'] = 'y'
colours[df['Variable']=='RCP6'] = 'o'
colours[df['Variable']=='RCP85'] = 'r'
colours[df['Variable']=='RCP85_bonds'] = 'r'

threedee.scatter(df['hhld premium'], df['Amenity value'], df['hhld    balance'], c = colours)
threedee.set_xlabel('hhld premium')
threedee.set_ylabel('Amenity value')
threedee.set_zlabel('hhld balance')
plt.show()

It plots the values just fine. However, when I try to colour I get: "ValueError: c of shape (6,) not acceptable as a color sequence for x with size 6, y with size 6". I just want to define each row in the df by colour. There should be a really simple fix that I am just not getting. Any help is much appreciated.

Ashton
  • 27
  • 2

0 Answers0