简体   繁体   中英

3D surface plot in Python using plotly

I have a question about how the plotly surface code works.

I've got the data from dataframe to plot surface 3D graph, 1D array of x, y and z

example:

x (temperatures) =      [26,25,24,29,21,20,21,21,26]

y (humidity)     =      [50,60,50,40,50,70,80,90,90]

z (power consumption) = [12,13,14,11,11,10,11,12,15]

I need to plot each point (ex: x1,y1,z1) to be a surface and I have used this code

import plotly.graph_objects as go
import numpy as np

#x,y,z from above

fig = go.Figure(data = go.Surface(z=z,
                                  x=x,
                                  y=y))

fig.update_traces(contours_z = dict(show = True , usecolormap = True ,
                                    highlightcolor = 'limegreen' , project_z = True))

fig.update_layout(title = 'Linear')

fig.show()

but it doesn't show anything.

(I also known that z need to be 2D array but I don't know why it is)

How can I fix this problem?

Thank you

My answer consists of two parts:

  • How you can plot this data in 3d.
  • What's needed to create a 3d surface plot.

Plotting your data in 3D

With the data you have - three vectors of x, y, and z, you can easily create a 3D scatter plot:

fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z,
                                   mode='markers')])

fig.show()

Here's what it looks like, and you can rotate it and swivel it in all directions.

在此处输入图像描述

3D Surface Plot

To create a 3D surface plot, you need a z-value for each combination of a and y. Think of a surface plot as a map. For each point (x,y) on the map, you need to provide the altitude (z) at that point, so that plotly can create the surface you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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