使用dataframe在Python中绘制3D表面

时间:2022-09-10 21:58:17

I am fairly new to Python, so I sorry if my question is not as coherent as a fluent python user, but I have searched the web and really haven't found a solution for what I am trying to do. I have a set of experimental data in a csv file that I would like to plot as a 3D surface. It is structured as follows:

我对Python很新,所以我很抱歉,如果我的问题不像流畅的python用户那样连贯,但是我已经在网上搜索了,并且找不到我想要做的解决方案。我在csv文件中有一组实验数据,我想将其绘制为3D表面。它的结构如下:

               x(time)
y(depth)   z(temperature readings)
y          z(temperature readings)
y          z(temperature readings)
y          z(temperature readings)

Basically, my top row and my left hand column should function as x,y parts of a depth time array and the z is the temperature reading at each x,y coordinate.

基本上,我的顶行和左手列应该用作深度时间数组的x,y部分,z是每个x,y坐标的温度读数。

I have tried setting my x and y as arrays and then using meshgrid(x,y) to set up a grid. But my z is values, not an explicit function of x,y. So I can't get it to work. I tried referencing the z using x,y but that didn't seem to work.

我已经尝试将我的x和y设置为数组,然后使用meshgrid(x,y)来设置网格。但我的z是值,而不是x,y的显式函数。所以我无法让它发挥作用。我尝试使用x,y引用z,但这似乎不起作用。

I have also tried importing it as a dataframe and playing around with 3D plotting options, but they all seem to require a format of:

我也尝试将其作为数据框导入并使用3D绘图选项,但它们似乎都需要以下格式:

   x y z
   .....
   .....
   .....

which doesn't help with my dataset. I would appreciate any ideas/suggestions on this! Thanks.

这对我的数据集没有帮助。我将不胜感激任何想法/建议!谢谢。

1 个解决方案

#1


1  

How about this for a start? The data reading and re-shaping part is not the fastest and most reliable on the planet - you might use some battle-proven Pandas routines instead if you do this sort of work on a larger scale - but the example might give you an insight into how this can be done.

一开始怎么样?数据读取和重新塑造部分并不是地球上最快和最可靠的部分 - 如果您在更大范围内进行此类工作,您可能会使用一些经过实战验证的Pandas例程 - 但该示例可能会让您深入了解怎么做到这一点。

from plotly.offline import plot
from plotly.graph_objs import Figure, Layout, Surface

# Some "random" data in the desired format
some_data = """depth;1;2;3;4
0.2;0;1;1;0
0.4;0.2;1.4;1.6;0.8
0.6;0.3;1.2;1.7;0.7
0.8;0.1;0.8;1.1;0.3
"""

# Iterate over lines in data - bringing it into shape
data_z = []
data_y = []
for line_index, line in enumerate(some_data.split('\n')):
    # Get time vector (x)
    if line_index == 0:
        data_x = [float(x) for x in line.split(';')[1:]]
    # Get z-values
    else:
        # If line is not empty
        if line != '':
            data_z.append([float(z) for z in line.split(';')[1:]])
            data_y.append(float(line.split(';')[0]))

# Prepare a plot
fig = Figure(
    data = [
        Surface(
            x = data_x,
            y = data_y,
            z = data_z
        )
    ],
    layout = Layout(
        title = 'some data plot',
        autosize = True
        )
    )

# Spit it out ...
plot(fig, filename='some-data-surface')

#1


1  

How about this for a start? The data reading and re-shaping part is not the fastest and most reliable on the planet - you might use some battle-proven Pandas routines instead if you do this sort of work on a larger scale - but the example might give you an insight into how this can be done.

一开始怎么样?数据读取和重新塑造部分并不是地球上最快和最可靠的部分 - 如果您在更大范围内进行此类工作,您可能会使用一些经过实战验证的Pandas例程 - 但该示例可能会让您深入了解怎么做到这一点。

from plotly.offline import plot
from plotly.graph_objs import Figure, Layout, Surface

# Some "random" data in the desired format
some_data = """depth;1;2;3;4
0.2;0;1;1;0
0.4;0.2;1.4;1.6;0.8
0.6;0.3;1.2;1.7;0.7
0.8;0.1;0.8;1.1;0.3
"""

# Iterate over lines in data - bringing it into shape
data_z = []
data_y = []
for line_index, line in enumerate(some_data.split('\n')):
    # Get time vector (x)
    if line_index == 0:
        data_x = [float(x) for x in line.split(';')[1:]]
    # Get z-values
    else:
        # If line is not empty
        if line != '':
            data_z.append([float(z) for z in line.split(';')[1:]])
            data_y.append(float(line.split(';')[0]))

# Prepare a plot
fig = Figure(
    data = [
        Surface(
            x = data_x,
            y = data_y,
            z = data_z
        )
    ],
    layout = Layout(
        title = 'some data plot',
        autosize = True
        )
    )

# Spit it out ...
plot(fig, filename='some-data-surface')