I've attached it.
Assignment - Using graphs to answer questions November 17, 2020 1 How to complete an assignment: Solve the given two Datasets below. An ecample is also provided below. You will need to get the data, then prepare the data, then present it as a graph. 1.0.1 FOR EACH DATASET you need to create a question, and then answer it with a graph. Your answer will inclide: • TWO-THREE SENTENCES long mini report: It will explain what question the graph is trying to answer and what is your attempt at the answer. (see example below). The answer does not need to be correct, but rather it needs to explain how you imagine the graph should be read/consumed. • CODE AND GRAPHS: code that creates A GRAPH OR TWO that will help to answer your question. It needs to be miningful and answer your question. Further Instructions: • use good names for things in your code (variables, functions) • use functions where appropriate • use tests for functions • prepare your data in a meaningful way • make your graphs meaningful (not necesserilly fancy, or complicated) • use classes and objects 2 Dataset 1: Countries • Get data from the api (For API see the example at the end) • Prepare the data and calculate things • Present the data as plotly graphs (any plotly graph is fine, including plotly express, search online for examples) • use classes and objects Questions for the countries api : • Are countries with more neighbours bigger in size? • If you group countries by their regions, what are their total populations and sizes? What’s the largest region in total? 1 • In Europe region is the size of country somehow connected with the fact they use Euro as currency? • What are other languages spoken in English-speaking countries? note: there’s no need to do any mathematical anlysis about these questions. Instead, create a graph that will allow the viewer to come to their own conclusions. [ ]: [ ]: [ ]: 3 Dataset 2: Weather • Get data from the api • Prepare the data and calculate things • Present the data as plotly graphs (any plotly graph is fine, including plotly express, search online for examples) • use classes and objects Questions for the Weather api : api_key = “2aee514fa2e3493e8b5100342202610” api_url_current_weather = f“http://api.weatherapi.com/v1/forecast.json?key={api_key}&q={city}&days={days}” • What is the temperature (Min, Max and Avg) in celcius in Glasgow over the next 5 days? • Compare the max wind speed in kmph in Edinburgh and Glasgow over next 5 days? • Daily Chance of rain in Glasgow over next 5 days? • What is the sunset time and moon rise time in Edinburgh over next 5 days? Come up with 2 more advance questions. note: there’s no need to do any mathematical anlysis about these questions. Instead, create a graph that will allow the viewer to come to their own conclusions. [ ]: [ ]: [ ]: [ ]: [ ]: 2 4 Mini-report and Code Example: 4.0.1 As mentioned ealrier, use code with calsses and objects as a coding style. 4.0.2 Example question and mini-report Question: More people in a country would mean you need more languages to speak, right? What are countries where people speak most languages? Is that somehow obviously connected with their size? Is it about the region? Or maybe about languages used by former colonial countries, like English or Spanish? Answer: It’s hard to say. On the graph below size of bubbles indicates number of languages spoken in each country, and it is possible that countries in Africa have most different official languages. In the second graph we can see that many countries speak Spanish or English, and indeed they seem to be the ones speaking more different languages. This could be because of colonisation among other reasons. [1]: ### use classes and objects like this: import requests import pprint as pp import plotly.express as px # notice we are doing all the data cleanup in __init__ class Country: def __init__(self, dictionary_from_api): self.name = dictionary_from_api['name'] self.region = dictionary_from_api['region'] self.area = dictionary_from_api['area'] if dictionary_from_api['area']␣ ↪→!= None else 0 self.languages = [language['name'] for language in dictionary_from_api['languages']] self.population = dictionary_from_api['population'] if␣ ↪→dictionary_from_api['population'] != None else 0 def population_density(self): if self.area != 0: return self.population / self.area else: return 0 def number_of_languages(self): return len(self.languages) def speaks_any_of_these_languages(self, seaked_languages): for seaked_language in seaked_languages: if seaked_language in self.languages: return True 3 return False [2]: def get_api_results_all_countries(): api_url = "https://restcountries.eu/rest/v2/all" response = requests.request("GET", api_url) return response.json() def data_into_objects(list_with_country_dictionaries): country_objects = [Country(country_dictionary) for country_dictionary in list_with_country_dictionaries] return country_objects def prepare_data(country_objects): return { 'area': [ country.area for country in country_objects], 'population': [ country.population for country in country_objects], 'nuber_of_languages': [country.number_of_languages() for country␣ ↪→in country_objects], 'name': [country.name for country in country_objects], 'region': [country.region for country in country_objects], 'speaks_spanish': [country. ↪→speaks_any_of_these_languages(["English","Spanish"]) for country in␣ ↪→country_objects], 'description': [f"{country}" for country in country_objects] } def prepare_diagram(city_data): fig = px.scatter(city_data, x="area", y="population", size="nuber_of_languages", color="speaks_spanish", hover_name="name", hover_data=["description"]) return fig [3]: countries_data = get_api_results_all_countries() countries_objects = data_into_objects(countries_data) countries_cleaned = prepare_data(countries_objects) figure = prepare_diagram(countries_cleaned) figure.show() [ ]: # just to see the data: pp.pprint(countries) [ ]: 4 How to complete an assignment: FOR EACH DATASET you need to create a question, and then answer it with a graph. Your answer will inclide: Dataset 1: Countries Dataset 2: Weather Mini-report and Code Example: As mentioned ealrier, use code with calsses and objects as a coding style. Example question and mini-report