0% found this document useful (0 votes)
8 views31 pages

4 Unit 4 Data Vizuvalization

This document covers data visualization using Matplotlib in Python, including importing the library, creating various types of plots such as line and scatter plots, and customizing them with labels, legends, and error bars. It provides examples of code for generating these visualizations and discusses the importance of effective data representation. Additionally, it highlights the use of Seaborn for advanced visualization techniques.

Uploaded by

chitra
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)
8 views31 pages

4 Unit 4 Data Vizuvalization

This document covers data visualization using Matplotlib in Python, including importing the library, creating various types of plots such as line and scatter plots, and customizing them with labels, legends, and error bars. It provides examples of code for generating these visualizations and discusses the importance of effective data representation. Additionally, it highlights the use of Seaborn for advanced visualization techniques.

Uploaded by

chitra
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
You are on page 1/ 31

UNIT-IV DATA VIZUALIZATION

Syllabus
Importing Matplotlib - Line plots - Scatter plots - visualizing errors - densityand
contour plots Histograms - legends - colors - subplots - text andannotation -
customization three dimensional plotting - Geographic Data withBasemap -
Visualization with Seaborn.

4.1 Importing Matplotlib

• Matplotlib is a cross-platform, data visualization and graphical plotting library for


Python and its numerical extension NumPy.
• Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python.
• Matplotlib is a plotting library for the Python programming language. It allows to
make quality charts in few lines of code. Most of the other python plotting library are
build on top of Matplotlib.
• The library is currently limited to 2D output, but it still provides you with the
means to express graphically the data patterns.

Visualizing Information: Starting with Graph


• Data visualization is the presentation of quantitative information in a graphical
form. In other words, data visualizations turn large and small datasets into visuals
that are easier for the human brain to understand and process.
• Good data visualizations are created when communication, data science, and
design collide. Data visualizations done right offer key insights into complicated
datasets in ways that are meaningful and intuitive.
• A graph is simply a visual representation of numeric data. MatPlotLib supports a
large number of graph and chart types.
• Matplotlib is a popular Python package used to build plots. Matplotlib can also be
used to make 3D plots and animations.
• Line plots can be created in Python with Matplotlib's pyplot library. To build a line
plot, first import Matplotlib. It is a standard convention to import Matplotlib's pyplot
library as plt.
• To define a plot, you need some values, the matplotlib.pyplot module, and an idea
of what you want to display.
import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()
• The plt.plot will "draw" this plot in the background, but we need to bring it to the
screen when we're ready, after graphing everything we intend to.
• plt.show(): With that, the graph should pop up. If not, sometimes can pop under, or
you may have gotten an error. Your graph should look like :

• This window is a matplotlib window, which allows us to see our graph, as well as
interact with it and navigate it

4.2 Line Plot


• More than one line can be in the plot. To add another line, just call the plot (x,y)
function again. In the example below we have two different values for y (y1, y2) that
are plotted onto the chart.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 50)
y1 = 2*x+ 1
y2 = 2**x + 1
plt.figure(num = 3, figsize=(8, 5))
plt.plot(x, y2)
plt.plot(x, y1,
linewidth=1.0,
linestyle='--'
)
plt.show()
• Output of the above code will look like this:
Example 5.1.1: Write a simple python program that draws a line graph where x
= [1,2,3,4] and y = [1,4,9,16] and gives both axis label as "X-axis" and "Y-axis".
Solution:
importmatplotlib.pyplot as plt
importnumpy as np
# define data values
x = np.array([1, 2, 3, 4]) # X-axis points
y = x*2 # Y-axis points
print("Values of :")
print("Values of Y):")
print (Y)
plt.plot(X, Y)
# Set the x axis label of the current axis.
plt.xlabel('x-axis')
# Set the y axis label of the current axis.
plt.ylabel('y-axis')
# Set a title
plt.title('Draw a line.')
# Display the figure.
plt.show()

Saving Work to Disk


• Matplotlib plots can be saved as image files using the plt.savefig() function.
• The .savefig() method requires a filename be specified as the first argument. This
filename can be a full path. It can also include a particular file extension if desired. If
no extension is provided, the configuration value of savefig.format is used instead.
• The .savefig() also has a number of useful optional arguments :
1. dpi can be used to set the resolution of the file to a numeric value.
2. transparent can be set to True, which causes the background of the chart to be
transparent.
3. bbox_inches can be set to alter the size of the bounding box (whitespace) around
the output image. In most cases, if no bounding box is desired, using bbox_inches =
'tight' is ideal.
4. If bbox_inches is set to 'tight', then the pad_inches option specifies the amount of
padding around the image.

