Trying to make several different figures using a loop, but figures keep building?

I am trying to create a series of graphs for several different locations using a database I’ve created. Everything works perfect when I manually input the location using location = input(“What’s your location”) in the master function, but I’m trying to automate for a long list of locations.

I created a list of counties and a for loop to accomplish the automation. Everything “works” just fine, but both fig and fig2 are being continuously added to the previous loop iteration.

Example: the first fig is fine, but the second iteration give both the first and second iterations figures superimposed on one another and fig 2 is a table that is 10 rows long, but by the 3rd iteration of the loop it is 30 rows long and so on.

Any help on clearing out the previous iteration’s data would be greatly appreciated.

def creategraph(data, location):

for i in data:
    df.append(dict(Task=i.name, Start=i.start, Finish=i.end, Resource=i.tool))

colors = ['rgb(220, 0, 0)', 'rgb(0, 255, 100)']

fig = ff.create_gantt(df, colors=colors, index_col='Resource', show_colorbar=True,
                      showgrid_x=True, showgrid_y=True, title='report for ' + location,
                      group_tasks=True)

# Initialize a figure with ff.create_table(table_data)
fig2 = ff.create_table(table, height_constant=60)

fig['layout']['annotations'] = annots

fig.write_image("PlotlyGraphs/" + location + "plotlygraph.png")
fig2.write_image("PlotlyGraphs/" + location + "plotlytable.png")

def master():

for i in counties:
    location = i
    creategraph(data,location)