0% found this document useful (0 votes)
10 views10 pages

Exp 9 Python

The document outlines an experiment focused on using the Matplotlib library in Python to plot various types of graphs, including line, bar, and scatter plots. It provides a theoretical background on Matplotlib's capabilities, implementation details with code examples, and concludes with the importance of mastering this library for effective data visualization in fields like data science and engineering. The experiment includes practical examples of graph plotting using datasets such as gas prices and FIFA player statistics.

Uploaded by

ranatirth7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Exp 9 Python

The document outlines an experiment focused on using the Matplotlib library in Python to plot various types of graphs, including line, bar, and scatter plots. It provides a theoretical background on Matplotlib's capabilities, implementation details with code examples, and concludes with the importance of mastering this library for effective data visualization in fields like data science and engineering. The experiment includes practical examples of graph plotting using datasets such as gas prices and FIFA player statistics.

Uploaded by

ranatirth7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭Vidyavardhini’s College of Engineering & Technology‬

‭Department of Computer Engineering‬

‭ xperiment No. 9‬
E
‭Program to plot graph using matplotlib library‬
‭Date of Performance:19/03/25‬
‭Date of Submission:26/03/25‬
‭Experiment No. 9‬

‭Title:‬‭Program to Plot Graphs Using Matplotlib Library‬

‭Aim:‬‭To study and implement graph plotting using the‬‭Matplotlib library in Python.‬

‭Objective:‬‭To‬‭learn‬‭how‬‭to‬‭create‬‭visual‬‭representations‬‭of‬‭data‬‭by‬‭plotting‬‭various‬‭types‬‭of‬
‭graphs, such as line, bar, and scatter plots, using the Matplotlib library.‬

‭Theory:‬

‭Matplotlib‬‭is‬‭a‬‭powerful‬‭and‬‭versatile‬‭Python‬‭library‬‭used‬‭for‬‭creating‬‭a‬‭wide‬‭variety‬‭of‬‭data‬
‭visualizations.‬ ‭It‬ ‭provides‬ ‭an‬ ‭object-oriented‬ ‭API‬‭for‬‭embedding‬‭plots‬‭into‬‭applications‬‭and‬
‭supports generating both static and interactive graphs.‬

‭The‬ ‭core‬ ‭library,‬ ‭[Link],‬ ‭offers‬ ‭functions‬ ‭for‬ ‭creating‬ ‭simple‬ ‭plots‬ ‭such‬ ‭as‬ ‭line‬
‭plots,‬ ‭bar‬ ‭charts,‬ ‭scatter‬ ‭plots,‬ ‭and‬ ‭histograms.‬ ‭It‬ ‭acts‬ ‭as‬ ‭a‬ ‭state-based‬ ‭interface,‬ ‭making‬ ‭it‬
‭intuitive for beginners to use.‬

‭Matplotlib also includes several sub-libraries:‬

‭1.‬ ‭[Link]:‬‭Used‬‭to‬‭create‬‭and‬‭manage‬‭figures,‬‭which‬‭are‬‭the‬‭containers‬‭for‬‭all‬
‭plot elements.‬

‭2.‬ ‭[Link]:‬ ‭Handles‬ ‭the‬ ‭plotting‬ ‭area‬ ‭where‬ ‭data‬ ‭is‬ ‭visualized,‬ ‭including‬ ‭axis‬
‭labels, limits, and gridlines.‬

‭3.‬ ‭[Link]:‬ ‭A‬ ‭lower-level‬ ‭interface‬ ‭that‬‭controls‬‭all‬‭visual‬‭elements‬‭(e.g.,‬‭lines,‬


‭text, and patches).‬

‭4.‬ ‭[Link]:‬ ‭Facilitates‬ ‭creating‬ ‭animated‬ ‭plots‬ ‭for‬ ‭dynamic‬ ‭data‬


‭visualization.‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭By‬‭combining‬‭these‬‭sub-libraries,‬‭Matplotlib‬‭allows‬‭extensive‬‭customization‬‭and‬‭fine-tuning‬
‭of graphs, making it suitable for academic, professional, and research purposes.‬

‭Implementation:‬

‭Code:‬

‭import [Link] as plt‬

‭import numpy as np‬

‭import pandas as pd‬

‭x = [0,1,2,3,4]‬

‭y = [0,2,4,6,8]‬

‭[Link](figsize=(8,5), dpi=100)‬

‭[Link](x, y, 'b^--', label='2x')‬

‭x2 = [Link](0, 4.5, 0.5)‬

‭[Link](x2[:6], x2[:6]**2, 'r', label='X^2')‬

‭[Link](x2[5:], x2[5:]**2, 'r--')‬

‭[Link]('Our First Graph!', fontdict={'fontname': 'Comic Sans MS', 'fontsize': 20})‬

‭[Link]('X Axis')‬

‭[Link]('Y Axis')‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭[Link]([0,1,2,3,4])‬

‭[Link]()‬

‭[Link]('[Link]', dpi=300)‬

‭[Link]()‬

‭labels = ['A', 'B', 'C']‬

