Search code examples
pythonmanim

Is there a way to create a Scatterplot in manim?


I am wondering if there is a way to create a Scatterplot in manim.

Has anyone ever done it? if yes, what is the best way to do it?


Solution

  • The best way (to me) is to use .csv files.

    Imagine we have a file called data.csv with the following data:

    0,0
    1,0
    -2,3
    -4,8
    1,-4
    3,4
    

    This file go in the manim-itself folder, to be able to include it to manim it could be done as follows (remember that the coordinates in manim are in 3D)

    class CSV(GraphScene):
        def construct(self):
            self.setup_axes()
            coords = self.return_coords_from_csv("data")
            dots = VGroup(*[Dot().move_to(self.coords_to_point(coord[0],coord[1])) for coord in coords])
            self.add(dots)
    
        def return_coords_from_csv(self,file_name):
            import csv
            coords = []
            with open(f'{file_name}.csv', 'r') as csvFile:
                reader = csv.reader(csvFile)
                for row in reader:
                    x,y = row
                    coord = [float(x),float(y)]
                    coords.append(coord)
            csvFile.close()
            return coords
    

    Idea from reddit.