28/11/09 @ 00:17 (+0100), thus spake Mike Anderson:
> Hi,
> 
> How can I put the bottom axis on top (or on top AND on bottom) for a barh 
> plot?
> 
> I'm trying to mimic this, made with gnuplot:
> 
>   http://www.hep.wisc.edu/cms/comp/cmsprod/dCacheUserUsage.png
> 
> within matplotlib, and I've come close,
> 
>   http://www.hep.wisc.edu/cms/comp/cmsprod/diskUserUsage.png
> 
> 
> Also, is it possible to just have grid lines in one direction (say, 
> vertical), I don't think the horizontal grid is necessary.

It's somewhat counter-intuitive, but it can be done.
You have to create some "twin" axes with the "twiny" option,
then make the plot on the twin axes so it will use the
top axis. The bottom axis still have to be adjusted manually
to make it match the top one and remove the labels.

See this example:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

data = [21, 17, 18, 15, 14, 11, 9, 8, 4, 6, 7, 4, 5, 1, 3, 2, 0, 0]
names = ['%s%s' % (a, b)
         for a in 'abcdefg' for b in 'abcdefg'][:len(data)]

xlim = (0, max(data)+2)

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax2 = ax1.twiny()

# actual plot
ax2.barh(range(len(data)), data[::-1], 1, align='center', color='red')

# x-axis
ax2.set_xlim(xlim)
ax1.set_xlim(xlim)
ax1.xaxis.set_major_formatter(ticker.NullFormatter())

# y-axis
ax2.set_ylim(-0.5, len(data)-1+0.5)
ax2.yaxis.set_major_locator(ticker.FixedLocator(range(len(data))))
ax2.yaxis.set_major_formatter(ticker.FixedFormatter(names[::-1]))

# grid
ax2.set_axisbelow(True)
ax1.set_axisbelow(True)
ax1.grid(True)

plt.show()


If you want only a vertical grid, use
"ax1.xaxis.grid(True)" instead of "ax1.grid(True)".

Bye.

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to