Data Visualization Exp. 1
Data Visualization Exp. 1
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
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.
Matplotlib is specifically suitable for creating basic graphs like line charts, bar charts, histograms, etc.
It can be imported by typing:
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.
Installation of Matplotlib
Install it using this command:
Import Matplotlib
Once Matplotlib is installed, import it in your applications by adding the import module statement:
import matplotlib
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:
plt.plot(xpoints, ypoints)
plt.show()
Result:
.
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):
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:
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
Result:
Markers
You can use the keyword argument marker to
emphasize each point with a specified marker:
Result:
Example
Mark each point with a star:...
.
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.
*****