简体   繁体   中英

How would I make a 3D surface plot in Matplotlib given this data of x,y,z coordinates?

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.

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM