0% found this document useful (0 votes)
20 views

Data Visualization Exp. 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Data Visualization Exp. 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

.

Experiment-1

Aim:-Implementation of basic Python Library Matplotlib or Seaborn to create line plots, which are useful
for visualizing trends and patterns in data over time or across different categories.

Introduction:

Data visualization is the practice of translating information into a visual context, such as a map or graph,
to make data easier for the human brain to understand and pull insights from. The main goal of data
visualization is to make it easier to identify patterns, trends and outliers in large data sets.

Python offers multiple great graphing libraries packed with lots of different features. Whether you want to
create interactive or highly customized plots, Python has an excellent library for you. Here are a few
popular plotting libraries:

• Matplotlib: low level, provides lots of freedom


• Pandas Visualization: easy to use interface, built on Matplotlib
• Seaborn: high-level interface, great default styles
• plotnine: based on R’s ggplot2, uses Grammar of Graphics
• Plotly: can create interactive plots

Matplotlib
Matplotlib is the most popular Python plotting library. It is a low-level library with a Matlab-like
interface that offers lots of freedom at the cost of having to write more code.

To install Matplotlib, pip, and conda can be used.

pip install matplotlib


or
conda install matplotlib

Matplotlib is specifically suitable for creating basic graphs like line charts, bar charts, histograms, etc.
It can be imported by typing:

import matplotlib.pyplot as plt

Seaborn
Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for
creating attractive graphs. Seaborn has a lot to offer. For example, you can create graphs in one line that
would take multiple tens of lines in Matplotlib. Its standard designs are awesome, and it also has a nice
interface for working with Pandas dataframes.

It can be imported by typing:

import seaborn as sns


.

Installation of Matplotlib
Install it using this command:

C:\Users\Your Name>pip install matplotlib

Import Matplotlib
Once Matplotlib is installed, import it in your applications by adding the import module statement:

import matplotlib

Now Matplotlib is imported and ready to use:

Checking Matplotlib Version


The version string is stored under __version__ attribute.

import matplotlib

print(matplotlib.__version__)

Matplotlib Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under the plt alias:

import matplotlib.pyplot as plt

Now the Pyplot package can be referred to as plt.

Draw a line in a diagram from position (0,0) to position (6,250):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([0, 6])


ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()

Result:
.

Matplotlib Plotting Plotting x and y points


• The plot() function is used to draw points (markers) in a diagram.

• By default, the plot() function draws a line from point to point.

• The function takes parameters for specifying points in the diagram.

• Parameter 1 is an array containing the points on the x-axis.

• Parameter 2 is an array containing the points on the y-axis.

If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the plot function.

Example: Draw a line in a diagram from position (1, 3) to position (8, 10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()

Result:

Plotting
Multiple Points
You can plot as many points as you like,
just make sure you have the same number of
points in both axis.

Example: Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8,
10):
import matplotlib.pyplot as plt
import numpy as np
.
xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints,
ypoints) plt.show()

Result

Default X-Points
If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 (etc., depending on the
length of the y-points.

So, if we take the same example as above, and leave out the x-points, the diagram will look like this:

Example: Plotting without x-points:


import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10, 5, 7])

plt.plot(ypoints)
plt.show()

Result:

Matplotlib Line
Linestyle
You can use the keyword argument linestyle, or shorter ls, to change the style of the plotted line:
Example: Use a dotted line:
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])


.

plt.plot(ypoints, linestyle = 'dotted')


plt.show()

Example: Use a dashed line:


plt.plot(ypoints, linestyle = 'dashed')

Result:

Markers
You can use the keyword argument marker to
emphasize each point with a specified marker:

Example: Mark each point with a circle:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o')


plt.show()

Result:

Example
Mark each point with a star:...
.

plt.plot(ypoints, marker = '*')


...

Conlusion: In this way we studied Implementation of basic Python Library Matplotlib or Seaborn to
create line plots, which are useful for visualizing trends and patterns in data over time or across different
categories.

*****

You might also like