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.

mathtick
  • 6,487
  • 13
  • 56
  • 101
  • 1
    what sort of artists do you have on your axes? – tacaswell Apr 03 '13 at 00: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. – mathtick Apr 03 '13 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. – tacaswell Apr 03 '13 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. – mathtick Apr 04 '13 at 01:30
  • My intuition on this comes from looking at how `twiny` and `twinx` work underneath. Maybe this is simpler, but would be surprised. – tacaswell Apr 04 '13 at 01:32
  • @pelson do you know if this can be done? – tacaswell Apr 07 '13 at 05:13
  • possible duplicate of [How to switch axes in matplotlib?](http://stackoverflow.com/questions/2361872/how-to-switch-axes-in-matplotlib) (note that one didn't get a good answer either) – tacaswell Apr 07 '13 at 05:17

2 Answers2

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())
hitzg
  • 12,133
  • 52
  • 54
tacaswell
  • 84,579
  • 22
  • 210
  • 199
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)
Syrtis Major
  • 3,791
  • 1
  • 30
  • 40
  • 4
    The autoscaling did not work for me: I had to put `line.axes.relim()` before `line.axes.autoscale_view()`, see https://stackoverflow.com/questions/7187504/set-data-and-autoscale-view-matplotlib. – Lia Fiona Jul 16 '19 at 12:26