简体   繁体   中英

Create a 3D surface plot in Plotly

I want to create a 3D surface plot in Plotly by reading the data from an external file. Following is the code I am using:

    import numpy as np
    import plotly.graph_objects as go 
    import plotly.express as px
    
    
    data = np.genfromtxt('values.dat', dtype=float)
    
    # The shape of X, Y and Z is (10,1)
    X = data[:,0:1] 
    Y = data[:,1:2]
    Z = data[:,2:3]
    
    fig = go.Surface(x=X, y=Y, z=Z, name='Surface plot', colorscale=px.colors.sequential.Plotly3)
    plot(fig)

The above code does not produce any surface plot. What changes has to be made to create a surface plot?

From plotly figure reference:

The data the describes the coordinates of the surface is set in z . Data in z should be a 2D list. Coordinates in x and y can either be 1D lists or {2D arrays}

I have an example data set. It contains three columns (x,y,z).

import plotly.graph_objects as go
import pandas as pd
import numpy as np
from scipy.interpolate import griddata


df = pd.read_csv('./test_data.csv')

x = np.array(df.lon)
y = np.array(df.lat)
z = np.array(df.value)


xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)

X,Y = np.meshgrid(xi,yi)

Z = griddata((x,y),z,(X,Y), method='cubic')



fig = go.Figure(go.Surface(x=xi,y=yi,z=Z))
fig.show()

Reference Page: https://plotly.com/python/reference/surface/

数字

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