python颜色参数_python matplotlib.pyplot中3D参数曲线的线条颜色

1586010002-jmsa.png

I've been googling quite some time with no success ... maybe my keywords are just lousy. Anyway, suppose I have three 1D numpy.ndarrays of the same length I'd like to plot them in 3D as a trajectory. Moreover, I'd like to be able to do either of the following things:

Change the colour of the line as a function of z

Change the colour of the line as a function of time (i.e. the index in the arrays)

This demo has an example of making such a curve:

import matplotlib as mpl

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

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

theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)

z = np.linspace(-2, 2, 100)

r = z**2 + 1

x = r * np.sin(theta)

y = r * np.cos(theta)

ax.plot(x, y, z)

plt.show()

gH7ly.png

But how do I achieve 1 or 2? Solutions to only one or the other are welcome!

Thanks in advance.

解决方案

As with normal 2d plots, you cannot have a gradient of color along an ordinary line. However, you can do it with scatter:

import matplotlib as mpl

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

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

theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)

z = np.linspace(-2, 2, 100)

r = z**2 + 1

x = r * np.sin(theta)

y = r * np.cos(theta)

#1 colored by value of `z`

ax.scatter(x, y, z, c = plt.cm.jet(z/max(z)))

#2 colored by index (same in this example since z is a linspace too)

N = len(z)

ax.scatter(x, y, z, c = plt.cm.jet(np.linspace(0,1,N)))

plt.show()

I liked @Junuxx's hack so I applied it here:

for i in xrange(N-1):

ax.plot(x[i:i+2], y[i:i+2], z[i:i+2], color=plt.cm.jet(255*i/N))

loWUe.png

tLj3p.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值