【问题标题】:pandas data frame plot multiple frames in 3Dpandas dataframe 以 3D 方式绘制多个帧
【发布时间】:2020-09-24 13:22:16
【问题描述】:

我想在一张 3D 图中绘制两个数据框

data1 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [5,2,1,6,9,3,8,2,0,5]} 
data2 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [19,12,1,26,19,33,28,28,10,5]} 
newdf = pd.DataFrame(data1)
newdf2= pd.DataFrame(data2)
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111, projection='3d')
newdf.plot(kind='bar',x ='numbers', y='frequency',figsize=(10,5), color='thistle', width=.4, legend=True, alpha=0.8, ax=ax)
newdf2.plot(kind='bar',x ='numbers', y='frequency',figsize=(10,5), color='navy', width=.2,legend=True, alpha=1,ax=ax) 
plot.show()

这会将两个图绘制在一个图中,但 y 轴和 z 轴被调换了。我想在 z 平面中绘制每个数据集,其中数字形成 x 轴,频率形成 y 轴。我从所有示例中都无法理解如何实现这一目标。我还想将条形图绘制为 3d 条形图。我很感激任何帮助,请

【问题讨论】:

    标签: pandas matplotlib


    【解决方案1】:

    我只是在猜测,因为我没有我希望看到的那种输出示例,但是您希望实现以下示例的 3D 图形:y 轴是数据框类型,z轴为频率。

    import matplotlib.pyplot as plt
    
    data1 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [5,2,1,6,9,3,8,2,0,5]} 
    data2 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [19,12,1,26,19,33,28,28,10,5]} 
    newdf = pd.DataFrame(data1)
    newdf2 = pd.DataFrame(data2)
    
    fig = plt.figure(figsize=(10,5))
    ax = fig.add_subplot(111, projection='3d')
    
    yticks = [4,3,2,1,0]
    ax.bar(newdf['numbers'], newdf['frequency'], zs=3, zdir='y', color='b', alpha=0.8)
    ax.bar(newdf2['numbers'], newdf2['frequency'], zs=1, zdir='y', color='r', alpha=0.8)
    
    ax.set_xlabel('number')
    ax.set_ylabel('df_type')
    ax.set_zlabel('frequency')
    
    ax.set_yticks(yticks)
    
    plt.show()
    

    bar3d 类型

    # ax.bar3d(xpos, ypos, zpos, dx, dy, dz)
    ax.bar3d(newdf['numbers'], 3, 0, dx=1, dy=1, dz=newdf['frequency'], color='b', alpha=0.6)
    ax.bar3d(newdf2['numbers'], 0, 0, dx=1, dy=1, dz=newdf2['frequency'], color='r', alpha=0.3)
    

    【讨论】:

    • 是否也可以将条形制作成 3D?非常清晰的解决方案。 add_subplot(111,projection ='3d') 中的 111 与什么有关?
    • ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average')请参考this
    • 感谢您并参考文档。多亏你的清晰,我现在明白如何使用它了
    【解决方案2】:

    这是我手头的解决方案(找不到原始链接)。

    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import pandas as pd
    
    # datasets
    data1 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [5,2,1,6,9,3,8,2,0,5]} 
    data2 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [19,12,1,26,19,33,28,28,10,5]} 
    
    newdf = pd.DataFrame(data1)
    newdf2= pd.DataFrame(data2)
    
    # put all the data in one place
    # can use data1['frequency'] and data2['frequency'] directly
    data = np.array([
            newdf['frequency'].values,
            newdf2['frequency'].values,
            ])
    
    fig = plt.figure(figsize=(8,8))
    ax = fig.add_subplot(111, projection='3d')
    colors = ["r","g","b"]*5  # for up to 15 sets of bars
    
    # Draw 3D bars 
    ncnt, nbins = data.shape[:2]
    xs = np.arange(nbins)
    for i in range(ncnt):
        ys = data[i]
        cs = [colors[i]] * nbins
        ax.bar(xs, ys.ravel(), zs=i, zdir='x', color=cs, alpha=0.8)
    
    ax.set_xlabel('data_frame')
    ax.set_ylabel('numbers')
    ax.set_zlabel('frequency')
    
    ax.set_xticks(range(data.shape[0]))    # 2 dataframes
    ax.set_yticks(newdf['numbers'].values) # from 'numbers' column
    
    plt.show()
    

    输出图:

    【讨论】:

    • 一个非常清晰的解决方案。我接受下面的原因仅仅是因为它更短。我想接受并投票赞成这两种解决方案。条形图也可以绘制成三维的吗?
    猜你喜欢
    • 2021-05-16
    • 1970-01-01
    • 2017-03-24
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2016-11-06
    • 2021-01-08
    相关资源
    最近更新 更多