pythonpandastrellis

trellis plots with pandas.tools.rplots: axes labels


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?


Solution

  • 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.