【问题标题】:Plotting multiple pandas DataFrames in one *3D* scatterplotplot在一个 *3D* 散点图中绘制多个 pandas DataFrame
【发布时间】:2021-05-16 17:22:18
【问题描述】:

我想在一个 3D 散点图中绘制两个数据框。

这是我用于一个数据帧的代码:

import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D

...

sns.set(style = "darkgrid")

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
x = df['xitem']
y = df['yitem']
z = df['zitem']

ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
ax.set_zlabel("Z Label")

ax.scatter(x, y, z)

plt.show()

我不知道如何调整它,所以我在同一个图上绘制了两个不同的数据框,但颜色不同。我该怎么做?

编辑:我正在寻找如何将两个数据框用于 3D 绘图。

【问题讨论】:

    标签: python python-3.x pandas matplotlib seaborn


    【解决方案1】:

    假设您有两个名为 df1df2 的 DataFrame,都包含列 'xitem', 'yitem', 'zitem',您可以这样绘制它们:

    for curr_df, c in zip((df1, df2), ('b', 'r')):
        ax.scatter(*curr_df[['xitem', 'yitem', 'zitem']].values.T, color=c)
    

    这里有一个完整的例子:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    sns.set(style = "darkgrid")
    
    df1 = pd.DataFrame(
        data=np.random.random((100, 3)) + np.array([1, 1, 1]),
        columns=['xitem', 'yitem', 'zitem'],
    )
    
    df2 = pd.DataFrame(
        data=np.random.random((100, 3)),
        columns=['xitem', 'yitem', 'zitem'],
    )
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection = '3d')
    
    for curr_df, c in zip((df1, df2), ('b', 'r')):
        ax.scatter(*curr_df[['xitem', 'yitem', 'zitem']].values.T, color=c)
    
    ax.set_xlabel("X Label")
    ax.set_ylabel("Y Label")
    ax.set_zlabel("Z Label")
    
    plt.show()
    

    猜你喜欢
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2014-09-03
    相关资源
    最近更新 更多