Setting the Axis, Ticks, Grids


• The axes define the x and y plane of the graphic. The x axis runs horizontally, and
the y axis runs vertically.
• An axis is added to a plot layer. Axis can be thought of as sets of x and y axis that
lines and bars are drawn on. An Axis contains daughter attributes like axis labels,
tick labels, and line thickness.
• The following code shows how to obtain access to the axes for a plot :
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)
axes.plot(x, y, 'r')
axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_title('title');
Output:

• A grid can be added to a Matplotlib plot using the plt.grid() command. By defaut,
the grid is turned off. To turn on the grid use:
plt.grid(True)
• The only valid options are plt.grid(True) and plt.grid(False). Note that True and
False are capitalized and are not enclosed in quotes.
Defining the Line Appearance and Working with Line Style
• Line styles help differentiate graphs by drawing the lines in various ways. Following
line style is used by Matplotlib.
• Matplotlib has an additional parameter to control the colour and style of the plot.
plt.plot(xa, ya 'g')

• This will make the line green. You can use any colour of red, green, blue, cyan,
magenta, yellow, white or black just by using the first character of the colour name in
lower case (use "k" for black, as "b" means blue).
• You can also alter the linestyle, for example two dashes -- makes a dashed line.
This can be used added to the colour selector, like this:
plt.plot(xa, ya 'r--')
• You can use "-" for a solid line (the default), "-." for dash-dot lines, or ":" for a dotted
line. Here is an example :
from matplotlib import pyplot as plt
import numpy as np
xa = np.linspace(0, 5, 20)
ya = xa**2
plt.plot(xa, ya, 'g')
ya = 3*xa
plt.plot(xa, ya, 'r--')
plt.show()
Output:

• MatPlotLib Colors are as follows:


Adding Markers
• Markers add a special symbol to each data point in a line graph. Unlike line style
and color, markers tend to be a little less susceptible to accessibility and printing
issues.
• Basically, the matplotlib tries to have identifiers for the markers which look similar
to the marker:
1. Triangle-shaped: v, <, > Λ
2. Cross-like: *,+, 1, 2, 3, 4
3. Circle-like: 0,., h, p, H, 8
• Having differently shaped markers is a great way to distinguish between different
groups of data points. If your control group is all circles and your experimental group
is all X's the difference pops out, even to colorblind viewers.
N = x.size // 3
ax.scatter(x[:N], y[:N], marker="o")
ax.scatter(x[N: 2* N], y[N: 2* N], marker="x")
ax.scatter(x[2* N:], y[2 * N:], marker="s")
• There's no way to specify multiple marker styles in a single scatter() call, but we can
separate our data out into groups and plot each marker style separately. Here we
chopped our data up into three equal groups.
Using Labels, Annotations and Legends
• To fully document your graph, you usually have to resort to labels, annotations,
and legends. Each of these elements has a different purpose, as follows:
1. Label: Make it easy for the viewer to know the name or kind of data illustrated
2. Annotation: Help extend the viewer's knowledge of the data, rather than simply
identify it.
3. Legend: Provides cues to make identification of the data group easier.
• The following example shows how to add labels to your graph:
values = [1, 5, 8, 9, 2, 0, 3, 10, 4, 7]
import matplotlib.pyplot as plt
plt.xlabel('Entries')
plt.ylabel('Values')
plt.plot(range(1,11), values)
plt.show()
• Following example shows how to add annotation to a graph:
import matplotlib.pyplot as plt
W=4
h=3
d = 70
plt.figure(figsize=(w, h), dpi=d)
plt.axis([0, 5, 0, 5])
x = [0, 3, 5]
y = [1, 4, 3.5]
label_x = 1
label_y = 4
arrow_x = 3
arrow_y= 4
arrow_properties=dict(
facecolor="black", width=0.5,
headwidth=4, shrink=0.1)
plt.annotate("maximum", xy=(arrow_x, arrow_y),
xytext=(label_x, label_y),
arrowprops arrow_properties)
plt.plot(x, y)
plt.savefig("out.png")
Output:
Creating a legend
• There are several options available for customizing the appearance and behavior of
the plot legend. By default the legend always appears when there are multiple series
and only appears on mouseover when there is a single series. By default the legend
shows point values when the mouse is over the graph but not when the mouse
leaves.
• A legend documents the individual elements of a plot. Each line is presented in a
table that contains a label for it so that people can differentiate between each line.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 9, 20)
y = x ** 3
Z = x ** 2
figure = plt.figure()
axes = figure.add_axes([0,0,1,1])
axes.plot(x, z, label="Square Function")
axes.plot(x, y, label="Cube Function")
axes.legend()
• In the script above we define two functions: square and cube using x, y and z
variables. Next, we first plot the square function and for the label parameter, we pass
the value Square Function.
• This will be the value displayed in the label for square function. Next, we plot the
cube function and pass Cube Function as value for the label parameter.
• The output looks likes this:
4.2 Scatter Plots
• A scatter plot is a visual representation of how two variables relate to each other. we
can use scatter plots to explore the relationship between two variables, for example
by looking for any correlation between them.
• Matplotlib also supports more advanced plots, such as scatter plots. In this case,
the scatter function is used to display data values as a collection of x, y coordinates
represented by standalone dots.
importmatplotlib.pyplot as plt
#X axis values:
x = [2,3,7,29,8,5,13,11,22,33]
# Y axis values:
y = [4,7,55,43,2,4,11,22,33,44]
# Create scatter plot:
plt.scatter(x, y)
plt.show()

