0% found this document useful (0 votes)
6 views12 pages

Data Visualization with Matplotlib

The document shows code for plotting different types of charts using Matplotlib in Python, including line graphs, histograms, and bar charts. It generates sample date for things like temperature over time and student ages, then plots that data to demonstrate different chart customization options like colors, labels, titles, legends, and more. The code progresses from simple to more advanced charts, adding additional parameters and functionality in each example.

Uploaded by

pranjalsingh0808
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)
6 views12 pages

Data Visualization with Matplotlib

The document shows code for plotting different types of charts using Matplotlib in Python, including line graphs, histograms, and bar charts. It generates sample date for things like temperature over time and student ages, then plots that data to demonstrate different chart customization options like colors, labels, titles, legends, and more. The code progresses from simple to more advanced charts, adding additional parameters and functionality in each example.

Uploaded by

pranjalsingh0808
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

10/24/23, 7:46 PM Untitled9.

ipynb - Colaboratory

import [Link] as plt

Ploting line graph

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]

[Link](days, temperature)
[Link]()

output

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
# plot line with x-axis, y-axis and title
[Link](days, temperature)
[Link]("Delhi Temperature")
[Link]("days")
[Link]("temperature")
[Link]()

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
# plot line with x-axis, y-axis and title
[Link](days, temperature)
[Link]([0,30, 0,50]) # set axis
[Link]("Delhi Temperature")
[Link]("days")

[Link] 1/13
10/24/23, 7:46 PM [Link] - Colaboratory
[Link]("temperature")
[Link]()

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
# plot line with x-axis, y-axis and title
[Link](days, temperature, color = "g", marker = "o", linestyle= ":",
linewidth = 2, markersize = 10)
[Link]("Delhi Temperature")
[Link]("days")
[Link]("temperature")
[Link]()

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
from matplotlib import style # import style module
[Link]("ggplot") # give ggplot parameter value to use() method
[Link](days, temperature, "mo--", linewidth = 3,
markersize = 10, label = "Temp line")
[Link]("Delhi Temperature", fontsize=15)
[Link]("days",fontsize=13)
[Link]("temperature",fontsize=13)
[Link](loc = 4)
[Link]()
[Link]()

[Link] 2/13
10/24/23, 7:46 PM [Link] - Colaboratory

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
from matplotlib import style # import style module
[Link]("ggplot") # give ggplot parameter value to use() method
[Link](days, temperature, "mo--", linewidth = 3,
markersize = 10, label = "Temp line")
[Link]("Delhi Temperature", fontsize=15)
[Link]("days",fontsize=13)
[Link]("temperature",fontsize=13)
[Link](loc = 4)
[Link](color='c', linestyle='-', linewidth=2)
[Link]()

days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
delhi_tem = [36.6, 37, 37.7,39,40.1,43,43.4,45,45.6,40.1,44,45,46.8,47,47.8]
mumbai_tem = [39,39.4,40,40.7,41,42.5,43.5,44,44.9,44,45,45.1,46,47,46]

[Link](days, delhi_tem, "mo--", linewidth = 3,


markersize = 10, label = "Delhi tem")

[Link](days, mumbai_tem, "yo:", linewidth = 3,


markersize = 10, label = "Mumbai tem}")

[Link]("Delhi & Mumbai Temperature", fontsize=15)


[Link]("days",fontsize=13)
[Link]("temperature",fontsize=13)
[Link](loc = 4)
[Link]()

[Link] 3/13
10/24/23, 7:46 PM [Link] - Colaboratory

[Link](days, delhi_tem, "mo-", linewidth = 3,


markersize = 10, label = "Delhi tem")

[Link](days, mumbai_tem, "ko-", linewidth = 3,


markersize = 10, label = "Mumbai tem")

[Link]("Delhi & Mumbai Temperature", fontsize=15)


[Link]("days",fontsize=13)
[Link]("temperature",fontsize=13)
[Link](loc = 4)
[Link](color='c', linestyle='-', linewidth=2) # grid with parameter
[Link]()

Ploting Histograms

import [Link] as plt


import numpy as np
import random

ml_students_age = [Link](18,45, (100))


py_students_age = [Link](15,40, (100))

print(ml_students_age)
print(py_students_age)

[29 33 36 26 28 28 33 34 26 33 29 39 18 20 27 30 43 26 38 40 41 29 27 41
26 21 42 24 39 38 24 26 38 24 29 34 28 43 39 21 33 35 21 44 20 44 27 35
18 28 35 43 19 31 38 41 42 43 35 43 19 33 18 19 24 31 43 34 37 40 18 23

[Link] 4/13
10/24/23, 7:46 PM [Link] - Colaboratory
27 37 25 19 31 32 34 41 39 40 24 41 42 25 39 42 25 42 28 28 37 25 29 29
41 41 27 27]
[33 21 33 30 16 28 19 28 36 30 25 18 15 24 39 32 21 36 19 39 37 16 31 39
17 20 32 31 24 32 33 27 16 34 34 21 27 18 39 35 28 38 23 29 27 28 36 17
29 22 19 28 31 21 32 20 39 15 22 17 24 20 30 33 33 31 24 18 36 16 27 18
26 32 28 31 20 20 33 22 31 26 21 39 33 23 24 39 33 17 39 27 19 16 21 26
22 26 22 19]

[Link](ml_students_age)

[Link]("ML Students age histograms")


[Link]("Students age cotegory")
[Link]("No. Students age")
[Link]()

bins = [15,20,25,30,35,40,45] # category of ML students age on x axis


