Showing information in scatterplot using a color spectrum

68 views Asked by At
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# Given data
x_values = np.array([1, 2, 3, 4, 5, 6])
y_values = np.array([4, 6, 3, 5, 7, 6])
information = np.array([
    [0.3, 0.5, 0.1, 0.1, 0.2, 0.1],
    [0.1, 0.2, 0.1, 0.1, 0.1, 0.5],
    [0.5, 0.1, 0.1, 0.1, 0.5, 0.1],
    [0.1, 0.1, 0.2, 0.5, 0.1, 0.1],
    [0.0, 0.1, 0.5, 0.2, 0.1, 0.2]])

I have x and y values and some information to show using color on a scatter plot. Each of the column from the information array represents 5 parameters for each (x,y) point. I want to show the participation of those parameters using a spectrum of color. How can I do it?

1

There are 1 answers

1
gboffi On

I think this is a reasonable solution, using mpl_toolkits.mplot3d.axes3d.Axes3D.bar3d

enter image description here

As you can see, there is a nasty artifact..., further there is (so it seems) a Matplotlib bug that prevents adding a legend, that is sorely needed (see this question of mine). I've added the sorely needed legend using the info Trenton McKinney provided in this comment — thank you Trenton.

Here it's the code, the only trick is how the bases array is computed

import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([4, 6, 3, 5, 7, 6])
informations = np.array([
    [0.3, 0.5, 0.1, 0.1, 0.2, 0.1],
    [0.1, 0.2, 0.1, 0.1, 0.1, 0.5],
    [0.5, 0.1, 0.1, 0.1, 0.5, 0.1],
    [0.1, 0.1, 0.2, 0.5, 0.1, 0.1],
    [0.0, 0.1, 0.5, 0.2, 0.1, 0.2]])

bases = np.vstack(([0]*len(x), informations.cumsum(axis=0)))[:-1]

fig = plt.figure(figsize=(8, 6))
ax1 = fig.add_subplot(111, projection='3d')

for n, (base, info) in enumerate(zip(bases, informations)):
    ax1.bar3d(x-0.5, y-0.5, base, 1, 1, info, label=str(n), shade=True)

plt.xlabel('X')
plt.ylabel('Y')

color = plt.rcParams['axes.prop_cycle']()
proxies = [plt.Rectangle((0, 0), 1, 1, **next(color)) for _ in informations[:,0]]
labels = 'Apricots Bananas Cherries Dates Elderberries'.split()
ax1.legend(proxies, labels, bbox_to_anchor=(1.05, 0.50), loc='center left', frameon=0)

plt.show()