• Comparing plt.scatter() and plt.plot(): We can also produce the scatter plot shown
above using another function within matplotlib.pyplot. Matplotlib'splt.plot() is a
general-purpose plotting function that will allow user to create various different line
or marker plots.
• We can achieve the same scatter plot as the one obtained in the section above with
the following call to plt.plot(), using the same data:
plt.plot(x, y, "o")
plt.show()
• In this case, we had to include the marker "o" as a third argument, as otherwise
plt.plot() would plot a line graph. The plot created with this code is identical to the
plot created earlier with plt.scatter().
. • Here's a rule of thumb that can use :
a) If we need a basic scatter plot, use plt.plot(), especially if we want to prioritize
performance.
b) If we want to customize our scatter plot by using more advanced plotting features,
use plt.scatter().
• Example: We can create a simple scatter plot in Python by passing x and y values
to plt.scatter():
# scatter_plotting.py
importmatplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
x = [2, 4, 6, 6, 9, 2, 7, 2, 6, 1, 8, 4, 5, 9, 1, 2, 3, 7, 5, 8, 1, 3]
y = [7, 8, 2, 4, 6, 4, 9, 5, 9, 3, 6, 7, 2, 4, 6, 7, 1, 9, 4, 3, 6, 9]
plt.scatter(x, y)
plt.show()
Output:

Creating Advanced Scatterplots


