I have stumbled accross the following snippet, for creating horizontal bar chart using matplotlib:
import matplotlib
from pylab import *
val = 3+10*rand(5) # the bar lengths
pos = arange(5)+.5 # the bar centers on the y axis
print pos
figure(1)
barh(pos,val, align='center')
yticks(pos, ('Tom', 'Dick', 'Harry', 'Slim', 'Jim'))
xlabel('Performance')
title('horizontal bar chart using matplotlib')
grid(True)
show()
I want to modify the above script as follows:
- Make the plotted bars ‘less chunky’ (i.e. reduce the height of the plotted horiz bars)
- Plot both negative and positive numbers as horizontal bars on the same plot
any help (code snippet or links) to help me make the above modifications would be very helpful.
as an aside, if I wanted to make stacked horizontal bars (say each label had 3 stacked horizontal bars), how would I modify the code above to do plot a 3 stacked horizontal bar plot?
[[Edit]]
Could someone post two short code snippet that shows how to:
-
Print labels on the opposite side of the horizontal bars (so that for example, the label for ‘negative’ bars appears in the 1st quarant, and the labels for ‘positive’ bars appears in the 2nd quadrant
-
Plot multiple (say 2 or 3) horizontal bars (instead of just one). Good examples are the first two images shown here
In general, I’d suggest not using
from pyplot import *. Unless you’re in the interactive mode, use the object-oriented approach:A good starting point for various sorts of plots is the
matplotlibgallery