×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Read And Plot Data Expectancy in The Python Assignment Solution

July 01, 2024
Dr. Lauren Chen
Dr. Lauren
🇦🇺 Australia
Python
Dr. Lauren Chen, a seasoned expert with 7 years of experience, is a doctorate of Yale University. With an impressive track record of completing over 500 Python assignments, she possesses a profound understanding of complex programming concepts. Dr. Chen's dedication to excellence and her ability to simplify intricate topics make her an invaluable resource for students seeking guidance in Python programming.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Familiarize yourself with OCaml's pattern matching; it simplifies handling recursive data structures like lists and trees, making your code concise and easier to debug.
News
In 2024, Girls Who Code introduced a Data Science + AI track in their free summer programs for high school students, fostering skills in cybersecurity and creative coding​

Instructions

Objective
Write a python assignment program to read and plot expectancy.

Requirements and Specifications

Program to read and plot expectancy in python

Source Code

# Your Name

# PA_14

import numpy as np

import matplotlib.pyplot as plt

import re

def build_plot(map):

    """

    Method for building plot from given data dictionary

    :param map: data dictionary

    :return: None

    """

    # getting years values as x-axis

    years = map['Year']

    axes = plt.gca()

    # setting plot view parameters

    axes.set_title('Health Life Expectancy from 2007 to 2017')

    axes.set_xlim(2007, 2017)

    axes.set_ylim(70, 75)

    axes.set_xlabel('Year')

    axes.set_ylabel('Age')

    axes.set_title('Health Life Expectancy from 2007 to 2017')

    # creating graphs for each of country

    plt.plot(np.array(years), np.array(map['Canada']), color="red", linewidth=2, linestyle="-", label='Canada')

    plt.plot(np.array(years), np.array(map['Italy']), color="green", linewidth=2, linestyle="-", label='Italy')

    plt.plot(np.array(years), np.array(map['New Zealand']), color="blue", linewidth=2, linestyle="-", label='New Zealand')

    plt.plot(np.array(years), np.array(map['Spain']), color="black", linewidth=2, linestyle="-", label='Spain')

    # showing legend

    axes.legend()

    # saving picture as png

    plt.savefig('life_exp.png')

    # showing result plot

    plt.show()

def read_data(filename):

    """

    Method for reading data from given filename

    :param filename: file to read data from

    :return: dictionary, containing read data

    """

    # result dictionary: maps column name to value list

    result = {}

    # mapping column index to column name

    columns = {}

    # reading csv file

    with open(filename, 'r') as csvfile:

        # reading lines

        lines = csvfile.read().splitlines()

        # removing 'wrong' byte at the beginning of file

        lines[0] = re.sub(r"[^ a-zA-Z,]", "", lines[0])

        # getting headers

        header = lines[0].split(',')

        # processing header values

        for i in range(len(header)):

            columns[i] = header[i]

            result[header[i]] = []

        # parsing line by line and enriching collected data

        for line in lines[1:]:

            parts = line.split(',')

            for i in range(len(parts)):

                result[columns[i]].append(float(parts[i]))

    return result

if __name__ == '__main__':

    # filename to read data from

    filename = 'HealthyLifeExpectancy.csv'

    # reading data and building plot

    build_plot(read_data(filename))

Related Samples

Explore Python Assignment Samples: Dive into our curated selection showcasing Python solutions across diverse topics. From basic algorithms to advanced data structures, each sample illustrates clear problem-solving strategies. Enhance your understanding and master Python programming with our comprehensive examples.