pyvista / pyvista-support

[moved] Please head over to the Discussions tab of the PyVista repository
https://github.com/pyvista/pyvista/discussions
59 stars 4 forks source link

Creating surface from a point cloud in two-dimension #275

Open forbonder opened 3 years ago

forbonder commented 3 years ago

Description

I have a set of coordinates in 2D (x,y), and corresponding value u. I want to create a surface plot from these data and if possible save it as .eps or .pdf. Here are my codes in Python for scatter plot of the data. Figure_2

u = pd.read_csv('u.csv')
domains = pd.read_csv("domain.csv")

u1 = u.iloc[0:735,0].values
u2 = u.iloc[0:2161,1].values

d1 = domains.iloc[0:735,0:2].values
d2 = domains.iloc[0:2161,2:4].values

Cen3D = plt.figure()
ax = Cen3D.add_subplot(111, projection='3d')
ax.scatter(d1[:,0],d1[:,1],u1,cmap='viridis',c=u1,s=2, zorder=1) 
ax.scatter(d2[:,0],d2[:,1],u2,cmap='viridis',c=u2,s=2, zorder=2)
ax.set_title(r"scat", fontsize=10) 
ax.grid(False)

ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))    
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('U')
ax.set_xticks([-1,0,1])
ax.set_yticks([0,1.5,3]) 

I am completely new to PyVista Is it possible to transform this concavely shaped scatter plot to a surface plot with pyvista?

Example Data

Data for scatter plot data.zip

akaszynski commented 3 years ago

This isn't perfect, mostly because delanuay_2d would create a smoother mesh with a bounding line, but it works. Approach is:

import numpy as np
import pandas as pd
import pyvista as pv

u = pd.read_csv('u.csv')
domains = pd.read_csv("domain.csv")

u1 = u.iloc[0:735,0].values
u2 = u.iloc[0:2161,1].values

d1 = domains.iloc[0:735,0:2].values
d2 = domains.iloc[0:2161,2:4].values

flower_pts = pv.PolyData(np.hstack((d1, u1.reshape(-1, 1))))
surf_pts = pv.PolyData(np.hstack((d2, u2.reshape(-1, 1))))

# point plot
pl = pv.Plotter()
pl.add_mesh(flower_pts, color='y', smooth_shading=True)
pl.add_mesh(surf_pts, color='orange')
# pl.camera_position = 'xy'
pl.show()

image

# mesh plot
flower_mesh = flower_pts.delaunay_2d(alpha=0.1)
surf_mesh = surf_pts.delaunay_2d(alpha=0.08)

pl = pv.Plotter()
pl.add_mesh(flower_mesh, color='y', smooth_shading=True)
pl.add_mesh(surf_mesh, color='orange')
pl.show()

image

akaszynski commented 3 years ago

Save as a pdf screenshot with:

pl = pv.Plotter()
pl.add_mesh(flower_mesh, color='y', smooth_shading=True)
pl.add_mesh(surf_mesh, color='orange')
pl.show(auto_close=False)
pl.save_graphic('flower_power.pdf')
pl.close()
forbonder commented 3 years ago

Thank you for your reply. However when I tried to save as pdf a blank pdf file is created. Any idea on how to fix that? My system info is as follows


            OS : Linux
        CPU(s) : 12
       Machine : x86_64
  Architecture : 64bit
           RAM : 15.5 GB
   Environment : Jupyter
    GPU Vendor : Intel
  GPU Renderer : Mesa Intel(R) UHD Graphics 630 (CFL GT2)
   GPU Version : 4.6 (Core Profile) Mesa 20.1.8

Python 3.8.3 | packaged by conda-forge | (default, Jun 1 2020, 17:43:00) [GCC 7.5.0]

       pyvista : 0.26.1
           vtk : 9.0.1
         numpy : 1.19.1
       imageio : 2.9.0
       appdirs : 1.4.4
        scooby : 0.5.6
        meshio : 4.3.2
    matplotlib : 3.3.1
         PyQt5 : 5.12.3
       IPython : 7.18.1
         scipy : 1.5.2
          tqdm : 4.50.2

Intel(R) Math Kernel Library Version 2019.0.4 Product Build 20190411 for Intel(R) 64 architecture applications