Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib line plot dont show vertical lines in step function

Tags:

matplotlib

I do have a plot that only consists of horizontal lines at certain values when I have a signal, otherwise none. So, I am looking for a way to plot this without the vertical lines. there may be gaps between the lines when there is no signal and I dont want the lines to connect nor do I want a line falling off to 0. Is there a way to plot this like that in matplotlib?

enter image description here

self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
axes = self.figure.add_subplot(111)
axes.plot(df.index, df["x1"], lw=1.0, c=self.getColour('g', i), ls=ls)
like image 229
chrise Avatar asked Feb 15 '17 01:02

chrise


People also ask

How do you plot a vertical line?

To graph a vertical line that goes through a given point, first plot that point. Then draw a straight line up and down that goes through the point, and you're done!

What does PLT step do?

The step() function designs the plot such that, it has a horizontal baseline to which the data points will be connected by vertical lines. This kind of plot is used to analyze at which points the change in Y-axis value has occurred exactly with respect to X-axis.

How do you graph straight lines in PLT?

Use plt. plot() to plot a horizontal line Call plt. plot(x, y) with x as a sequence of differing x-coordinates and y as a sequence of equal y-coordinates to draw a horizontal line.


1 Answers

The plot you are looking for is Matplotlib's plt.hlines(y, xmin, xmax).

For example:

import matplotlib.pyplot as plt

y = range(1, 11)
xmin = range(10)
xmax = range(1, 11)
colors=['blue', 'green', 'red', 'yellow', 'orange', 'purple', 
        'cyan', 'magenta', 'pink', 'black']

fig, ax = plt.subplots(1, 1)
ax.hlines(y, xmin, xmax, colors=colors)
plt.show()

Yields a plot like this:

Matplotlib hlines plot

See the Matplotlib documentation for more details.

like image 75
Brian Avatar answered Sep 28 '22 01:09

Brian