简体   繁体   English

在matplotlib中绘制3D数据

[英]Plotting 3d data in matplotlib

-- This is an updated post, old post removed -- -这是更新的帖子,旧帖子已删除-

Suppose I have data like below I want to plot in a 3D graph/surface with matplotlib . 假设我有如下数据,我想使用matplotlib在3D图形/表面中进行绘制。 How can I do it? 我该怎么做?

在此处输入图片说明

Following @JouniK.Seppänen's answer, I figured I need meshgrid() , but I think my axis data is not very correct. 遵循@JouniK.Seppänen的回答,我认为我需要meshgrid() ,但是我认为我的轴数据不是很正确。 If I have data (in JSON) like: 如果我有(JSON)的资料,例如:

{
    "data": {
        "Random": [
            [1834, 3353, 4602, 5471, 6522, 7873], 
            [2637, 8575, 8357, 10329, 9742, 10359], 
            [3648, 10602, 10667, 10751, 10666, 10552], 
            [4570, 10220, 9202, 10460, 10329, 10928], 
            [5879, 10788, 10832, 10923, 11075, 10989], 
            [6783, 11104, 10235, 10499, 11024, 10731], 
            [7074, 11097, 10222, 10613, 10508, 10767], 
            [7300, 11002, 10727, 11073, 10328, 10864]], 
        "LRU": [
            [123, 155, 201, 223, 296, 321], 
            [143, 590, 1046, 1566, 1924, 2434], 
            [163, 1167, 1774, 2578, 3363, 3980], 
            [182, 1172, 2259, 3038, 4200, 4907], 
            [219, 1718, 3044, 3658, 5236, 5680], 
            [709, 2263, 3588, 4551, 5262, 6197], 
            [2065, 3865, 4430, 5024, 5986, 6617], 
            [3048, 4249, 5029, 5790, 6157, 6826]], 
        "FIFO": [
            [180, 269, 350, 424, 580, 601], 
            [230, 906, 1446, 2009, 2408, 2902], 
            [316, 1590, 2261, 3042, 3848, 4457], 
            [473, 1664, 2781, 3542, 4764, 5398], 
            [665, 2290, 3609, 4194, 5781, 6207], 
            [1158, 2826, 4115, 5064, 5751, 6613], 
            [2457, 4375, 4974, 5471, 6464, 7077], 
            [3512, 4724, 5485, 6272, 6684, 7312]]
    }, 
    "workingSetAxis": [2, 22, 42, 62, 82, 102, 122, 142], 
    "stabilityAxis": [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
}

Where workingSetAxis is supposed to be the x-axis, and stabilityAxis the y-axis. 其中workingSetAxis应该是x轴,和stabilityAxis y轴。 I did something like 我做了类似的事情

plot(jsonObj["data"]["FIFO"], jsonObj["workingSetAxis"], jsonObj["stabilityAxis"])

def plot(data, workingSetAxis, stabilityAxis):
    # make axis data
    X, Y = numpy.meshgrid(workingSetAxis, stabilityAxis)
    Z = data

    fig = plt.figure()
    ax = fig.add_subplot(111, projection="3d")
    ax.plot_wireframe(X, Y, Z)
    plt.show()

and got something like: 并得到类似:

在此处输入图片说明

very different from the plot I got from excel (for FIFO data) 与我从excel获得的图非常不同(对于FIFO数据)

在此处输入图片说明

I believe my axis data or something is wrong, but which? 我相信我的轴数据或某些错误,但是哪一个呢?

Try plotting the transpose of your data, for example, using 尝试绘制数据的转置,例如,使用

def plot(data, workingSetAxis, stabilityAxis):
    # make axis data
    X, Y = numpy.meshgrid(workingSetAxis, stabilityAxis)
    Z = numpy.transpose(data) #<--- This is the only line I have changed.

    fig = plt.figure()
    ax = fig.add_subplot(111, projection="3d")
    ax.plot_wireframe(X, Y, Z)
    plt.show()

and your example data gives me the following plot: 您的示例数据给了我以下图: 显示预期输出的图

See eg this example . 参见例如这个例子 Turn your X and Y values into 2D arrays with meshgrid , then use your data values as Z. 使用meshgrid将X和Y值转换为2D数组,然后将数据值用作Z。

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

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