UNIT Vnotes
UNIT Vnotes
Syllabus
Importing Matplotlib - Line plots - Scatter plots - visualizing errors - density and contour plots
Histograms - legends - colors - subplots - text and annotation - customization three dimensional
plotting - Geographic Data with Basemap - Visualization with Seaborn
• Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and
its numerical extension NumPy.
• 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.
• 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.
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
• To define a plot, you need some values, the matplotlib.pyplot module, and an idea of what you
want to display.
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
Line Plot
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
• 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 numpy as np
x = np.linspace(-1, 1, 50)
y1 = 2*x+ 1
y2 = 2**x + 1
plt.plot(x, y2)
plt.plot(x, y1,
linewidth=1.0,
linestyle='--'
plt.show()
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:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
importmatplotlib.pyplot as plt
importnumpy as np
print("Values of :")
print("Values of Y):")
print (Y)
plt.plot(X, Y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
# Set a title
plt.title('Draw a line.')
plt.show()
• 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.
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.
• 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:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
• 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.
• 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')
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
• 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 :
import numpy as np
xa = np.linspace(0, 5, 20)
ya = xa**2
ya = 3*xa
plt.show()
Output:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
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:
2. Cross-like: *,+, 1, 2, 3, 4
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
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
• 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.
• 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.
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
plt.xlabel('Entries')
plt.ylabel('Values')
plt.plot(range(1,11), values)
plt.show()
W=4
h=3
d = 70
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(
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
facecolor="black", width=0.5,
headwidth=4, shrink=0.1)
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 numpy as np
x = np.linspace(-10, 9, 20)
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
y = x ** 3
Z = x ** 2
figure = plt.figure()
axes = figure.add_axes([0,0,1,1])
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.
• 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]
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().
a) If we need a basic scatter plot, use plt.plot(), especially if we want to prioritize performance.
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
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:
• Scatterplots are especially important for data science because they can show data patterns that
aren't obvious when viewed in other ways.
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
y_axis1 =[5, 16, 34, 56, 32, 56, 32, 12, 76, 89]
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.
• 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:
Where:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
yerr = The error value of the Y axis. Each point has its own error value.
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
plt.show()
Output:
• We plot using the command "plt.errorbar (...)", giving it the desired characteristics.
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
importmatplotlib.pyplot as plt
importnumpy as np
x = np.arange(1,8)
y = np.array([20,10,45,32,38,21,27])
plt.show()
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:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
• 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)
uplims = True, )
uplims = True,
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
lolims True, )
uplims =upperlimits,
lolims = lowerlimits, )
plt.legend(loc='upper left')
plt.title('Example')
plt.show()
Output:
• 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 :
1. Contour plot :
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
• 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
X, Y = np.meshgrid(xlist, ylist
print(xlist)
print(ylist)
print(X)
print(Y)
Output:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
importmatplotlib.pyplot as plt
plt.figure()
plt.clabel(cp, inline=True,
fontsize=10)
plt.title('Contour Plot')
plt.xlabel('x (cm))
plt.ylabel('y (cm)')
plt.show()
Output:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
• 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.
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
c) The "Deformed shape with undeformed model" option overlays the contour plot on the
original finite element model.
5.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".
• 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.
import numpy as np
x = 40* np.random.randn(50000)
plt.legend()
plt.title(' Histogram')
plt.show()
5.6 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.
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()
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
ax = plt.subplot(111)
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.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
p=Polynomial(-0.8,2.3,0.5,1,0.2)
p_der=p.derivative()
fig, ax=plt.subplots()
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:
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)
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
fig = plt.figure()
ax = plt.subplot(111)
plt.title('Legend inside')
shadow=True, ncol=2)
plt.show()
Output:
5.7Subplots
• 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.
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
fig, ax = plt.subplots()
ax.plot(x,y)
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.
• 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
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
x=np.linspace (0,2*pi,100)
foriinrange(1,7):
plt.plot(np.sin(x), np.cos(i*x))
plt.title('subplot'+'(2,3,' + str(i)+')')
plt.show()
Output:
• 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().
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
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.
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()
# Annotation
ax.annotate('Local Max',
xy = (3.3, 1),
shrink =0.05))
ax.set_ylim(-2, 2)
plt.plot(x, y)
plt.show()
Output:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
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,
showarrow=True,
arrowhead=1)
fig.add_annotation(x=4,y=4,
showarrow=False,
yshift = 10)
fig.update_layout(showlegend=False)
fig.show()
Output:
5.9 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.
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
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
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"""
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
ry=np.ones(rn)
ry[rx>=0.5]=0
returnry
ry={half-sawtooth':lambdarx:rx.copy(),
'top-hat':tophat,
'sawtooth':lambdarx:2*np.abs(rx-0.5)}
nrep=4
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x,y,'k',lw=2)
ax.set_ylim(-0.1,1.1)
ax.set_xlim(x[0]-0.5,x[-1]+0.5)
ax.minorticks_on()
ax.tick_params(which='minor',length=5,width=2, direction='in')
ax.grid(which='both')
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
plt.show()
Output:
• 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.
importmatplotlib.pyplot as plt
• 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.
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111, projection='3d')
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
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_xlabel('x',labelpad=20)
ax.set_ylabel('y', labelpad=20)
ax.set_zlabel('t', labelpad=20)
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
plt.show()
Output:
• 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.
• 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.
import warnings
warnings.filterwarmings('ignore')
importmatplotlib.pyplot as plt
map = Basemap()
map.drawcoastlines()
# plt.show()
plt.savefig('test.png')
Output:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
• 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:
Seaborn works easily with dataframes and the Pandas library. The graphs created can also be
customized easily.
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.
importmatplotlib.pyplot as plt
importseaborn as sns
import pandas as pd
df = pd.read_csv('worldHappiness2016.csv').
plt.show()
Output:
P.T.Lee Chengalvaraya Naicker
College Of Engineering & Technology
P.T.Lee Chengalvaraya Naicker Nagar, Oovery, Kancheepuram – 631 502.
Department of Computer Science And Engineering
Ans.: • Constructing ways in absorbing information. Data visualization enables users to receive
vast amounts of information regarding operational and business conditions.
Ans. Reasons:
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.
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.
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.
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.
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.
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.
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.