Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

# What is the Matplotlib.pyplot.stem() method in Python? Fouzia Bashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

### Overview

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.

### Stem plot

A graph is used to classify data as leaf and step. It has a baseline with heads moving outwards away from the baseline.

Stem plot

### The Matplotlib.pyplot.stem() method

The 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.

### Horizontal stem plot

For the horizontal stem plot, locs are x positions while the heads are y values.

### Vertical stem plot

For the vertical stem plot, locs are y positions while the heads are x values.

### Syntax

matplotlib.pyplot.stem(locs, heads, linefmt=None, markerfmt=None, basefmt=None)

### Parameters

• 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.

### Return value

This method returns a tuple of marker lines, stemlines, and baselines.

### Example

# import libraries in program
import matplotlib.pyplot as plt
import numpy as np
# generate x values
x = np.linspace(1, 2** np.pi)
# generate y values.
y = np.exp(np.cos(x))
# invoking stem() method
plt.stem(x, y, linefmt ='red', markerfmt ='-.', bottom = 1.1, use_line_collection = True)
# save above generated plot as png image in root directory
plt.savefig('output/graph.png')

### Explanation

• Lines 2 and 3: We import the matplotlib.pyplot API as plt and NumPy as np.
• Line 5: We use the np.linspace() method that generates 50 sample ranges between 1 and 8.824977827076287.
• Line 7: We calculate the exponent of np.cos(x) values and return a list of 50 samples, x.
• Line 9: We pass the following parameters to the 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 lines
• Line 11: We use the plt.savefig() method to save the output graph as agraph.png file.

RELATED TAGS

python

CONTRIBUTOR Fouzia Bashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Learn in-demand tech skills in half the time 