Python Matplotlib line plot aligned with contour/imshow

前端 未结 3 613
误落风尘
误落风尘 2021-02-08 15:36

How can I set the visual width of one subplot equal to the width of another subplot using Python and Matplotlib? The first plot has a fixed aspect ratio and square pixels from i

3条回答
  •  天命终不由人
    2021-02-08 15:48

    Are you looking for arbitrary positioning relative to the first axis? You could play around with the figure's bbox.

    ax2.set_position(ax.get_position().translated(0, -.5)) Will trivially place the second axis under the first with the same basic shape. Or you can do

    box = ax.get_position()
    
    # Positioning code here
    
    ax2.set_position(box)
    

    Where your positioning code then alters box by reassignment (box = box.translated(0, -.5)) or mutation (box.x1 += .1). Box appears to expose it's bottom-left and top-right points with attributes .p0, .x0, .y0, and .p1, .x1, .y1; as well as .width and .height

    Box is more or less a figure coordinate, and you can just "set the width, height, and offsets" with raw numbers explicitly too: ax2.set_position([left, bottom, width, height])

    PS: Unfortunately, this bbox also includes the text labels in it's width and height. For example, your first plot has a width of 0.27... and a height of 0.36... You won't distort the text by altering the dimensions, but it does mean it's hard to get a perfect square unless you start with one.

提交回复
热议问题