3b1b / manim

Animation engine for explanatory math videos
MIT License
62.98k stars 5.83k forks source link

Unexpected error when plotting data points #948

Open lewis-cooper opened 4 years ago

lewis-cooper commented 4 years ago

Trying to plot some data points using sublime text and I get this error. Any help would be appreciated.

Command ran:

from manimlib.imports import *

#Function to return data from .csv file to an array

def get_coords_from_csv(coronavirus_data):
    import csv
    coords = []
    with open(f'coronavirus_data.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

class GraphFromData(GraphScene):
    def get_points_from_coords(self,coords):
        return[
        self.coords_to_point(px,py)
        for px,py in coords
        ]

    def get_dots_from_coords(self, coords, radius=0.1):
        points = self.get_points_from_coords(coords)
        dots = VGroup(*[
            Dot(radius=radius).move_to([px,py,pz])
            for px,py,pz in points
            ]
        )
        return dots

class DiscreteGraphFromSetPoints(VMobject):
    def __init__(self, set_of_points, **kwargs):
        super().__init__(**kwargs)
        self.set_points_as_corners(set_of_points)

class SmoothGraphFromSetPoints(VMobject):
    def __init__(self, set_of_points, **kwargs):
        super().__init__(**kwargs)
        self.set_points_smoothly(set_of_points)

#Graph with set of points
class CustomGraph1(GraphFromData):
    def construct(self):
        self.setup_axes()
        coords = get_coords_from_csv("coronavirus_data")
        dots = self.get_dots_from_coords(coords)
        self.add(dots)

The error: Traceback (most recent call last): File "/Users/lewis/Documents/animation/manim/manimlib/extract_scene.py", line 155, in main scene = SceneClass(**scene_kwargs) File "/Users/lewis/Documents/animation/manim/manimlib/scene/scene.py", line 53, in init self.construct() File "coronavirus.py", line 49, in construct coords = get_coords_from_csv("coronavirus_data") File "coronavirus.py", line 13, in get_coords_from_csv coord = [float(x),float(y)] ValueError: could not convert string to float: '\ufeff1'

Many thanks :)

andre-caldas commented 4 years ago

This issue is not related to manim.

Your CSV file has a "byte order mark" (BOM- FEFF). Don't be upset... BOM is designed to break things that should work. :-/

Just remove the first byte from your CSV. I think it is a good idea to use a good editor that does not put invisible garbage into your file.