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

Plotting Earthquake Hypocenters as Time Series Data #317

Open AlexanderJuestel opened 3 years ago

AlexanderJuestel commented 3 years ago

Description

Hello,

I would like to plot earthquake data as Time Series with PyVista, ideally having a slider or a sort of "play button" to start adding data. The data used for the slider would be the year the earthquake occured. So while time progresses, more and more events should appear in the plotting window.

Example Data

I have attached the sample data.

What I have accomplished so far is plotting each single event as sphere and adjust its size and color to the magnitude of the event. Is there a way to combine the single spheres like done for the lines in #310 and then somehow plot the data by year by adjusting a slider?

image

data = pd.read_csv(file_path + 'earthquakes_aachen.csv', delimiter=';').dropna()
data = data[data['Magnitude'] != '-']
data = data[data['Tiefe [km]'] != '-']

data['Magnitude'] = pd.to_numeric(data['Magnitude'])
data['Tiefe [km]'] = pd.to_numeric(data['Tiefe [km]'])
data['Date'] = pd.to_datetime(data['Date'])
data['Year'] = pd.DatetimeIndex(data['Date']).year

data['X'] = pd.to_numeric(data['X'])
data['Y'] = pd.to_numeric(data['Y'])
data['Z'] = pd.to_numeric(data['Z'])

spheres = [pv.Sphere(radius=data_df.loc[i]['Magnitude']*200, center=data_df.loc[i][['X', 'Y', 'Z']].tolist()) for i in range(len(data_df))]

for i in range(len(spheres)):
    spheres[i]['Magnitude'] = np.zeros(len(spheres[i].points)) + data_df.loc[i]['Magnitude']
    spheres[i]['Year'] = np.zeros(len(spheres[i].points)) + data_df.loc[i]['Year']

sargs = dict(fmt="%.1f", color='black')

p = pv.Plotter(notebook=False)

for i in spheres:
    p.add_mesh(i,scalars='Magnitude', cmap='Reds', clim=[0,6],scalar_bar_args=sargs)

p.set_background('white')
p.show_grid(color='black')
p.show()

Looking forward to some hints and tipps :)

earthquakes_aachen.txt

banesullivan commented 3 years ago

This is awesome and I want to provide some help but am lacking time at the moment. Hopefully, these tips help:

Is there a way to combine the single spheres like done for the lines in #310

Yep, you can either merge all the spheres into a single unstructured grid or put them all in a MultiBlock dataset. Since you already have the spheres as a list, you can just pass that to the MultiBlock constructor:

spheres = pv.MultiBlock([pv.Sphere(radius=data_df.loc[i]['Magnitude']*200, center=data_df.loc[i][['X', 'Y', 'Z']].tolist()) for i in range(len(data_df))])

...

# Remove the for loop plotting
p.add_mesh(spheres, scalars='Magnitude', cmap='Reds', clim=[0,6],scalar_bar_args=sargs)

then somehow plot the data by year by adjusting a slider?

See if you can create a multiblock like this for each year. Then track which data to show by having some sort of class that will hold all of that data and have callbacks for updating the data shown in the viewer.

At the moment, we only have slider widgets to control the timestep but we could add this time widget: https://vtk.org/Wiki/File:PlaybackWidget.png

Otherwise, I'd use ipywdigets or a qt interface with BackgroundPlotter

Hope this helps!