0% found this document useful (0 votes)
38 views22 pages

Matplotlib Use Cases in Jupyter Notebook

The document outlines an experiment aimed at understanding and implementing the Matplotlib library for data visualization in Jupyter Notebook. It discusses the importance of data visualization, including its ability to reveal trends, provide context, save time, and tell a data story. The document also includes various code examples demonstrating how to create different types of plots, such as cosine curves, bar graphs, histograms, and scatter plots using Matplotlib.

Uploaded by

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

Matplotlib Use Cases in Jupyter Notebook

The document outlines an experiment aimed at understanding and implementing the Matplotlib library for data visualization in Jupyter Notebook. It discusses the importance of data visualization, including its ability to reveal trends, provide context, save time, and tell a data story. The document also includes various code examples demonstrating how to create different types of plots, such as cosine curves, bar graphs, histograms, and scatter plots using Matplotlib.

Uploaded by

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

Name: Nikhil Namade

Roll No. A16


ID - TU4F2223015

Experiment No. 3
Aim:

To understand and implement use cases of Matplotlib library in Jupyter Notebook.

Theory :
Data visualization is the graphical representation of information and data in a pictorial or
graphical format (Example: charts, graphs, and maps). Data visualization tools provide an
accessible way to see and understand trends, patterns in data, and outliers. Data visualization
tools and technologies are essential to analyzing massive amounts of information and making
data-driven decisions. The concept of using pictures is to understand data that has been used
for centuries. General types of data visualization are Charts, Tables, Graphs, Maps,
Dashboards.
Data visualization is very critical to market research where both numerical and categorical
data can be visualized, which helps in an increase in the impact of insights and also helps in
reducing the risk of analysis paralysis.
Suppose you compile a data visualization of the company’s profits from 2010 to 2020 and
create a line chart. It would be very easy to see the line going constantly up with a drop in
just 2018. So, you can observe in a second that the company has had continuous profits in all
the years except a loss in 2018. It would not be that easy to get this information so fast from a
data table. This is just one demonstration of the usefulness of data visualization. Let’s see
some more reasons why data visualization is so important.

1. Data Visualization Discovers the Trends in Data.


The most important thing that data visualization does is discover the trends in data. After all,
it is much easier to observe data trends when all the data is laid out in front of you in a visual
form as compared to data in a table. For example, the screenshot below on Tableau
demonstrates the sum of sales made by each customer in descending order. However, the
color red denotes loss while grey denotes profits. So, it is very easy to observe from this
visualization that even though some customers may have huge sales, they are still at a loss.
This would be very difficult to observe from a table.

2. Data Visualization Provides a Perspective on the Data.


Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Data Visualization provides a perspective on data by showing its meaning in the larger
scheme of things. It demonstrates how particular data references stand with respect to the
overall data picture. In the data visualization below, the data between sales and profit
provides a data perspective with respect to these two measures. It also demonstrates that there
are very few sales above 12K and higher sales do not necessarily mean a higher profit.

3. Data Visualization Puts the Data into the Correct Context.


It is very difficult to understand the context of the data with data visualization. Since context
provides the whole circumstances of the data, it is very difficult to grasp by just reading
numbers in a table. In the below data visualization on Tableau, a Tree Map is used to
demonstrate the number of sales in each region of the United States. It is very easy to
understand from this data visualization that California has the largest number of sales out of
the total number since the rectangle for California is the largest. But this information is not
easy to understand outside of context without data visualization.
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

4. Data Visualization Saves Time.


It is definitely faster to gather some insights from the data using data visualization rather than
just studying a chart. In the screenshot below on Tableau, it is very easy to identify the states
that have suffered a net loss rather than a profit. This is because all the cells with a loss are
colored red using a heat map, so it is obvious states have suffered a loss. Compare this to a
normal table where you would need to check each cell to see if it has a negative value to
determine a loss. Obviously, data visualization saves a lot of time in this situation!

5. Data Visualization Tells a Data Story.


Data visualization is also a medium to tell a data story to the viewers. The visualization can
be used to present the data facts in an easy-to-understand form while telling a story and
leading the viewers to an inevitable conclusion. This data story, like any other type of story,
should have a good beginning, a basic plot, and an ending that it is leading towards. For
example, if a data analyst has to craft a data visualization for company executives detailing
the profits on various products, then the data story can start with the profits and losses of
various products and move on to recommendations on how to tackle the losses.

Advantages of Data Visualization


1. Better Agreement: In business, for numerous periods, it happens that we need to look
at the exhibitions of two components or two situations. A conventional methodology is to
experience the massive information of both the circumstances and afterward examine it. This
will clearly take a great deal of time.
2. A Superior Method: It can tackle the difficulty of placing the information of both
perspectives into the pictorial structure. This will unquestionably give a superior
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

comprehension of the circumstances. For instance, Google patterns assist us with


understanding information identified with top ventures or inquiries in pictorial or graphical
structures.

3. Simple Sharing of Data: With the representation of the information, organizations


