python matplotlib: way to transpose axes

后端 未结 2 1921
無奈伤痛
無奈伤痛 2020-12-20 14:17

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

相关标签:
2条回答
  • 2020-12-20 14:49

    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())
    
    0 讨论(0)
  • 2020-12-20 15:01

    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)
    
    0 讨论(0)
提交回复
热议问题