17

I want to plot(x, sin(x)) but instead of a line from (xi,yi) to (x_i+1,y_i+1) I want a vertical line to each point from (xi,0) to (xi,yi) as sometimes interpolation between the points makes no sense (e.g. quantized data) (and it looks much better with that vertical line than without).

Much like the stem plot below (from the matlab docs) but with the option of being able to turn off the circles and the "-" between points. Unfortunately, I couldn't find an appropriate plotting function in the matplotlib documentation myself.

stem plot

2 Answers 2

19

There is a stem plot (a.k.a. lollipop plot) in the matplotlib as well.


Below you can find an example from the docs.

import matplotlib.pyplot as plt
import numpy as np

# returns 10 evenly spaced samples from 0.1 to 2*PI
x = np.linspace(0.1, 2 * np.pi, 10)

markerline, stemlines, baseline = plt.stem(x, np.cos(x), '-.')

# setting property of baseline with color red and linewidth 2
plt.setp(baseline, color='r', linewidth=2)

plt.show()

stem plot


If you want to adjust the y-position of the baseline, you can use the bottom parameter.

(adapted) Example:

import matplotlib.pyplot as plt
import numpy as np

# returns 10 evenly spaced samples from 0.1 to 2*PI
x = np.linspace(0.1, 2 * np.pi, 10)

plt.stem(x, np.cos(x), '-.', bottom=-2)

plt.show()

stem plot 2

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks now I need to figure out how to remove the blue circles.
@user948652 you only need to set the markerfmt parameter appropriately, e.g. markerfmt='.'. Just have a look at the documentation of the call parameters, it's nicely explained there.
is there a way to start the stems at negative infinity, though? I want something like this: namoseley.files.wordpress.com/2012/09/…
@endolith You can use the bottom parameter to specify the y-position of the baseline.
@moooeeeep: Must be new? stem() got an unexpected keyword argument 'bottom'
|
0

I think you want to use linestyle='steps--'

plot(x, sin(x), linestyle='steps--')

2 Comments

This is a horizontal step plot, doesn't do vertical lines like in the .gif link
My bad, I think @moooeeeep has it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.