• Scatterplots are especially important for data science because they can show data
patterns that aren't obvious when viewed in other ways.
import matplotlib.pyplot as plt
x_axis1 =[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_axis1 =[5, 16, 34, 56, 32, 56, 32, 12, 76, 89]
x_axis2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_axis2 = [53, 6, 46, 36, 15, 64, 73, 25, 82, 9]
plt.title("Prices over 10 years")
plt.scatter(x_axis1, y_axis1, color = 'darkblue', marker='x', label="item 1")
plt.scatter(x_axis2, y_axis2, color='darkred', marker='x', label="item 2")
plt.xlabel("Time (years)")
plt.ylabel("Price (dollars)")
plt.grid(True)
plt.legend()
plt.show()
• The chart displays two data sets. We distinguish between them by the colour of the
marker.
Foundatio

4.3 Visualizing Errors


• Error bars are included in Matplotlib line plots and graphs. Error is the difference
between the calculated value and actual value.
• Without error bars, bar graphs provide the perception that a measurable or
determined number is defined to a high level of efficiency. The method
matplotlib.pyplot.errorbar() draws y vs. x as planes and/or indicators with error bars
associated.
• Adding the error bar in Matplotlib, Python. It's very simple, we just have to write the
value of the error. We use the command:
plt.errorbar(x, y, yerr = 2, capsize=3)
Where:
x = The data of the X axis.
Y = The data of the Y axis.
yerr = The error value of the Y axis. Each point has its own error value.
xerr = The error value of the X axis.
capsize = The size of the lower and upper lines of the error bar
• A simple example, where we only plot one point. The error is the 10% on the Y axis.
importmatplotlib.pyplot as plt
x=1
y = 20
y_error = 20*0.10 ## El 10% de error
plt.errorbar(x,y, yerr = y_error, capsize=3)
plt.show()
Output:
• We plot using the command "plt.errorbar (...)", giving it the desired characteristics.
importmatplotlib.pyplot as plt
importnumpy as np
x = np.arange(1,8)
y = np.array([20,10,45,32,38,21,27])
y_error = y * 0.10 ##El 10%
plt.errorbar(x, y, yerr = y_error,
linestyle="None", fmt="ob", capsize=3, ecolor="k")
plt.show()

Parameters of the errorbar :


a) yerr is the error value in each point.
b) linestyle, here it indicate that we will not plot a line.
c) fmt, is the type of marker, in this case is a point ("o") blue ("b").
d) capsize, is the size of the lower and upper lines of the error bar.
e) ecolor, is the color of the error bar. The default color is the marker color.
Output:
• Multiple lines in MatplotlibErrorbar in Python : The ability to draw numerous lines
in almost the same plot is critical. We'll draw many errorbars in the same graph by
using this scheme.
importnumpy as np
importmatplotlib.pyplot as plt
fig = plt.figure()
x = np.arange(20)
y = 4* np.sin(x / 20 * np.pi)
yerr = np.linspace (0.06, 0.3, 20)
plt.errorbar(x, y + 8, yerr = yerr, )
plt.errorbar(x, y + 6, yerr = yerr,
uplims = True, )
plt.errorbar(x, y + 4, yerr = yerr,
uplims = True,
lolims True, )
upperlimits = [True, False] * 6
lowerlimits = [False, True]* 6
plt.errorbar(x, y, yerr = yerr,
uplims =upperlimits,
lolims = lowerlimits, )
plt.legend(loc='upper left')
plt.title('Example')
plt.show()
Output:
4.4 Density and Contour Plots
• It is useful to display three-dimensional data in two dimensions using contours or
color- coded regions. Three Matplotlib functions are used for this purpose. They are :
a) plt.contour for contour plots,
b) plt.contourf for filled contour plots,
c) plt.imshow for showing images.
1. Contour plot :
• A contour line or isoline of a function of two variables is a curve along which the
function has a constant value. It is a cross-section of the three-dimensional graph of
the function f(x, y) parallel to the x, y plane.
• Contour lines are used e.g. in geography and meteorology. In cartography, a
contour line joins points of equal height above a given level, such as mean sea level.
• We can also say in a more general way that a contour line of a function with two
variables is a curve which connects points with the same values.
import numpy as np
xlist = np.linspace(-3.0, 3.0, 3)
ylist = np.linspace(-3.0, 3.0, 4)
X, Y = np.meshgrid(xlist, ylist
print(xlist)
print(ylist)
print(X)
print(Y)
Output:
Changing the colours and the line style
importmatplotlib.pyplot as plt
plt.figure()
cp = plt.contour(X, Y, Z, colors='black', linestyles='dashed')
plt.clabel(cp, inline=True,
fontsize=10)
plt.title('Contour Plot')
plt.xlabel('x (cm))
plt.ylabel('y (cm)')
plt.show()
Output:

• When creating a contour plot, we can also specify the color map. There are different
classes of color maps. Matplotlib gives the following guidance :
a) Sequential: Change in lightness and often saturation of color incrementally, often
using a single hue; should be used for representing information that has ordering.
b) Diverging: Change in lightness and possibly saturation of two different colors that
meet in the middle at an unsaturated color; should be used when the information
being plotted has a critical middle value, such as topography or when the data
deviates around zero.
c) Cyclic : Change in lightness of two different colors that meet in the middle and
beginning/end at an unsaturated color; should be used for values that wrap around
at the endpoints, such as phase angle, wind direction, or time of day.
d) Qualitative: Often are miscellaneous colors; should be used to represent
information which does not have ordering or relationships.
• This data has both positive and negative values, which zero representing a node for
the wave function. There are three important display options for contour plots: the
undisplaced shape key, the scale factor, and the contour scale.
a) The displaced shape option controls if and how the deformed model is shown in
comparison to the undeformed (original) geometry. The "Deformed shape only" is the
default and provides no basis for comparison.
b) The "Deformed shape with undeformed edge" option overlays the contour plot on
an outline of the original model.
c) The "Deformed shape with undeformed model" option overlays the contour plot on
the original finite element model.

