I'd like to create a plot like the one below with python/pandas/matplotlib. The upper clip is no problem, but I haven't been able to get a plot like the lower clip to work. I can do it in gnuplot where the equivalent plot style is 'with impulses'. Is this possible with matplotlib? If it is not possible with matplotlib is there another python graphics package that would work?
Asked
Active
Viewed 4,427 times
4
-
Looks like you can obtain this simply by using subplots for generating two plots with sharedx where the upper one is a normal plot and the lower one is a barplot. But maybe you should explain the theory of those values so that we don't need to reverse-engineer this. And if you expect someone to post ready code, it would be easier, if you provide a simple data-example. – sascha Jun 13 '17 at 23:15
-
Each period there are two values for the lower clip. The number of stocks making new 52-week highs new highs and the number of new lows. New highs are a positive number and are plotted above the zero line with green impulses, new lows are a negative number plotted below the zero line with red impulses. – John Jun 13 '17 at 23:28
-
That chart was created by a c program using libpng, so my example code wouldn't be very useful. I get the upper clip and the sharex parts, but I can't get anywhere with creating the lower clip. – John Jun 13 '17 at 23:35
-
If you want to do this in python, you probably do have access to data within python. Add it to your question or generate some random-data with comparable behaviour. OF course someone could post an answer without, but motivation will be decreased and maybe he made different assumptions about that data. Also: what's the problem implementing something given [this](http://matplotlib.1069221.n5.nabble.com/matplolib-equivalent-of-gnuplot-s-impulse-td8972.html) and [this](https://stackoverflow.com/questions/25550308/pyplot-bar-chart-of-positive-and-negative-values)? The latter beeing my previous rec. – sascha Jun 13 '17 at 23:45
2 Answers
7
The easiest way to create such a plot 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()
Another option is to use pyplot.vlines
.

ImportanceOfBeingErnest
- 321,279
- 53
- 665
- 712
-
-
-
@John You now have more than 15 rep and can up-vote. Feel free to up vote this answer in addition to accepting it thx :-) [What to do when someone answers my question](https://stackoverflow.com/help/someone-answers) – piRSquared Jun 14 '17 at 00:26
1
Here is a worked example using vlines as @ImportanceOfBeingErnes suggested, which raises another question. Is one solution preferable to 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()

John
- 435
- 6
- 15
-
I chose to use vlines as in my application it plots faster than stem. I'll post a fully worked example when I am done. – John Jun 15 '17 at 14:56