[Matplotlib-users] Add and remove points to a scatter plot repeatedly

Scott Lasley selasley at icloud.com
Tue Dec 3 15:34:53 EST 2019


You could use animation.  See https://stackoverflow.com/questions/41602588/matplotlib-3d-scatter-animations 

Here is a short example based on the Matplotlib wireframe example (https://matplotlib.org/3.1.1/gallery/mplot3d/wire3d.html) and your two sets of points.  This will work in a Jupyter notebook if you use the  %matplotlib notebook  magic command but the animation does not work with jupyter lab.


import numpy as np
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import matplotlib.animation


def update_graph(num):
    # alternate between the 2 sets of points
    num = 3 * (num % 2)
    data = points[num:num+3]
    dots.set_data (data[:,0], data[:,1])
    dots.set_3d_properties(data[:,2])
    return dots, 


fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')

# Plot a basic wireframe.
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10, color='green')

# set up the points to be plotted
points_list = np.array([(2,6),(30,14),(39,15)])
points = np.append(points_list, 
                  Z[points_list[:,0], points_list[:,1]].reshape(3,1),
                  1)
points_list2 = np.array([(5,23),(24,4),(12,45)])
points = np.append(points, 
                  np.append(points_list2, Z[points_list2[:,0], points_list2[:,1]].reshape(3,1), 1),
                  0)

dots, = ax.plot(points[:,0][:3], points[:,1][:3], points[:,2][:3], 
                linestyle="", marker="o", c='r')


ani = matplotlib.animation.FuncAnimation(fig, update_graph, 2, 
                               interval=500, blit=True)
plt.show()


I ran the example with matplotlib 3.1.1 in python 3.8.

Hth,
Scott


> On Dec 3, 2019, at 10:09 AM, regev81 <regev81 at gmail.com> wrote:
> 
> I am trying to add and remove points to a scatter plot repeatedly, each time
> with a different set of points,  while a wireframe plot is in the
> background.
> is there a way to do so with out closing the all figure window and the
> background wireframe plot?
> 
> Thanks
> 
> Here is my python code:
> import gdal
> from mpl_toolkits.mplot3d.axes3d import *
> import matplotlib.pyplot as plt
> from pathlib import Path
> 
> #import the raster tif file and convert to 2d array
> dataset = gdal.Open("dem_demo.tif")
> dem_arr = dataset.ReadAsArray()
> 
> #slicing
> #dem_arr[start row:end row,start col:end col]
> sliced_dem_arr = dem_arr[100:151,100:151]
> rowCount = sliced_dem_arr.shape[0]
> colCount = sliced_dem_arr.shape[1]
> 
> #set the X, Y, Z arrays for plotting process
> rowArr = np.arange(1,rowCount+1)
> colArr = np.arange(1,colCount+1)
> X, Y = np.meshgrid(rowArr, colArr)
> Z = sliced_dem_arr
> 
> #set 3d view ans labels
> ax = plt.axes(projection='3d')
> ax.set_xlabel('x')
> ax.set_ylabel('y')
> ax.set_zlabel('z')
> 
> ############plotting the dtm ########################
> ###wireframe
> ax.plot_wireframe(X, Y, Z, color='green')
> 
> #Add points #01
> points_list = [(2,6),(30,14),(39,15)]
> for p in points_list:
>    ax.scatter(p[0], p[1], sliced_dem_arr[p[0],p[1]], c = 'r')
> plt.show()
> 
> #Add points #02
> points_list2 = [(5,23),(24,4),(12,45)]
> for p in points_list2:
>    ax.scatter(p[0], p[1], sliced_dem_arr[p[0],p[1]], c = 'r')
> plt.show()
> 
> <http://matplotlib.1069221.n5.nabble.com/file/t5275/example.jpeg> 
> 
> 
> 
> --
> Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users



More information about the Matplotlib-users mailing list