简体   繁体   English

matplotlib:在 3D 轴上绘制路径

[英]matplotlib: Plotting the path in 3D axis

I would like to plot the path based on xyz location data.我想 plot 基于 xyz 位置数据的路径。 Below is a reproducible example, all the lines keep starting from 0 instead of following one after each other.下面是一个可重现的示例,所有行都从 0 开始,而不是一个接一个地跟随。

import seaborn as sns
# loading sample data and replicating my scenario 
data = sns.load_dataset("iris")
# giving it a numeric value to replicate my scenario 
cat_lbl = {'setosa': 1, 'versicolor': 2,'virginica' : 3}
data['cat_lbl'] = data['species'].map(cat_lbl)


#plot headings
species = ['setosa', 'versicolor', 'virginica']


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

sepal_length = data.loc[:,['sepal_length','cat_lbl']]
sepal_width = data.loc[:,['sepal_width','cat_lbl']]
petal_length = data.loc[:,['petal_length','cat_lbl']]

fig = plt.figure(figsize=([20,15]))
for lbl in range(3):
    lbl=lbl+1
    x=sepal_length[(sepal_length.cat_lbl == lbl)].values
    y=sepal_width[(sepal_width.cat_lbl == lbl)].values
    z=petal_length[(petal_length.cat_lbl == lbl)].values


    ax=fig.add_subplot(3,3,lbl, projection='3d')
    ax.plot(x.flatten(),y.flatten(),z.flatten())
    ax.set_title(species[lbl-1])
plt.show()

在此处输入图像描述

Your problem is that你的问题是

x=sepal_length[(sepal_length.cat_lbl == lbl)].values
y=sepal_width[(sepal_width.cat_lbl == lbl)].values
z=petal_length[(petal_length.cat_lbl == lbl)].values

are actually 2D arrays that contain the category index (1,2,3).实际上是包含类别索引 (1,2,3) 的 2D arrays。 So when you flatten x.flatten() , you alternate between the coordinate and the category index (you can see that the lines actually loop back to (1,1) on the first graph, (2,2) on the second and (3,3) on the third)因此,当您展平x.flatten()时,您会在坐标和类别索引之间交替(您可以看到这些线实际上在第一个图上循环回 (1,1),在第二个图上返回 (2,2) 和 ( 3,3) 在第三个)

Here is how I would write your code:以下是我将如何编写您的代码:

import seaborn as sns
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

data = sns.load_dataset("iris")
species = ['setosa', 'versicolor', 'virginica']

fig,axs = plt.subplots(1,3,subplot_kw=dict(projection='3d'),figsize=(9,3))
for sp,ax in zip(species, axs.flat):
    temp = data.loc[data['species']==sp]
    x=temp['sepal_length'].values
    y=temp['sepal_width'].values
    z=temp['petal_length'].values

    ax.plot(x,y,z)
    ax.set_title(sp)
plt.show()

在此处输入图像描述

Try ax.plot3D(...) instead of ax.plot(...) as indicated in this tutorial for 3D plotting:尝试ax.plot3D(...)而不是ax.plot(...)教程中所示用于 3D 绘图:

ax = plt.axes(projection='3d')

# Data for a three-dimensional line
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')

# Data for three-dimensional scattered points
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');

3D 线图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM