0

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?

Jonas
  • 29
  • 2
  • 1
    Welcome to StackOverflow! Please provide some examples of what you have tried and what is not working. Here's an example of [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) & also some tips on [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – John Veldboom Sep 21 '19 at 15:54

1 Answers1

5

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.

TheoremOfBeethoven
  • 1,864
  • 6
  • 15
  • Hi I get this error message when running this `AttributeError: 'CSV' object has no attribute 'coords_to_point'`. Do you know why? – giac Mar 09 '21 at 19:11
  • 1
    Possibly it is due to the version of Manim you have, if you are using ManimGL it does not have GraphScene defined, so you will have to edit the code, you have to use the cairo-backend branch for this code to work for you. I recommend that you update the code yourself using the Axes class to create the graphs. If you need more help, you can request it on Manim's Discord (see link in the GitHub repo) server, if you want private help visit this [link](https://zavden.github.io/freelance-job/docs/html/jobs/tutorials.html). – TheoremOfBeethoven Mar 09 '21 at 19:20
  • thanks a lot. Maybe it would be interesting to read the data using `Pandas`? Might be more flexible. – giac Mar 09 '21 at 19:29
  • 1
    Sure, it's up to you what you want to do. – TheoremOfBeethoven Mar 09 '21 at 19:41
  • 1
    Do you know if there are major changes with `coords_to_point`? The command seems to have disappeared – giac Mar 11 '21 at 18:48
  • Any chance of updating this nugget @TheoremOfBeethoven? I guess its defunct due to many Manim updates. – Magnus Nordmo Nov 03 '22 at 18:31