python颜色参数表,Python-3D参数曲线的线颜色

本文介绍如何使用Python绘制带有颜色渐变的3D曲线。通过调整颜色映射范围并利用Normalize对象来确保颜色映射正确地应用于曲线上的z值。最终实现了一种能够根据不同z值显示不同颜色的3D曲线绘制方法。

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.

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.

My tab_z also containe negatives values

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.

Thanks for the help.

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)

I have this for now

9c15082912d1ae6d00f5a0fcb9424463.png

I've tried this code

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)))

A total failure:

c10bd8eed74d756ad568685f5192832b.png

解决方案

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()

dbc9214a89577818379636b25a89db72.png

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值