Python 2D 绘图为 3D (Matplotlib) [英] Python 2D plots as 3D (Matplotlib)

查看:67
本文介绍了Python 2D 绘图为 3D (Matplotlib)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python图:我每天同时采集大量样本,这些样本显示(某些方面)测量值的变化.这可能显示为2D图(左下方),但是随着样本数量的增加,我想将此数据显示为堆叠的3D图(右下方图像)-此图像仅用于说明.

Python plot in Matplotlib: I have a number of samples taken daily at the same time which shows a change in measurement (of something). This may be shown as a 2D plot (below left), but as the sample number increases I'd like to display this data as a 3D plot which is stacked (below right image) - this image is for illustration only.

作为起点,我的代码如下,我该如何实现?

For a starting point my code is below, how may I achieve this?

import numpy as np
import pylab as plt


t  = np.arange(1024)*1e-6
y1 = np.sin(t*2e3*np.pi) 
y2 = 0.5*y1
y3 = 0.25*y1

plt.plot(t,y1,'k-', label='12/03/14')
plt.plot(t,y2,'r-', label='13/03/14')
plt.plot(t,y3,'b-', label='14/03/14')
plt.xlabel('Time/sample no.')
plt.ylabel('Pk-pk level (arbitrary units)')
plt.legend()
plt.grid()
plt.show()

推荐答案

会是这样吗?

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

zs = [0.0, 1.0, 2.0]
t  = np.arange(1024)*1e-6
ones = np.ones(1024)
y1 = np.sin(t*2e3*np.pi) 
y2 = 0.5*y1
y3 = 0.25*y1

verts=[list(zip(t, y1)), list(zip(t, y2)), list(zip(t, y3))]


poly = PolyCollection(verts, facecolors = ['r','g','b'])
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')
ax.set_xlabel('X')
ax.set_xlim3d(0, 1024e-6)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 3)
ax.set_zlabel('Z')
ax.set_zlim3d(-1, 1)

plt.show()

这篇关于Python 2D 绘图为 3D (Matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