[Link](figsize = (16,9)) # size of histogram in 16:9 format

[Link](ml_students_age, bins, rwidth=0.8, histtype = "bar",


orientation='vertical', color = "m", label = "ML Student")

[Link]("ML Students age histograms")


[Link]("Students age cotegory")
[Link]("No. Students age")
[Link]()
[Link]()

[Link] 5/13
10/24/23, 7:46 PM [Link] - Colaboratory

from matplotlib import style # for style


[Link]("ggplot") # return grid
[Link](figsize = (16,9))

[Link]([ml_students_age, py_students_age], bins, rwidth=0.8, histtype = "bar",


orientation='vertical', color = ["m", "y"], label = ["ML Student", "Py Student"])

#[Link](py_students_age, bins, rwidth=0.8, histtype = "bar",


# orientation='vertical', color = "y", label = "Py Student")

[Link]("ML & Py Students age histograms")


[Link]("Students age cotegory")
[Link]("No. Students age")
[Link]()
[Link]()

Ploting Bar Chart

import [Link] as plt


import numpy as np
from matplotlib import style

[Link] 6/13
10/24/23, 7:46 PM [Link] - Colaboratory
# Dataset of 'Indian Artificial Intelligence Production (IAIP) Class"

#classes = ["Python", "R", "Artificial Intelligence", "Machine Learning", "Data Science"]

classes = ["Python", "R", "AI", "ML", "DS"]


class1_students = [30, 10, 20, 25, 10] # out of 100 student in each class
class2_students = [40, 5, 20, 20, 10]
class3_students = [35, 5, 30, 15, 15]

[Link](classes, class1_students) # Plot vertical Bar Chart

<BarContainer object of 5 artists>

[Link](classes, class1_students) # Plot horizontal bar chart

<BarContainer object of 5 artists>

Use multiple parameters of [Link]() method

[Link](classes, class1_students, width = 0.2, align = "edge", color = "y",


edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students", visible=True)
#visible = True ## bar Chart will hide

[Link] 7/13
10/24/23, 7:46 PM [Link] - Colaboratory

<BarContainer object of 5 artists>

[Link](classes, class1_students, width = 0.2, align = "edge", color = "y",


edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students", visible=False)
#visible = True ## bar Chart will hide

<BarContainer object of 5 artists>

Increase figure size and use style.

[Link]("ggplot")
[Link](figsize=(16,9)) # figure size with ratio 16:9
[Link](classes, class1_students, width = 0.6, align = "edge", color = "k",
edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students") #visible=False

[Link] 8/13
10/24/23, 7:46 PM [Link] - Colaboratory

<BarContainer object of 5 artists>

Plot horizontal bar chart with the above specification.

[Link](figsize=(16,9))
[Link](classes, class1_students, align = "edge", color = "k",
edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students") #visible=False

<BarContainer object of 5 artists>

Decorating bar chart using multiple functions.

[Link](figsize=(16,9))
[Link](classes, class1_students, width = 0.6, align = "edge", color = "k",
edgecolor = "m", linewidth = 5, alpha = 0.9, linestyle = "--",
label =" Class 1 Students") #visible=False

[Link]("Bar Chart of IAIP Class", fontsize = 18)


[Link]("Classes",fontsize = 15)
[Link]("No. of Students", fontsize = 15)
[Link]()

[Link] 9/13
10/24/23, 7:46 PM [Link] - Colaboratory

Trying to plot two bar charts with a different dataset.

[Link](figsize=(16,9))

[Link](classes, class1_students, width = 0.2, color = "b",


label =" Class 1 Students") #visible=False

[Link](classes, class2_students, width = 0.2, color = "g",


label =" Class 2 Students")

[Link]("Bar Chart of IAIP Class", fontsize = 18)


[Link]("Classes",fontsize = 15)
[Link]("No. of Students", fontsize = 15)
[Link]()

[Link] 10/13
10/24/23, 7:46 PM [Link] - Colaboratory

The above code not generating two bar charts in one figure but they are overlapping. So use the below code to plot multiple bar charts. In below
chart plot three bar charts using three different datasets.

[Link](figsize=(16,9))

classes_index = [Link](len(classes))

width = 0.2

[Link](classes_index, class1_students, width , color = "b",


label =" Class 1 Students") #visible=False

[Link](classes_index + width, class2_students, width , color = "g",


label =" Class 2 Students")

[Link](classes_index + width + width, class3_students, width , color = "y",


label =" Class 2 Students")

[Link](classes_index + width, classes, rotation = 20)


[Link]("Bar Chart of IAIP Class", fontsize = 18)
[Link]("Classes",fontsize = 15)
[Link]("No. of Students", fontsize = 15)
[Link]()

[Link] 11/13
10/24/23, 7:46 PM [Link] - Colaboratory

Plot Matplotlib Horizontal bar chart with the above specification.

[Link](figsize=(16,9))

classes_index = [Link](len(classes))

width = 0.2

[Link](classes_index, class1_students, width , color = "b",


label =" Class 1 Students") #visible=False

[Link](classes_index + width, class2_students, width , color = "g",


label =" Class 2 Students")

[Link](classes_index + width + width, class3_students, width , color = "y",


label =" Class 3 Students")

[Link](classes_index + width, classes, rotation = 20)


[Link]("Bar Chart of IAIP Class", fontsize = 18)
[Link]("Classes",fontsize = 15)
[Link]("No. of Students", fontsize = 15)
[Link]()
[Link]()

Double-click (or enter) to edit

[Link] 12/13

You might also like