How can I plot a style like gnuplot "with pulses" with matplotlib?

I would like to create a graph like the one below using python / pandas / matplotlib. The top clip is not a problem, but I was not able to get the plot as the bottom clip to work. I can do this in gnuplot where the equivalent plot style is "with pulses". Is this possible with matplotlib? If this is not possible with matplotlib, is there another python graphics package that would work?

Gnuplot style for bottom clip

+3


source to share


2 answers


The easiest way to create a plot like this is to use pyplot.stem

. An example can be found here .

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.1, 6*np.pi, 50)
plt.stem(x, np.cos(x)+1, linefmt='g-', markerfmt=' ')
plt.stem(x, -np.sin(x)-1, linefmt='r-', markerfmt=' ', basefmt="gray")

plt.show()

      



enter image description here

Another option is to use pyplot.vlines

.

+5


source


Here's an example of working using vlines as @ImportanceOfBeingErnes suggested, which begs a different question. Is one solution preferred over the other? More efficient or better in some way?

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.1, 6*np.pi, 50)
plt.vlines(x, 0, np.cos(x)+1, color='g')
plt.vlines(x, 0, -np.sin(x)-1, color='r')
plt.show()

      



enter image description here

+1


source







All Articles