【问题标题】:3D graph plotting using matplotlib使用 matplotlib 绘制 3D 图形
【发布时间】:2016-08-17 01:55:37
【问题描述】:

[在此处输入图像描述][1]我正在处理一个项目,该项目的数据是从 c 代码生成的,我将其复制到一个 txt 文件中,我在下面给出了该文件。我应该通过 python 读取该数据,因此使用 matplotlib 生成 3D 图。我经历了很多pyhton代码,但我不知道如何从数据中找出x、y和z轴来绘制。 我知道这是一个模糊而蹩脚的问题,但我是新手,数学也很烂。

数据.txt

s1  s2  s3  s4  s5  s6  s7  s8  s9  s10 s11 s12 s13 s14 s15 s16 
64m 838.4   829.2   819.0   807.5   798.9   787.5   773.9   765.3   752.9   742.0   728.3   713.3   702.2   687.2   683.2   660.3   
32m 838.3   828.7   818.5   808.5   799.9   785.9   774.4   766.8   752.8   741.0   729.6   712.9   701.2   688.6   680.3   659.1   
16m 838.5   828.1   816.8   806.8   800.2   787.8   777.0   767.6   752.7   738.0   733.3   716.8   704.2   692.8   684.9   660.2   
8m  835.5   830.3   822.3   812.4   799.8   792.1   779.6   769.8   757.5   744.8   733.2   716.4   704.2   692.2   684.7   664.6   
4m  835.5   829.9   818.7   815.1   807.4   795.5   759.0   775.2   761.8   752.3   739.2   723.8   711.6   696.4   688.5   669.0   
2m  842.5   852.1   849.0   840.9   842.5   836.0   824.8   825.9   819.1   820.5   815.5   809.8   803.8   794.7   786.5   772.7   
1024k   855.4   855.8   854.4   851.1   853.0   851.0   848.1   831.7   843.6   842.2   841.2   839.7   836.7   830.0   822.3   812.0   
512k    855.3   856.7   854.3   851.8   853.1   849.8   848.1   845.7   843.2   842.8   841.2   840.4   836.4   831.2   821.5   812.0   
256k    853.6   854.5   825.0   831.8   851.4   846.4   846.5   843.2   842.6   841.8   842.3   843.0   845.3   847.0   839.1   829.9   
128k    854.6   853.3   853.6   851.2   852.9   852.7   846.6   845.5   843.8   843.7   847.6   849.9   853.4   855.1   853.8   844.8   
64k 854.4   854.6   854.0   849.6   853.2   851.6   847.3   844.4   841.6   843.2   847.7   846.6   847.6   847.4   848.1   841.7   
32k 855.8   859.7   857.2   857.3   856.0   861.4   859.8   859.4   861.8   854.7   852.4   852.9   854.0   847.8   844.6   846.4   
16k 857.6   860.4   851.9   850.0   850.4   846.9   857.0   845.1   838.3   841.6   838.5   844.9   837.1   847.1   839.7   829.4   
8k  851.1   850.0   843.8   869.5   840.6   832.4   848.6   829.4   839.2   829.0   811.9   833.7   823.0   810.7   810.8   821.4   
4k  851.9   856.4   833.6   828.1   818.7   814.3   822.1   808.4   819.8   784.8   773.3   769.9   766.6   771.5   752.7   765.2   
2k  867.3   830.7   810.1   810.9   794.2   777.5   758.2   768.5   739.7   726.9   719.1   718.2   699.9   700.0   672.1   685.9   
1k  832.3   807.8   794.8   774.0   726.9   712.4   687.5   687.7   721.9   726.9   703.5   695.7   692.5   662.2   537.7   667.2   

【问题讨论】:

    标签: python matplotlib plot 3d


    【解决方案1】:

    首先看看docs。我假设您以前从未使用 matplotlib 绘图。让我们从它的基本工作原理开始。首先,将您的数据格式化为 Python 可迭代对象,如列表/数组/元组。当然,我们还需要 matplotlib:

    # we need this to create figures
    import matplotlib.pyplot as plt
    # this is needed for 3d projections
    from mpl_toolkits.mplot3d import Axes3D
    
    # some chunks of your data as lists
    x = [128e3, 64e3, 32e3, 16e3, 8e3, 4e3, 2e3, 1e3]
    s1 = [854.6, 854.4, 855.8, 857.6, 851.1, 851.9, 867.3, 832.3]
    s2 = [853.3, 854.6, 859.7, 860.4, 850.0, 856.4, 830.7, 807.8]
    s3 = [853.6, 854.0, 857.2, 851.9, 843.8, 833.6, 810.1, 794.8]
    
    # to plot 2d data create a figure
    fig = plt.figure()
    # add a (sub)plot
    ax = fig.add_subplot(111)
    # use it to plot your 2d data
    ax.plot(x, s1)
    plt.show()
    

    对于 3d 数据基本相同:

    fig = plt.figure()
    # tell matplotlib to use 3d projection
    ax = fig.add_subplot(111, projection='3d')
    # now we need 3d data of course
    ax.plot(x, s1, s2)
    plt.show()
    

    现在的 3d 数据是通过 3d 空间的 n 个点 (x_0, s1_0, s2_0) 到 (x_n, s1_n, s2_n) 的轨迹。有很多方法可以呈现您的数据(请参阅链接)。它们基本上都遵循相同的语法。另一个例子是 3d 散点图:

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(x, s1, s2)
    plt.show()
    
    # a bit more tricky, we will need NumPy
    import numpy as np
    
    # we want to plot three graphs
    idx = np.arange(3)
    # So we need a meshgrid
    X, Y = np.meshgrid(x, idx)
    Z = np.array([s1, s2, s3])
    # Basically X has now three 'lanes'
    # Y has 1k to 128k for each lane
    # And Z[n] has the data for lane n
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_wireframe(X, Y, Z, cstride=0)
    
    plt.show()
    

    【讨论】:

    • 我已经复制了数据,请看一下。
    • 令人印象深刻的编辑。请仅在将来发布完整答案,因为仅链接的答案(例如编辑前的答案)是不可接受的。在How to Answer 中查看更多信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2015-08-08
    • 2013-08-06
    • 1970-01-01
    相关资源
    最近更新 更多