0

I'm having a hard time getting pandas.tools.rplots to label my axes. Here's a minimal example:

 import pandas as pd
 import pandas.tools.rplot as rplot
 import numpy as np
 import matplotlib.pyplot as plt

 sitecodes = ['A'] * 10 + ['B'] * 10 + ['C'] * 10
 altitude = np.random.rand(len(sitecodes)) * 1000
 obs = np.random.rand(len(sitecodes))

 df = pd.DataFrame({'sitecodes':sitecodes,
                    'altitude':altitude,
                    'obs':obs})

 plt.figure()
 plot = rplot.RPlot(df, x='altitude', y='obs')
 plot.add(rplot.TrellisGrid(['sitecodes', '.']))
 plot.add(rplot.GeomScatter())
 discard = plot.render()
 plt.show()

Some of the examples in http://pandas.pydata.org/pandas-docs/stable/visualization.html#trellis-plotting-interface have axis labels and some do not; in my example above mine don't. I can't find a way to add them in the documentation I can find or by poking around inside the plot object that rplot.RPlot returns.

Surely there's a way to label axes?

  • If you are struggling with pandas native trellis plots I would suggest having a look at mwaskom's superb [seaborn](http://web.stanford.edu/~mwaskom/software/seaborn/tutorial/axis_grids.html) package. It can do trellis plots and a whole lot more. – b10n Oct 25 '14 at 10:36
  • Note that this `rplots` submodule of pandas is not actively developed, and chances are that is will become deprecated (https://github.com/pydata/pandas/issues/3445). So indeed, you can look at [seaborn](http://web.stanford.edu/~mwaskom/software/seaborn/tutorial/axis_grids.html) or [ggplot for python](http://ggplot.yhathq.com/) – joris Oct 26 '14 at 22:52
  • @b10, I've seen seaborn recommended in a couple of blog posts too. I was hoping to just use the stuff I already have installed, but I'll check it out. – Timothy W. Hilton Oct 28 '14 at 02:54
  • @joris, thanks - I scanned that discussion and it's useful to know. – Timothy W. Hilton Oct 28 '14 at 02:55

1 Answers1

0

This might be a bug or "work in progress" ...

However if at the end of your code just before plt.show() you add this:

for i, a in enumerate(plt.gcf().axes):
    if a.get_xticks().any():
        plt.setp(a, xlabel='altitude')
    if i == 1:
        plt.setp(a, ylabel='obs')
plt.show()

It will add X Label to the bottom Axe and Y Label to the middle Axe.

This is indeed not a universal solution and needs to be adjusted if x, y dimension of Trellis plot are different.

EDIT:

A Universal solution would be to add a Text to the Figure, which could be done like this: Instead of your discard = plot.render() use this code:

fig = plt.gcf()
fig.text(s='altitude', x=0.5, y=0, ha='center')
fig.text(s='obs', x=0, y=0.5, va='center', rotation='vertical')
discard = plot.render(fig)

This should work regardless of number of axes used in the plot.

Primer
  • 10,092
  • 5
  • 43
  • 55