4.5 Histogram
• In a histogram, the data are grouped into ranges (e.g. 10 - 19, 20 - 29) and then
plotted as connected bars. Each bar represents a range of data. The width of each bar
is proportional to the width of each category, and the height is proportional to the
frequency or percentage of that category.
• It provides a visual interpretation of numerical data by showing the number of data
points that fall within a specified range of values called "bins".
• Fig. 5.5.1 shows histogram.
• Histograms can display a large amount of data and the frequency of the data
values. The median and distribution of the data can be determined by a histogram. In
addition, it can show any outliers or gaps in the data.
• Matplotlib provides a dedicated function to compute and display histograms:
plt.hist()
• Code for creating histogram with randomized data :
import numpy as np
import matplotlib.pyplot as plt
x = 40* np.random.randn(50000)
plt.hist(x, 20, range=(-50, 50), histtype='stepfilled',
align='mid', color='r', label="Test Data')
plt.legend()
plt.title(' Histogram')
plt.show()
Legend
• Plot legends give meaning to a visualization, assigning labels to the various plot
elements. Legends are found in maps - describe the pictorial language or symbology
of the map. Legends are used in line graphs to explain the function or the values
underlying the different lines of the graph.
• Matplotlib has native support for legends. Legends can be placed in various
positions: A legend can be placed inside or outside the chart and the position can be
moved. The legend() method adds the legend to the plot.
• To place the legend inside, simply call legend():
import matplotlib.pyplot as plt
import numpy as np

y = [2,4,6,8,10,12,14,16,18,20]
y2 = [10,11,12,13,14,15,16,17,18,19]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
ax.plot(x, y2, label='$y2 = other numbers')
plt.title('Legend inside')
ax.legend()
plt.show()
Output:

• If we add a label to the plot function, the value will be used as the label in the
legend command. There is another argument that we can add to the legend function:
We can define the location of the legend inside of the axes plot with the parameter
"loc". If we add a label to the plot function, the values will be used in the legend
command:
frompolynomialsimportPolynomial
importnumpyasnp
importmatplotlib.pyplotasplt
p=Polynomial(-0.8,2.3,0.5,1,0.2)
p_der=p.derivative()
fig, ax=plt.subplots()
X=np.linspace (-2,3,50, endpoint=True)
F=p(X)
F_derivative=p_der(X)
ax.plot(X,F,label="p")
ax.plot(X,F_derivative,label="derivation of p")
ax.legend(loc='upper left')
Output:

Matplotlib legend on bottom


importmatplotlib.pyplotasplt
importnumpyasnp
y1 = [2,4,6,8,10,12,14,16,18,20]
y2 = [10,11,12,13,14,15,16,17,18,19]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
ax.plot(x, y2, label='$y2= = other numbers')
plt.title('Legend inside')
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), &nbsp;
shadow=True, ncol=2)
plt.show()
Output:
4.6 Subplots
• Subplots mean groups of axes that can exist in a single matplotlib figure. subplots()
function in the matplotlib library, helps in creating multiple layouts of subplots. It
provides control over all the individual plots that are created.
• subplots() without arguments returns a Figure and a single Axes. This is actually
the simplest and recommended way of creating a single Figure and Axes.
fig, ax = plt.subplots()
ax.plot(x,y)
ax.set_title('A single plot')
Output:

