How to create stacked barchart using pygal

22 views Asked by At

I would like to create line chart using Pygal. The output should have 3 lines - 1 line for each category against the year. I have this error message: "NameError: name 'x' is not defined"

import pygal as pg
import pandas as pd
import csv
import numpy

filename = 'data.csv'
with open(filename, 'r') as f:
    reader = csv.reader(f)
    #print(list(reader))
    pg_bar = pg.Bar()
    for row in reader:
            x.append(int(row[0]))
            y.append(int(row[1]))
    
pg_bar.render_to_file("simple.svg")

Sample of the data is shown as below. data

Can someone advise where I have done wrongly?

1

There are 1 answers

0
Ryan McGreal On

It looks like you have not initialized your x and y variables. Try something like this:

import pygal as pg
import csv

x = [] # initialize x 
y = [] # initialize y
filename = 'data.csv'
with open(filename, 'r', encoding='utf8') as f:
    reader = csv.reader()
    for row in reader:
        x.append(int[row[0]))
        y.append(int[row[1]))
    pg_bar = pg.Bar()
    pg_bar.add('X', x)
    pg_bar.add('Y', y)
    pg_bar.render_to_file('simple.svg')