I am working on a project where I need to create a 3D surface plot. The dataset “x y z color” is in a non uniform grid.
enter image description here
Additionally, I have a separate file containing a set of points that form the left boundary of the surface.
I am trying to interpolate the surface data within this boundary and plot it in 3D. However, I am struggling with correctly interpolating the data to conform to the concave shape of the boundary and excluding data outside this area.
enter image description here
Here is the data for the boundary
0.5000011312772511 0.9999503225071016 0.010000442395794491 0.005
0.500001140089003 0.9998002675422002 0.020003885702647665 0.01
0.5000011798112299 0.999550025819314 0.030013333831617344 0.015
0.5000012880768092 0.9991993719317389 0.04003179670142108 0.02
0.5000015177307953 0.9987479898351447 0.05006229325944634 0.025
0.5000019369886614 0.9981954723749855 0.0601078545158198 0.03
0.5000026296433487 0.99754132067445 0.07017152661573131 0.035
0.5000036953188669 0.9967849433839776 0.08025637392316401 0.04
0.5000052497752266 0.9959256557902396 0.09036548217238696 0.045
0.5000074252622885 0.9949626787843852 0.10050196163782579 0.05
0.50001037092775 0.9938951376862389 0.11066895034826003 0.055
0.5000142532806519 0.9927220609188651 0.1208696174503791 0.06
0.5000192567121552 0.9914423785382339 0.13110716646585244 0.065
0.5000255840757588 0.9900549206052752 0.14138483880658953 0.07
0.5000334573344162 0.988558415409107 0.15170591725634755 0.075
0.5000431182722865 0.9869514875231834 0.1620737295921134 0.08
0.5000548292798774 0.9852326557027417 0.1724916522825041 0.085
0.5000688742149383 0.9834003306119743 0.1829631143141833 0.09
0.500085559345211 0.9814528123825679 0.1934916011003794 0.095
0.5001052143776197 0.9793882879909978 0.20408065857909385 0.1
0.5001281935810702 0.9772048284473063 0.21473389738046406 0.105
0.5001548770105084 0.9749003858220506 0.22545499720357498 0.11
0.5001856718379002 0.9724727900384286 0.23624771129161323 0.115
0.5002210138008032 0.9699197454999648 0.24711587112097785 0.12
0.5002613687769464 0.9672388274922852 0.25806339128651706 0.125
0.5003072344956199 0.9644274783732285 0.2690942745497424 0.13
0.5003591423985235 0.9614830035591169 0.28021261710569323 0.135
0.5004176596613754 0.9584025672581398 0.2914226140959445 0.14
0.5004833913919013 0.9551831879700036 0.30272856539803655 0.145
0.500556983020396 0.951821733801294 0.3141348815354828 0.15
0.5006391229012028 0.9483149173990695 0.3256460900918845 0.155
0.500730545142759 0.9446592907928485 0.33726684218940073 0.16
0.5008320326920536 0.940851239827587 0.34900191947516196 0.165
0.500944420695111 0.9368869784045428 0.3608562413853676 0.17
0.5010686001648622 0.9327625423973618 0.37283487275017296 0.175
0.5012055219830495 0.928473783303336 0.3849430317611802 0.18
0.5013562012767159 0.9240163615818817 0.39718609850015574 0.185
0.5015217222030115 0.9193857396872898 0.40956962380599543 0.19
0.5017032431923294 0.9145771747822609 0.4220993386567344 0.195
0.501902002694183 0.9095857111687172 0.43478116401354083 0.2
0.502119325487346 0.9044061723537689 0.44762122137807864 0.205
0.5023566296137947 0.8990331528041908 0.4606258437802666 0.21
0.5026154340122538 0.8934610093820474 0.4738015875578159 0.215
0.5028973669323259 0.8876838524725617 0.487155244591037 0.22
0.5032041752214875 0.8816955367253859 0.5006938556171434 0.225
0.503537734593336 0.8754896515619643 0.5144247239954818 0.23
0.5039000610005778 0.8690595113277488 0.5283554306160175 0.235
0.5042933232495755 0.8623981451554441 0.5424938495011189 0.24
0.5047198570189394 0.8554982865920885 0.5568481645646254 0.245
0.505182180467757 0.8483523629991662 0.571426887368682 0.25
0.5056830116471486 0.8409524846489255 0.5862388759594233 0.255
0.5062252879618473 0.8332904338346913 0.6012933551917243 0.26
0.5068121879699291 0.8253576537523871 0.6165999381272089 0.265
0.5074471558569453 0.8171452374200945 0.6321686488743842 0.27
0.5081339289719434 0.8086439166972035 0.6480099473788276 0.275
0.5088765688896839 0.7998440514298645 0.664134755469705 0.28
0.5096794965298042 0.7907356190107414 0.6805544849433707 0.285
0.5105475319783913 0.7813082043538541 0.697281067723305 0.29
0.5114859397561708 0.7715509906409421 0.7143269878796238 0.295
0.512500480435574 0.7614527510047073 0.7317053165797631 0.3
0.5135974696698373 0.7510018414615505 0.7494297488503675 0.305
0.5147838459176807 0.740186195506984 0.7675146437743293 0.31
0.5160672484029836 0.7289933207357892 0.7859750673037237 0.315
0.5174561071761141 0.7174102981323879 0.8048268386720585 0.32
0.5189597475375545 0.7054237845375272 0.8240865801376177 0.325
0.520588511593822 0.6930200192335912 0.8437717710869546 0.33
0.5223539003403157 0.6801848354401041 0.8639008058846505 0.335
0.5242687404687704 0.6669036779648141 0.8844930569937796 0.34
0.5263473811110114 0.6531616283650671 0.9055689430871349 0.345
0.5286059270326668 0.6389434392782417 0.9271500026689459 0.35
0.5310625164900867 0.6242335800042101 0.949258974475845 0.355
0.5337376541491514 0.6090162958169182 0.9719198840753223 0.36
0.5366546123587664 0.593275684061104 0.9951581387880066 0.365
0.5398399179112119 0.5769957907657641 1.0190006305394426 0.37
0.5433239465414645 0.5601607324195828 1.0434758481846664 0.375
0.5471416543696762 0.542754848542136 1.0686140000559379 0.38
0.5513334849823286 0.5247628921916805 1.0944471479593683 0.385
0.555946503999085 0.5061702671604679 1.1210093538485488 0.39
0.5610358314137184 0.48696332291185224 1.1483368404733023 0.395
0.5666664682309694 0.4671297212804191 1.1764681678504938 0.4
0.5729156518012448 0.4466588925349565 1.2054444271037783 0.405
0.5798759299003311 0.4255426035576688 1.2353094540383123 0.41
0.587659226805283 0.4037756673954496 1.2661100644503391 0.415
0.5964023016218197 0.38135683243129664 1.2978963140511428 0.42
0.6062741973081358 0.35828990153063617 1.3307217862129004 0.425
0.6174865959370622 0.33458514865452355 1.3646439105978794 0.43
0.6303085174484443 0.31026112433532643 1.3997243169603169 0.435
0.6450876849760825 0.28534697660993835 1.4360292288480574 0.44
0.6622824390257734 0.25988546553037967 1.4736299021986068 0.445
0.6825109430619073 0.23393692932442386 1.5126031150400912 0.45
0.7066299291512341 0.2075845860056578 1.5530317156231248 0.455
0.7358664520942582 0.1809417628557541 1.595005236996346 0.46
0.7720505997414752 0.1541620087933328 1.6386205876967788 0.465
0.7803478711012877 0.14880678468070216 1.6475497697994446 0.466
0.7890619588121246 0.14345616115067164 1.6565496807482705 0.467
0.7982259432767803 0.13811225005513658 1.665621217339594 0.468
0.8078766455473015 0.13277730162988333 1.6747652913445186 0.469
0.8180551867790513 0.12745371512795517 1.6839828298942647 0.47
0.8288076532574393 0.12214405056999607 1.6932747758764743 0.471
0.8401858913443033 0.11685104176582206 1.7026420881087545 0.472
0.8522484634213099 0.1115776107989804 1.7120857417502302 0.473
0.8650618047847557 0.10632688420889695 1.721606728701647 0.474
0.878701633362084 0.1011022111057068 1.7312060579530748 0.475
0.893254680178963 0.09590718360247748 1.7408847558255638 0.476
0.9088208304564052 0.09074565990947925 1.7506438665467137 0.477
0.9255157956083957 0.085621790604625 1.760484452466698 0.478
0.9434744788921962 0.08054004868814157 1.7704075945912794 0.479
0.9628552579224899 0.07550526416668894 1.780414392920661 0.48
0.9838454943605606 0.07052266413378144 1.790505966798538 0.481
1.0066687089066437 0.06559791956668944 1.8006834555142062 0.482
1.0315940506140184 0.06073720042200571 1.8109480186202576 0.483
1.058948980531323 0.0559472411013757 1.8213008363738372 0.484
1.0891365431350983 0.05123541905570999 1.8317431102332167 0.485
1.1226593237110865 0.04660985025746674 1.842276063343787 0.486
1.160153380499215 0.04207950669941212 1.8529009409328063 0.487
1.2024374596138432 0.03765436317787694 1.863619010782999 0.488
1.2505863501420222 0.033345583841344124 1.8744315636500313 0.489
1.3060437398271136 0.02916576404673712 1.8853399136066773 0.49
1.3708024339438873 0.025129251356129784 1.8963453981062126 0.491
1.4477052429276849 0.0212525836145188 1.9074493777484796 0.492
1.5409752839578719 0.017555107349163778 1.9186532345311706 0.493
1.6572158061090254 0.014059887989824724 1.9299583672804796 0.494
1.8074656435341185 0.010795122763222214 1.9413661760516001 0.495
2.011943167544061 0.007796493207890757 1.952878008300029 0.496
2.3129488558303493 0.005111482675285709 1.9644949197594892 0.497
2.8202249897828557 0.0028085468814210922 1.9762161255682387 0.498
3.9708933572086713 0.0010021701642300709 1.9880169544472097 0.499
Here is the code I am currently using
import pandas as pd
import plotly.graph_objects as go
import numpy as np
from scipy.spatial import Delaunay
# Read data
def read_data(file_name):
return pd.read_csv(file_name, delim_whitespace=True, header=None, names=['x', 'y', 'z', 'color'])
# Read the surface data
data = read_data('3dplot.dat')
# Prepare points for Delaunay triangulation
points = data[['x', 'y']].values
# Create a Delaunay triangulation
tri = Delaunay(points)
# Extract vertices and create lists for triangles
i, j, k = tri.simplices[:, 0], tri.simplices[:, 1], tri.simplices[:, 2]
# Custom color scale definition
custom_colorscale = [
[0, "#6446af"], # Start color
[0.5, "#818aa3"], # Middle color
[1, "#c5dca0"], # End color
]
# Create a 3D mesh plot
fig = go.Figure()
# Add the surface plot
fig.add_trace(go.Mesh3d(
x=data['x'], y=data['y'], z=data['z'],
i=i, j=j, k=k,
intensity=data['color'],
colorscale=custom_colorscale,
colorbar=dict(
title='Φ<sub>H</sub>',
tickfont=dict(size=14, family='Times New Roman'),
x=0.9,
len=0.75,
thickness=25
),
name='Surface',
opacity=1.
))
# Layout
fig.update_layout(
title='Data',
width=800, # Width of the figure in pixels
height=600, # Height of the figure in pixels
scene=dict(
xaxis_title=dict(text=r'X', font=dict(size=16, family='serif')),
yaxis_title='Y',
zaxis_title='Z',
xaxis=dict(
showbackground=True, backgroundcolor="white", gridcolor="black", title=dict(font=dict(size=16, family='serif')),
showline=True, linecolor="black", linewidth=1, showspikes=False,
range=[data['x'].min(), data['x'].max()]
),
yaxis=dict(
showbackground=True, backgroundcolor="white", gridcolor="black", title=dict(font=dict(size=16, family='serif')),
showline=True, linecolor="black", linewidth=1, showspikes=False,
range=[0, data['y'].max()]
),
zaxis=dict(
showbackground=True, backgroundcolor="white", gridcolor="black", title=dict(font=dict(size=16, family='serif')),
showline=True, linecolor="black", linewidth=1, showspikes=False,
range=[data['z'].min(), data['z'].max()] # Define the range
),
aspectmode='manual',
aspectratio=dict(x=1, y=1, z=0.7),
),
margin=dict(l=0, r=0, b=0, t=0)
)
# Gridline properties
fig.update_scenes(
xaxis_showgrid=True,
yaxis_showgrid=True,
zaxis_showgrid=True,
xaxis_gridwidth=1,
yaxis_gridwidth=1,
zaxis_gridwidth=1,
)
fig.update_layout(
scene_camera=dict(
eye=dict(x=-0.9, y=1.5, z=0.5),
center=dict(x=0, y=0, z=-0.1),
up=dict(x=0, y=0, z=1)
)
)
# Show the figure
fig.show()
Etevaldo Costa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.