Matplotlib is used to visualize statistical data for clear and concise understanding. It makes complicated data coherent with the aid of different graphs and plots.
A graph is used to classify data as leaf and step. It has a baseline with heads moving outwards away from the baseline.
Matplotlib.pyplot.stem()
methodThe stem()
method of the matplotlib.pyplot
API is used to create a stem plot. It can be plotted in two ways either vertically or horizontally.
For the horizontal stem plot, locs
are x positions while the heads
are y values.
For the vertical stem plot, locs
are y positions while the heads
are x values.
matplotlib.pyplot.stem(locs, heads, linefmt=None, markerfmt=None, basefmt=None)
locs
: For the horizontal plot, these are y-positions of the stem. On the other hand, for a vertical plot, these value shows the x-position of the stem.heads
: For the horizontal plot, these are x-values of stem heads. On the other hand, for a vertical plot, these value shows the y-values of stem heads.linefmt
: This string value defines the color and style of vertical lines.marketfmt
: This string value defines the color and style of markers.basefmt
: This is a format for the baseline.This method returns a tuple of marker lines, stemlines, and baselines.
# import libraries in programimport matplotlib.pyplot as pltimport numpy as np# generate x valuesx = np.linspace(1, 2** np.pi)# generate y values.y = np.exp(np.cos(x))# invoking stem() methodplt.stem(x, y, linefmt ='red', markerfmt ='-.', bottom = 1.1, use_line_collection = True)# save above generated plot as png image in root directoryplt.savefig('output/graph.png')
matplotlib.pyplot
API as plt
and NumPy as np
.np.linspace()
method that generates 50 sample ranges between 1
and 8.824977827076287
.np.cos(x)
values and return a list of 50 samples, x
.stem
method:locs
and heads
as a list of 50 values.linefmt
indicates lines will be red
in color.markerfmt
shows the style of the marker.use_line_collection
is set to True
, which means it will first use lines collection and then individual linesplt.savefig()
method to save the output graph as agraph.png
file.