12

Suppose I have a plotting function that takes an axes argument (or returns one). Is there some low-level method for transposing the whole plot so that the x-axis becomes the y-axis and vice-versa? Or even the axes before the plot so that the plotting function just does everything correctly (labeling) by relying on the axes functions?

I know how to do this "manually", but I'm wondering if there is a slightly hidden level of abstraction that allows this kind of transformation.

7
  • 1
    what sort of artists do you have on your axes? Commented Apr 3, 2013 at 0:39
  • 1
    Good question, I guess I could be specific and mention that I am thinking of the gfx plot function attached to pandas Series (and DataFrame) objects (via dataframe.plot() ... ). I'll review the code and try to get more specific info later if you're not familiar with the panadas plot function. Commented Apr 3, 2013 at 15:25
  • 1
    The thing that worries me about this is that there are too many little bits and pieces that would need to be switched separately and any solution will end up being really brittle. Commented Apr 3, 2013 at 19:03
  • I think you're probably right ... it's probably easiest to do this at the specialized level of the the actual plot. There will be a lot of small things to adjust such as the labeling. Commented Apr 4, 2013 at 1:30
  • My intuition on this comes from looking at how twiny and twinx work underneath. Maybe this is simpler, but would be surprised. Commented Apr 4, 2013 at 1:32

2 Answers 2

9

An old post (circa 2005) to the mailing list from John Hunter. I suspect that this a rare enough of a desire and tricky enough to do that it has not been added sense then.

John Hunter's example for swapping axes on an existing plot was

line2d = plot(rand(10))[0]

def swap(xdata, ydata):
    line2d.set_xdata(ydata)
    line2d.set_ydata(xdata)
    draw()

swap(line2d.get_xdata(), line2d.get_ydata())
Sign up to request clarification or add additional context in comments.

Comments

4

Not more than a wrap for tcaswell's answer.

def swap(*line_list):
    """
    Example
    -------
    line = plot(linspace(0, 2, 10), rand(10))
    swap(line)
    """
    for lines in line_list:
        try:
            iter(lines)
        except:
            lines = [lines]

        for line in lines:
            xdata, ydata = line.get_xdata(), line.get_ydata()
            line.set_xdata(ydata)
            line.set_ydata(xdata)
            line.axes.autoscale_view()

An example

line = plot(linspace(0, 2, 10), rand(10))
swap(line)

1 Comment

The autoscaling did not work for me: I had to put line.axes.relim() before line.axes.autoscale_view(), see stackoverflow.com/questions/7187504/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.