Python-3D参数曲线的线颜色 [英] Python - Line colour of 3D parametric curve

查看:190
本文介绍了Python-3D参数曲线的线颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个列表, tab_x (包含x的值)和 tab_z (包含z的值),它们的长度相同,并且值为y .

I have 2 lists tab_x (containe the values of x) and tab_z (containe the values of z) which have the same length and a value of y.

我想绘制一个3D曲线,该曲线由z的值着色.我知道可以将其绘制为2D图,但是我想绘制其中一些具有不同y值的图以进行比较,所以我需要将其设为3D.

I want to plot a 3D curve which is colored by the value of z. I know it's can be plotted as a 2D plot but I want to plot a few of these plot with different values of y to compare so I need it to be 3D.

我的 tab_z 也包含否定值

在此

I've found the code to color the curve by time (index) in this question but I don't know how to transforme this code to get it work in my case.

感谢您的帮助.

我添加了更具体的代码:

I add my code to be more specific:

fig8 = plt.figure()
ax8 = fig8.gca(projection = '3d')
tab_y=[]
for i in range (0,len(tab_x)):
  tab_y.append(y)
ax8.plot(tab_x, tab_y, tab_z)

我现在有这个

我已经尝试过此代码

for i in range (0,len(tab_t)):
    ax8.plot(tab_x[i:i+2], tab_y[i:i+2], tab_z[i:i+2],color=plt.cm.rainbow(255*tab_z[i]/max(tab_z)))

完全失败:

推荐答案

您的第二次尝试几乎成功了.唯一的变化是 colormap cm.jet()的输入必须在0到1的范围内.您可以使用标准化.

Your second attempt almost has it. The only change is that the input to the colormap cm.jet() needs to be on the range of 0 to 1. You can scale your z values to fit this range with Normalize.

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

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

N = 100
y = np.ones((N,1))
x = np.arange(1,N + 1)
z = 5*np.sin(x/5.)

cn = colors.Normalize(min(z), max(z)) # creates a Normalize object for these z values
for i in xrange(N-1):
    ax.plot(x[i:i+2], y[i:i+2], z[i:i+2], color=plt.cm.jet(cn(z[i])))

plt.show()

这篇关于Python-3D参数曲线的线颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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