0

I have data in this format:

from itertools import permutations
import random

values = [val for val in range(5)]
xy_coordinate_pairs = list(permutations(values, 2))
z_values = [random.randint(0,20) for val in range(len(xy_coordinate_pairs))]
data = dict(zip(xy_coordinate_pairs,z_values))

{(0, 1): 4,
 (0, 2): 20,
 (0, 3): 16,
 (0, 4): 12,
 (1, 0): 6,
 (1, 2): 3,
 (1, 3): 6,
 (1, 4): 16,
 (2, 0): 19,
 (2, 1): 17,
 (2, 3): 17,
 (2, 4): 11,
 (3, 0): 18,
 (3, 1): 13,
 (3, 2): 11,
 (3, 4): 20,
 (4, 0): 20,
 (4, 1): 19,
 (4, 2): 6,
 (4, 3): 0}

And I'd like to plot this as a 3d surface plot, where the x and y coordinates are the first and second value respectively in the key tuples, and the z (height of surface plot) are the dictionary values. How would I go about doing this?

Thanks so much and have a great day.

dcoolwater07
  • 51
  • 1
  • 5
  • Don't create tuples and dictionaries. Plot the data directly using numpy arrays and np.meshgrid. See [Surface plots in matplotlib](https://stackoverflow.com/questions/9170838/surface-plots-in-matplotlib) or the matplotlib [documentation](https://matplotlib.org/stable/gallery/mplot3d/surface3d.html). – Mr. T Jan 20 '22 at 13:46

1 Answers1

0

I am not sure why you are creating tuples & dictionaries. Assuming you have three different arrays for x, y and z axis:

import random
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D  
import matplotlib.pyplot as plt

x = [val for val in range(10)]
y = [val * val for val in range(10)]
z = [val * val * val for val in range(10)]

All you have to do is:

fig = plt.figure()
ax = Axes3D(fig)
surf = ax.plot_trisurf(x, y, z, linewidth=0.1)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

End result below:

enter image description here

yakutsa
  • 642
  • 3
  • 13