簡體   English   中英

Python - matplotlib中的colormap用於3D線圖

[英]Python - colormap in matplotlib for 3D line plot

我正在嘗試使用matplotlib的工具包mplot3D繪制3D線圖我有4個數組

  • tab_C [0]是x值的數組
  • tab_C [1]是y值的數組
  • tab_C [2]是一個z值數組
  • tab_t是一個時間值數組

我用這個繪制了我的情節:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig1 = plt.figure()
ax = fig1.gca(projection='3d')
ax.plot(tab_C[0], tab_C[1], tab_C[2])

plt.show()

它工作但現在我希望這個情節有基於時間值的彩虹色。 我搜索了matplotlib的網頁,但沒有任何內容。 有關這個問題的任何建議嗎?

你可以像Bill所展示的那樣以純matploblib的方式做到這一點,但它對Mayavi來說更直觀。 這是他們的文檔中的一個很好的例子:

from mayavi import mlab
n_mer, n_long = 6, 11
dphi = np.pi / 1000.0
phi = np.arange(0.0, 2 * pi + 0.5 * dphi, dphi)
mu = phi * n_mer
x = np.cos(mu) * (1 + np.cos(n_long * mu / n_mer) * 0.5)
y = np.sin(mu) * (1 + np.cos(n_long * mu / n_mer) * 0.5)
z = np.sin(n_long * mu / n_mer) * 0.5
t = np.sin(mu)

mlab.plot3d(x, y, z, t, tube_radius=0.025, colormap='Spectral')

在此輸入圖像描述

只是參數colormap決定了colormapxyzt可以被你想要的特定數組替換。

沒有簡單的“單行”方式來做到這一點。 然而,前進的方向並不是那么糟糕。 您唯一需要考慮的是如何將時間值映射到顏色。 這是一種可行的方法:

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

N_points = 10
x = np.arange(N_points, dtype=float)
y = x
z = np.random.rand(N_points)
t = x

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

# colors need to be 3-tuples with values between 0-1.
# if you want to use the time values directly, you could do something like
t /= max(t)
for i in range(1, N_points):
    ax.plot(x[i-1:i+1], y[i-1:i+1], z[i-1:i+1], c=(t[i-1], 0, 0))
plt.show()

在此輸入圖像描述

你可以玩那個元組。 使用一個帶有2個零的值將根據非零參數的位置為您提供紅色,綠色和藍色的陰影。 其他一些可能的顏色選擇可能是灰色陰影

c = (t[i-1], t[i-1], t[i-1])

或者通過預定義顏色列表循環:

# Don't do: t /= max(t)
from itertools import cycle
colors = cycle('bgrc')
for i in range(1, N_points):
    ax.plot(x[i-1:i+1], y[i-1:i+1], z[i-1:i+1], c=colors[t[i-1]])
plt.show()

但是,這取決於您如何定義時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM