Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

python - Plotting a 3d surface from a list of tuples in matplotlib

I'm trying to plot a 3d surface from a list of (X,Y,Z) tuples, data is like this, I'm unsure how to feed it to matplotlib:

[(60,5,'121'), (61,5,'103'),  (62,5,'14.8'), (63,5,'48.5'),
(64,5,'57.5'), (65,5,'75.7'), (66,5,'89.6'), (67,5,'55.3'),
(68,5,'63.3'), (69,5,'118'),  (70,5,'128'),  (71,5,'105'),
(72,5,'115'),  (73,5,'104'),  (74,5,'134'),  (75,5,'123'),
(76,5,'66.3'), (77,5,'132'),  (78,5,'145'),  (79,5,'115'),
(80,5,'38.2'), (81,5,'10.4'), (82,5,'18.4'), (83,5,'87'),
(84,5,'86.7'), (85,5,'78.9'), (86,5,'89.9'), (87,5,'108'),
(88,5,'57.1'), (89,5,'51.1'), (90,5,'69.1'), (91,5,'59.8'),
(60,6,'48.9'), (61,6,'33.3'), (62,6,'-19.2'),(63,6,'-17.5'),
(64,6,'-6.5'), (65,6,'75.7'), (66,6,'89.6'), (67,6,'55.3'),
(68,6,'99.8'), (69,6,'156'),  (70,6,'141'),  (71,6,'54.1'),
(72,6,'66.1'), (73,6,'98.9'), (74,6,'155'),  (75,6,'146'),
(76,6,'111'),  (77,6,'132'),  (78,6,'145'),  (79,6,'97.3'),
(80,6,'101'),  (81,6,'59.4'), (82,6,'70.4'), (83,6,'142'),
(84,6,'145'),  (85,6,'140'),  (86,6,'56.9'), (87,6,'77.8'),
(88,6,'21.1'), (89,6,'27.1'), (90,6,'48.1'), (91,6,'41.8')]

Ideally I would like it to look something like this:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is one way to do it.

from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import numpy as np

data = [(60, 5, '121'), (61, 5, '103'), (62, 5, '14.8'), (63, 5, '48.5'), (64, 5, '57.5'), (65, 5, '75.7'), (66, 5, '89.6'), (67, 5, '55.3'), (68, 5, '63.3'), (69, 5, '118'), (70, 5, '128'), (71, 5, '105'), (72, 5, '115'), (73, 5, '104'), (74, 5, '134'), (75, 5, '123'), (76, 5, '66.3'), (77, 5, '132'), (78, 5, '145'), (79, 5, '115'), (80, 5, '38.2'), (81, 5, '10.4'), (82, 5, '18.4'), (83, 5, '87'), (84, 5, '86.7'), (85, 5, '78.9'), (86, 5, '89.9'), (87, 5, '108'), (88, 5, '57.1'), (89, 5, '51.1'), (90, 5, '69.1'), (91, 5, '59.8'), (60, 6, '48.9'), (61, 6, '33.3'), (62, 6, '-19.2'), (63, 6, '-17.5'), (64, 6, '-6.5'), (65, 6, '75.7'), (66, 6, '89.6'), (67, 6, '55.3'), (68, 6, '99.8'), (69, 6, '156'), (70, 6, '141'), (71, 6, '54.1'), (72, 6, '66.1'), (73, 6, '98.9'), (74, 6, '155'), (75, 6, '146'), (76, 6, '111'), (77, 6, '132'), (78, 6, '145'), (79, 6, '97.3'), (80, 6, '101'), (81, 6, '59.4'), (82, 6, '70.4'), (83, 6, '142'), (84, 6, '145'), (85, 6, '140'), (86, 6, '56.9'), (87, 6, '77.8'), (88, 6, '21.1'), (89, 6, '27.1'), (90, 6, '48.1'), (91, 6, '41.8')]
x, y, z = zip(*data)
z = map(float, z)
grid_x, grid_y = np.mgrid[min(x):max(x):100j, min(y):max(y):100j]
grid_z = griddata((x, y), z, (grid_x, grid_y), method='cubic')

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(grid_x, grid_y, grid_z, cmap=plt.cm.Spectral)
plt.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...