from defusedxml import ElementTree from collections import deque import matplotlib.pyplot as plt data = {"Germany": deque() , "France": deque() , "Italy": deque() , "United States": deque() } with open("data/API_NY.GDP.MKTP.KN_DS2_en_xml_v2_10230884.xml") as fin: tree = ElementTree.parse(fin) for record in tree.getroot().find("data").findall("record"): this_data = {field.get("name"): field.text for field in record.findall("field")} if(this_data["Country or Area"] in data): if(this_data["Value"] != None): data[this_data["Country or Area"]].append((this_data["Year"], this_data["Value"])) class Data(object): def __init__(self, raw_data): self.x = [int(k) for k, v in raw_data] self.y = [float(v) for k, v in raw_data] data = {k: Data(v) for k,v in data.items()} plots = [plt.plot(v.x, v.y, label=k)[0] for k, v in data.items()] plt.legend(handles = plots) plt.show()