UNIT 2
UNIT 2
• 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
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
lOMoARcPSD|36553333
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):")
lOMoARcPSD|36553333
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.
lOMoARcPSD|36553333
• 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:
• 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:
lOMoARcPSD|36553333
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.
lOMoARcPSD|36553333
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.
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( faceco
lor="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:
lOMoARcPSD|36553333
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.
lOMoARcPSD|36553333
• 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:
• 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:
lOMoARcPSD|36553333
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:
lOMoARcPSD|36553333
uplims =upperlimits,
lolims = lowerlimits, )
plt.legend(loc='upper left')
plt.title('Example')
plt.show()
Output:
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.
lOMoARcPSD|36553333
• 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.
In a histogram, the data are grouped into ranges (e.g. 10 - 19, 20 - 29) and then
plotted as connected bars.
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.
lOMoARcPSD|36553333
• 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()
lOMoARcPSD|36553333
Plot legends give meaning to a visualization, assigning labels to the various plot
elements.
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:
lOMoARcPSD|36553333
• 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:
lOMoARcPSD|36553333
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.
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:
lOMoARcPSD|36553333
• 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.
Import matplotlib.pyplotasplt
Import numpy asnp
From math import pi
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))
lOMoARcPSD|36553333
plt.title('subplot'+'(2,3,' + str(i)+')')
plt.show()
Output:
Example :
Import plotly.graph_objectsasgo
fig=go.Figure()
fig.add_trace(go.Scatter( x=[0,1
,2,3,4,5,6,7,8],
lOMoARcPSD|36553333
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:
A tick is a short line on an axis. For category axes, ticks separate each category.
Customization
lOMoARcPSD|36553333
• 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.
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()
lOMoARcPSD|36553333
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.,
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:
lOMoARcPSD|36553333
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:
lOMoARcPSD|36553333
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.
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.
lOMoARcPSD|36553333
h) Tools for choosing color palettes that faithfully reveal patterns in your data.
Plot a Scatter Plot in Seaborn:
Import matplotlib.pyplot as plt
Import seaborn 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: