1

The following function results in a pyplot with duplicate legend entries for 'Actual' (see image link below). This function is called within a larger body of code that may loop as many times as desired but, regardless of the number of iterations, the legend entry is only ever duplicated once. Furthermore, I have designed the function to be independent from iterations; the iteration variable is passed as an integer and is only used to access a corresponding csv file in the same directory.

I have tried adding pyplot.close() at the beginning and end of the function with no luck. I have also tried the solution found here, which did successfully remove the duplicate legend entry but resulted in both legend entries having the same color.

def show_total_num_tests(run_number):
    model_series = pandas.read_csv('timeSeries%s.csv' % run_number, index_col="Day", usecols=['Day', 'Tests Today'])
    actual_series = pandas.read_csv('data_total_tests.csv')
    pyplot.plot(model_series, label='Predicted')
    pyplot.plot(actual_series, label='Actual')
    pyplot.legend()
    pyplot.xlabel("Day")
    pyplot.ylabel("Number of Tests Administered")
    pyplot.title("Total Number of COVID19 Tests Administered)")
    pyplot.show()

Image of Code Output - Reputation doesn't allow embedding

DavidG
  • 24,279
  • 14
  • 89
  • 82
John Harrington
  • 1,314
  • 12
  • 36

0 Answers0