• There are 3 different ways (at least) to create plots (called axes) in matplotlib. They
are:plt.axes(), figure.add_axis() and plt.subplots()
• plt.axes(): The most basic method of creating an axes is to use the plt.axes
function. It takes optional argument for figure coordinate system. These numbers
represent [bottom, left, width, height] in the figure coordinate system, which ranges
from 0 at the bottom left of the figure to 1 at the top right of the figure.
• Plot just one figure with (x,y) coordinates: plt.plot(x, y).
• By calling subplot(n,m,k), we subdidive the figure into n rows and m columns and
specify that plotting should be done on the subplot number k. Subplots are
numbered row by row, from left to right.
importmatplotlib.pyplotasplt
importnumpyasnp
frommathimportpi
plt.figure(figsize=(8,4)) # set dimensions of the figure
x=np.linspace (0,2*pi,100)
foriinrange(1,7):
plt.subplot(2,3,i)# create subplots on a grid with 2 rows and 3 columns
plt.xticks([])# set no ticks on x-axis
plt.yticks([])# set no ticks on y-axis
plt.plot(np.sin(x), np.cos(i*x))
plt.title('subplot'+'(2,3,' + str(i)+')')
plt.show()
Output:

Text and Annotation


• When drawing large and complex plots in Matplotlib, we need a way of labelling
certain portion or points of interest on the graph. To do so, Matplotlib provides us
with the "Annotation" feature which allows us to plot arrows and text labels on the
graphs to give them more meaning.
• There are four important parameters that you must always use with annotate().
a) text: This defines the text label. Takes a string as a value.
b) xy: The place where you want your arrowhead to point to. In other words, the
place you want to annotate. This is a tuple containing two values, x and y.
c) xytext: The coordinates for where you want to text to display.
d) arrowprops: A dictionary of key-value pairs which define various properties for the
arrow, such as color, size and arrowhead type.
Example :
importmatplotlib.pyplot as plt
importnumpy as np
fig, ax = plt.subplots()
x = np.arange(0.0, 5.0, 0.01)
y =np.sin(2* np.pi *x)
# Annotation
ax.annotate('Local Max',
xy = (3.3, 1),
xytext (3, 1.8),
arrowprops = dict(facecolor = 'green',
shrink =0.05))
ax.set_ylim(-2, 2)
plt.plot(x, y)
plt.show()
Output:

Example :
importplotly.graph_objectsasgo
fig=go.Figure()
fig.add_trace(go.Scatter(
x=[0,1,2,3,4,5,6,7,8],
y=[0,1,3,2,4,3,4,6,5]
))
fig.add_trace(go.Scatter(
x=[0,1,2,3,4,5,6,7,8],
y=[0,4,5,1,2,2,3,4,2]
))
fig.add_annotation(x=2,y=5,
text="Text annotation with arrow",
showarrow=True,
arrowhead=1)
fig.add_annotation(x=4,y=4,
text="Text annotation without arrow",
showarrow=False,
yshift = 10)
fig.update_layout(showlegend=False)
fig.show()
Output:

4.7 Customization
• A tick is a short line on an axis. For category axes, ticks separate each category. For
value axes, ticks mark the major divisions and show the exact point on an axis that
the axis label defines. Ticks are always the same color and line style as the axis.
• Ticks are the markers denoting data points on axes. Matplotlib's default tick
locators and formatters are designed to be generally sufficient in many common
situations. Position and labels of ticks can be explicitly mentioned to suit specific
requirements.
• Fig. 5.9.1 shows ticks.
• Ticks come in two types: major and minor.
a) Major ticks separate the axis into major units. On category axes, major ticks are
the only ticks available. On value axes, one major tick appears for every major axis
division.
b) Minor ticks subdivide the major tick units. They can only appear on value axes.
One minor tick appears for every minor axis division.
• By default, major ticks appear for value axes. xticks is a method, which can be used
to get or to set the current tick locations and the labels.
• The following program creates a plot with both major and minor tick marks,
customized to be thicker and wider than the default, with the major tick marks point
into and out of the plot area.
importnumpyasnp
importmatplotlib.pyplotasplt
# A selection of functions on rnabcissa points for 0 <= x < 1
rn=100
rx=np.linspace(0,1,rn, endpoint=False)
deftophat(rx):
"""Top hat function: y = 1 for x < 0.5, y=0 for x >= 0.5"""
ry=np.ones(rn)
ry[rx>=0.5]=0
returnry
# A dictionary of functions to choose from
ry={half-sawtooth':lambdarx:rx.copy(),
'top-hat':tophat,
'sawtooth':lambdarx:2*np.abs(rx-0.5)}
# Repeat the chosen function nrep times
nrep=4
x=np.linspace (0,nrep,nrep*rn, endpoint=False)
y=np.tile(ry['top-hat'] (rx), nrep)
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x,y,'k',lw=2)
# Add a bit of padding around the plotted line to aid visualization
ax.set_ylim(-0.1,1.1)
ax.set_xlim(x[0]-0.5,x[-1]+0.5)
# Customize the tick marks and turn the grid on
ax.minorticks_on()
ax.tick_params (which='major',length=10, width=2,direction='inout')
ax.tick_params(which='minor',length=5,width=2, direction='in')
ax.grid(which='both')
plt.show()
Output:

