0

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:

               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.

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.

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:

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

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

1 Answer 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.

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That is enough to get me started. I thought pandas would have something more obvious but I certainly haven't found it. I'll give this a shot and see how I do. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.