‭values = [1,4,2]‬

‭[Link](figsize=(5,3), dpi=100)‬

‭bars = [Link](labels, values)‬

‭patterns = ['/', 'O', '*']‬

‭for bar, pattern in zip(bars, patterns):‬

‭bar.set_hatch(pattern)‬

‭[Link]('[Link]', dpi=300)‬

‭[Link]()‬

‭# Load Gas Prices Data‬

‭gas = pd.read_csv(r"C:\Users\Dell\OneDrive\Desktop\gas_prices.csv")‬

‭[Link](figsize=(8,5))‬

‭[Link]('Gas Prices over Time (in USD)', fontdict={'fontweight':'bold', 'fontsize': 18})‬

‭[Link]([Link], [Link], 'b.-', label='United States')‬

‭[Link]([Link], [Link], 'r.-')‬


‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭[Link]([Link], gas['South Korea'], 'g.-')‬

‭[Link]([Link], [Link], 'y.-')‬

‭[Link]([Link][::3].tolist() + [2011])‬

‭[Link]('Year')‬

‭[Link]('US Dollars')‬

‭[Link]()‬

‭[Link]('Gas_price_figure.png', dpi=300)‬

‭[Link]()‬

‭# Load FIFA Data‬

‭fifa = pd.read_csv(r"C:\Users\Dell\OneDrive\Desktop\fifa_data.csv")‬

‭bins = [40,50,60,70,80,90,100]‬

‭[Link](figsize=(8,5))‬

‭[Link]([Link], bins=bins, color='#abcdef')‬

‭[Link](bins)‬

‭[Link]('Number of Players')‬

‭[Link]('Skill Level')‬

‭[Link]('Distribution of Player Skills in FIFA 2018')‬

‭[Link]('[Link]', dpi=300)‬

‭[Link]()‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭# Preferred Foot‬

‭left = [Link][fifa['Preferred Foot'] == 'Left'].shape[0]‬

‭right = [Link][fifa['Preferred Foot'] == 'Right'].shape[0]‬

‭[Link](figsize=(8,5))‬

‭[Link]([left, right], labels=['Left', 'Right'], colors=['#abcdef', '#aabbcc'], autopct='%.2f %%')‬

‭[Link]('Foot Preference of FIFA Players')‬

‭[Link]('[Link]', dpi=300)‬

‭[Link]()‬

‭# Weight Distribution‬

‭[Link](figsize=(8,5), dpi=100)‬

‭[Link]('ggplot')‬

‭fifa['Weight'] = fifa['Weight'].astype(str).[Link]('lbs', '').astype(float)‬

‭light = [Link][[Link] < 125].shape[0]‬

‭light_medium = [Link][([Link] >= 125) & ([Link] < 150)].shape[0]‬

‭medium = [Link][([Link] >= 150) & ([Link] < 175)].shape[0]‬

‭medium_heavy = [Link][([Link] >= 175) & ([Link] < 200)].shape[0]‬

‭heavy = [Link][[Link] >= 200].shape[0]‬

‭weights = [light, light_medium, medium, medium_heavy, heavy]‬

‭labels = ['under 125', '125-150', '150-175', '175-200', 'over 200']‬

‭[Link](weights, labels=labels, explode=(.4,.2,0,0,.4), pctdistance=0.8, autopct='%.2f %%')‬

‭[Link]('Weight of Professional Soccer Players (lbs)')‬


‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭[Link]('[Link]', dpi=300)‬

‭[Link]()‬

‭# Box Plot Comparison‬

‭[Link](figsize=(5,8), dpi=100)‬

‭[Link]('default')‬

‭barcelona = [Link][[Link] == "FC Barcelona", 'Overall']‬

‭madrid = [Link][[Link] == "Real Madrid", 'Overall']‬

‭revs = [Link][[Link] == "New England Revolution", 'Overall']‬

‭bp = [Link]([barcelona, madrid, revs], tick_labels=['FC Barcelona','Real Madrid','NE‬

‭Revolution'], patch_artist=True, medianprops={'linewidth': 2})‬

‭[Link]('Professional Soccer Team Comparison')‬

‭[Link]('FIFA Overall Rating')‬

‭for box in bp['boxes']:‬

‭[Link](color='#4286f4', linewidth=2, facecolor='#e0e0e0')‬

‭[Link]('[Link]', dpi=300)‬

‭[Link]()‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭Outputs:‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬
‭Vidyavardhini’s College of Engineering & Technology‬
‭Department of Computer Engineering‬

‭Conclusion:‬‭Mastering Matplotlib is a must for anyone‬‭working in data science, engineering,‬


‭or research, as it allows you to create clear and effective visualizations. This powerful Python‬
‭library helps turn raw data into insightful charts, graphs, and plots, making complex trends‬
‭and patterns easy to understand. Whether you're analyzing datasets, running simulations, or‬
‭presenting research results, being skilled with Matplotlib improves the way you communicate‬
‭data. It helps ensure your reports, presentations, and real-world applications are both accurate‬
‭and easy to interpret.‬

You might also like