4.8 Three Dimensional Plotting


• Matplotlib is the most popular choice for data visualization. While initially
developed for plotting 2-D charts like histograms, bar charts, scatter plots, line plots,
etc., Matplotlib has extended its capabilities to offer 3D plotting modules as well.
• First import the library :
importmatplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
• The first one is a standard import statement for plotting using matplotlib, which
you would see for 2D plotting as well. The second import of the Axes3D class is
required for enabling 3D projections. It is, otherwise, not used anywhere else.
• Create figure and axes
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111, projection='3d')
Output:

Example :
fig=plt.figure(figsize=(8,8))
ax=plt.axes(projection='3d')
ax.grid()
t=np.arange(0,10*np.pi,np.pi/50)
x=np.sin(t)
y=np.cos(t)
ax.plot3D(x,y,t)
ax.set_title('3D Parametric Plot')
# Set axes label
ax.set_xlabel('x',labelpad=20)
ax.set_ylabel('y', labelpad=20)
ax.set_zlabel('t', labelpad=20)
plt.show()
Output:
4.9 Geographic Data with Basemap
• Basemap is a toolkit under the Python visualization library Matplotlib. Its main
function is to draw 2D maps, which are important for visualizing spatial data.
Basemap itself does not do any plotting, but provides the ability to transform
coordinates into one of 25 different map projections.
• Matplotlib can also be used to plot contours, images, vectors, lines or points in
transformed coordinates. Basemap includes the GSSH coastline dataset, as well as
datasets from GMT for rivers, states and national boundaries.
• These datasets can be used to plot coastlines, rivers and political boundaries on a
map at several different resolutions. Basemap uses the Geometry Engine-Open
Source (GEOS) library at the bottom to clip coastline and boundary features to the
desired map projection area. In addition, basemap provides the ability to read
shapefiles.
• Basemap cannot be installed using pip install basemap. If Anaconda is installed,
you can install basemap using canda install basemap.
• Example objects in basemap:
a) contour(): Draw contour lines.
b) contourf(): Draw filled contours.
c) imshow(): Draw an image.
d) pcolor(): Draw a pseudocolor plot.
e) pcolormesh(): Draw a pseudocolor plot (faster version for regular meshes).
f) plot(): Draw lines and/or markers.
g) scatter(): Draw points with markers.
h) quiver(): Draw vectors.(draw vector map, 3D is surface map)
i) barbs(): Draw wind barbs (draw wind plume map)
j) drawgreatcircle(): Draw a great circle (draws a great circle route)
• For example, if we wanted to show all the different types of endangered plants
within a region, we would use a base map showing roads, provincial and state
boundaries, waterways and elevation. Onto this base map, we could add layers that
show the location of different categories of endangered plants. One added layer could
be trees, another layer could be mosses and lichens, another layer could be grasses.
Basemap basic usage:
import warnings
warnings.filterwarmings('ignore')
frommpl_toolkits.basemap import Basemap
importmatplotlib.pyplot as plt
map = Basemap()
map.drawcoastlines()
# plt.show()
plt.savefig('test.png')
Output:
4.10 Visualization with Seaborn
• Seaborn is a Python data visualization library based on Matplotlib. It provides a
high-level interface for drawing attractive and informative statistical graphics.
Seaborn is an open- source Python library.
• Seaborn helps you explore and understand your data. Its plotting functions operate
on dataframes and arrays containing whole datasets and internally perform the
necessary semantic mapping and statistical aggregation to produce informative plots.
• Its dataset-oriented, declarative API. User should focus on what the different
elements of your plots mean, rather than on the details of how to draw them.
• Keys features:
a) Seaborn is a statistical plotting library
b) It has beautiful default styles
c) It also is designed to work very well with Pandas dataframe objects.
Seaborn works easily with dataframes and the Pandas library. The graphs created
can also be customized easily.
• Functionality that seaborn offers:
a) A dataset-oriented API for examining relationships between multiple variables
b) Convenient views onto the overall structure of complex datasets
c) Specialized support for using categorical variables to show observations or
aggregate statistics
d) Options for visualizing univariate or bivariate distributions and for comparing
them between subsets of data
e) Automatic estimation and plotting of linear regression models for different kinds of
dependent variables
f) High-level abstractions for structuring multi-plot grids that let you easily build
complex visualizations
g) Concise control over matplotlib figure styling with several built-in themes
h) Tools for choosing color palettes that faithfully reveal patterns in your data.
Plot a Scatter Plot in Seaborn :
importmatplotlib.pyplot as plt
importseaborn as sns
import pandas as pd
df = pd.read_csv('worldHappiness2016.csv').
sns.scatterplot(data= df, x = "Economy (GDP per Capita)", y =
plt.show()
Output:

Difference between Matplotlib and Seaborn


Two Marks Questions with Answers
Q.1What is data visualization?
Ans. : Data visualization is the graphical representation of information and data.

Q.2 Which concept is used in data visualization?


Ans.: Data visualization based on two concepts:
1. Each attribute of training data is visualized in a separate part of screen.
2. Different class labels of training objects are represented by different colors.

Q.3 List the benefits of data visualization.


Ans.: • Constructing ways in absorbing information. Data visualization enables users
to receive vast amounts of information regarding operational and business
conditions.
• Visualize relationships and patterns in businesses.
• More collaboration and sharing of information.
• More self-service functions for the end users.

Q.4 Why big data visualization is important?


Ans. Reasons:
• It provides clear knowledge about patterns of data.
• Detects hidden structures in data.
• Identify areas that need to be improved.
• Help us to understand which products to place where.
• Clarify factors which influence human behaviour.

Q.5 Explain Matplotlib.


Ans. Matplotlib is a cross-platform, data visualization and graphical plotting library
for Python and its numerical extension NumPy. Matplotlib is a comprehensive library
for creating static, animated and interactive visualizations in Python. Matplotlib is a
plotting library for the Python programming language. It allows to make quality
charts in few lines of code. Most of the other python plotting library are build on top
of Matplotlib.

Q.6 What is contour plot ?


Ans. A contour line or isoline of a function of two variables is a curve along which the
function has a constant value. It is a cross-section of the three-dimensional graph of
the function f(x, y) parallel to the x, y plane. Contour lines are used e.g. in geography
and meteorology. In cartography, a contour line joins points of equal height above a
given level, such as mean sea level.
Q.7 Explain legends.
Ans. Plot legends give meaning to a visualization, assigning labels to the various plot
elements. Legends are found in maps describe the pictorial language or symbology of
the map. Legends are used in line graphs to explain the function or the values
underlying the different lines of the graph.

Q.8 What is subplots?


Ans. Subplots mean groups of axes that can exist in a single matplotlib figure.
subplots() function in the matplotlib library, helps in creating multiple layouts of
subplots. It provides control over all the individual plots that are created.

Q.9 What is use of tick?


Ans. A tick is a short line on an axis. For category axes, ticks separate each category.
For value axes, ticks mark the major divisions and show the exact point on an axis
that the axis label defines. Ticks are always the same color and line style as the axis.
• Ticks are the markers denoting data points on axes. Matplotlib's default tick
locators and formatters are designed to be generally sufficient in many common
situations. Position and labels of ticks can be explicitly mentioned to suit specific
requirements.

Q.10 Describe in short Basemap.


Ans. • Basemap is a toolkit under the Python visualization library Matplotlib. Its
main function is to draw 2D maps, which are important for visualizing spatial data.
Basemap itself does not do any plotting, but provides the ability to transform
coordinates into one of 25 different map projections.

• Matplotlib can also be used to plot contours, images, vectors, lines or points in
transformed coordinates. Basemap includes the GSSH coastline dataset, as well as
datasets from GMT for rivers, states and national boundaries.

Q.11 What is Seaborn?


Ans. : • Seaborn is a Python data visualization library based on Matplotlib. It
provides a high-level interface for drawing attractive and informative statistical
graphics. Seaborn is an opensource Python library.
• Its dataset-oriented, declarative API. User should focus on what the different
elements of your plots mean, rather than on the details of how to draw them.

You might also like