present another arrangement of correspondence. Rather than sharing the cumbersome
information, sharing the visual data will draw in and pass on across the data which is more
absorbable.
4. Deals Investigation: With the assistance of information representation, a salesman
can, without much of a stretch, comprehend the business chart of items. With information
perception instruments like warmth maps, he will have the option to comprehend the causes
that are pushing the business numbers up just as the reasons that are debasing the business
numbers. Information representation helps in understanding the patterns and furthermore,
different variables like sorts of clients keen on purchasing, rehash clients, the impact of
topography, and so forth.
5. Discovering Relations Between Occasions: A business is influenced by a lot of
elements. Finding a relationship between these elements or occasions encourages chiefs to
comprehend the issues identified with their business. For instance, the online business market
is anything but another thing today. Each time during certain happy seasons, like Christmas
or Thanksgiving, the diagrams of online organizations go up. Along these lines, state if an
online organization is doing a normal $1 million business in a specific quarter and the
business ascends straightaway, at that point they can rapidly discover the occasions compared
to it.
6. Investigating Openings and Patterns: With the huge loads of information present,
business chiefs can discover the profundity of information in regard to the patterns and
openings around them. Utilizing information representation, the specialists can discover
examples of the conduct of their clients, subsequently preparing for them to investigate
patterns and open doors for business.

Matplotlib Data Visualization Library in Python


Matplotlib is one of the most popular Python packages used for data visualization. It is a
cross- platform library for making 2D plots from data in arrays. It provides an object-oriented
API that helps in embedding plots in applications using Python GUI toolkits such as PyQt,
WxPythonotTkinter. It can be used in Python and IPython shells, Jupyter notebook and web
application servers also.
Basically, it is a plotting library for the Python programming language and its numerical
mathematics extension NumPy. It provides an object-oriented API for embedding plots into
applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK.
Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python. Matplotlib makes easy things easy and hard things possible.
Developers can also use matplotlib's APIs (Application Programming Interfaces) to embed
plots in GUI applications.
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Implementation :

Query 1: Plot cosine curve.

Code:

import numpy as np

import [Link] as plt #from

matplotlib import pyplot as plt

t=[Link](0.0, 2.0, 0.01)

# print(t)

s= [Link](2*[Link]*t) # Because cos(even pie) = 1 always

# print(s) [Link](t,s) [Link]()

[Link]('Time(t)')

[Link]('Voltage(mV)')

[Link]('Cosine Wave Plot(Cos(x))')

[Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 2: For the same graph change the limits from 0 to 1 with the gap of 1.

Code: import numpy as np import [Link] as plt #from

matplotlib import pyplot as plt t=[Link](0.0, 10, 1)

# print(t) s= [Link](2*[Link]*t) # Because cos(even pie)

= 1 always

# print(s) [Link](t,s) [Link]()

[Link]('Time(t)')

[Link]('Voltage(mV)')

[Link]('Cosine Wave Plot(Cos(x))')

[Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 3: Plot multiple graphs i.e., subplot in one plotting window.

Code:

import numpy as np import

[Link] as plt

x1=[Link](0.0, 5.0)

x2=[Link](0.0, 2.0)

y1=[Link](2*[Link]*x1)*[Link](-x1)
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

y2=[Link](2*[Link]*x2)

[Link](2,1,1)

[Link](x1,y1,'g-') [Link]('Subplot-

1') [Link]('x1')

[Link]('Amp(y1)')

[Link](2,1,2) [Link](x2,y2,'*')

[Link]('Subplot-2') [Link]('x2')

[Link]('Amp(y2)')
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 4: For the same graph, change variable x1 as 0 to 10, x2 as 0 to 5. Change colors for
subplots make dimension as 2*2. Take exponential values for any of the subplots. Use sine as
well as cosine waves.

Code: import numpy as np

import [Link] as plt

x1=[Link](0.0, 10)

x2=[Link](0.0, 5)
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

y1=[Link](2*[Link]*x1)*[Link]

p(-x1)

y2=[Link](2*[Link]*x2)

y3=[Link](2*[Link]*x1)

y4=[Link](2*[Link]*x1)*[Link]

p(-x1) [Link](2,2,1)

[Link](x1,y1,'g-')

[Link]('Subplot-1')

[Link]('x1')

[Link]('Amp(y1)')

[Link](2,2,2)

[Link](x2,y2,'b-')

[Link]('Subplot-2')

[Link]('x2')

[Link]('Amp(y2)')

[Link](2,2,3)

[Link](x2,y3,'y-')

[Link]('Subplot-3')

[Link]('x2')

[Link]('Amp(y2)')

[Link](2,2,4)

[Link](x2,y4,'r-')

[Link]('Subplot-4')

[Link]('x2')

[Link]('Amp(y2)')
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 5: Plot a bar graph.

Code:
#Bar Graph is along only one axis for eg y axis:(ie country in x axis and there population in y
axis) import [Link] as plt x=[1,2,3,4,5] y=[26,24,36,40,5]

a=['India', 'Australia', 'Brazil', 'China', 'Egypt']

[Link](x, y, tick_label=a, width=0.5, color=['pink', 'blue', 'orange', 'purple', 'red'])


[Link]('x-axis') [Link]('y-axis') [Link]('Bar Graph') [Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 6: Add two more countries in the above graph i.e., Nigeria and America. Make
Nigeria asblack color and America as green color. Extend y axis to 45, 55 respectively.

Code:
#Bar Graph is along only one axis for eg y axis:(ie country in x axis and there population in y
axis)

import [Link] as plt

x=[1,2,3,4,5,6,7] y=[26,24,36,40,5,45,55]

a=['India', 'Australia', 'Brazil', 'China',

'Egypt', 'Nigeria', 'America']

[Link](x, y, tick_label=a, width=0.5, color=['pink', 'blue', 'orange', 'purple', 'red', 'black',


'green']) [Link]('x-axis')

[Link]('y-axis')
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

[Link]('Bar Graph')

[Link]()

Query 7: Plot histogram

Code:
#It is used for frequency measurement of any one variable(for eg: If you need age distribution
forany particular company, #intensity/color distribution in image and video) import
[Link] as plt ages=[2,50,70,40,30,45,50,45,43,40,44,60] range=(0,100) bins=10

#In how many distribution we want to distribute

[Link](ages, bins, range, color='green', histtype='bar', rwidth=10.0)

[Link]('Ages') [Link]('Bins')

[Link]('Histogram Plot')

[Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 8: Delete last 5 age values and change rwidth to 2.0 Code:

#It is used for frequency measurement of any one variable(for eg: If you need age distribution
forany particular company, #intensity/color distribution in image and video) import
[Link] as plt ages=[2,50,70,40,30,45,50] range=(0,100)
bins=10 #In how many distribution we want to distribute

[Link](ages, bins, range, color='green', histtype='bar', rwidth=2.0)

[Link]('Ages') [Link]('Bins') [Link]('Histogram Plot')

[Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 9: Plot scatterplot

Code:

import [Link] as plt

x=[1,2,3,4,5,6,7,8,9,10] y=[2,4,5,7,6,8,9,10,12,11]

y1=[1,2,1,2,3,4,3,2,1,2] [Link](x,y,

label='rectangle', color='green', marker="*", s=60)

[Link](x,y1,label='circle', color='blue',

marker="+", s=60) [Link]('x-axis')

[Link]('y-label') [Link]('Scatter Plot')

[Link]() [Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 10: For the same graph, add one more y-axis y2 and assume the values, plot and
customize the ledger.

Code:
# A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter
diagram) is a type of plot or mathematical diagram using Cartesian coordinates to display
values for typically two variables for a set of data.

import [Link] as plt

x=[1,2,3,4,5,6,7,8,9,10]
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

y=[2,4,5,7,6,8,9,10,12,11]

y1=[1,2,1,2,3,4,3,2,1,2] y2=[3,2,5,1,3,1,2,4,1,2]

[Link](x,y, label='rectangle', color='green', marker="*", s=60)


[Link](x,y1,label='circle', color='blue', marker="+", s=60)
[Link](x,y2,label='square', color='orange', marker="o", s=60)
[Link]('x-axis') [Link]('y-label') [Link]('Scatter Plot')
[Link]() [Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 11: Plot a pie chart.

Code: import [Link] as

plt activities=['eat', 'sleep', 'work',

'play'] slice=[3, 6, 7, 5]

color=['r', 'lightgreen', 'm', 'lightblue']


[Link](slice, labels=activities, colors=color, startangle=90,shadow=True, explode=(0,0,0,0),
autopct='%1.2f%%') [Link]()

[Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 12: For the same graph, add two more activities and try with start angle 60 degree and
explode all 0 values.

Code:

import [Link] as plt

activities=['eat', 'sleep', 'work', 'play', 'rest', 'workout'] slice=[3,

6, 7, 5, 2, 1]

color=['r', 'lightgreen', 'm', 'lightblue', 'orange', 'y']


[Link](slice, labels=activities, colors=color, startangle=60,shadow=True,
explode=(0,0,0,0,0,0), autopct='%1.2f%%') [Link]() [Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

Query 13: Merging plot.

Code:

import [Link] as plt

import numpy as np

x=[Link](0,2*[Link], 0.01)

y1=[Link](x) y2=[Link](x)

y3=[Link](-x) y4=[Link](-x)

[Link](x,y1,label='sin')

[Link](x,y2, label='cos')

[Link](x,y3,label='exp(-x)')

[Link](x,y4,label='exp(-x)')

[Link]('x-axis')

[Link]('y-axis') [Link]()

[Link]()
Name: Nikhil Namade
Roll No. A16
ID - TU4F2223015

[Link]('Sine and cosine Function') [Link]()

Conclusion :
Thus, in the above experiment, we understood the concept Data Visualization and plotted
various graphs such as Histogram, Piechart, Scatterplot, Bargraph, etc., using Matplotlib
library of Python.

You might also like