How to specify the size with add_dock_widget?

I want to use add_dock_widget to display the matplotlib figure, but the margin is too big. How can I specify the size in add_dock_widget?

view1 = napari.view_image(image)
    canvas = FigureCanvas(Figure(figsize=(5, 15)))

    axes1 = canvas.figure.add_subplot(3, 1, 1)
    axes1.imshow(np.zeros((100, 100), np.uint8))
    axis2 = canvas.figure.add_subplot(3, 1, 2)
    axis2.imshow(np.zeros((100, 100), np.uint8))
    axis3 = canvas.figure.add_subplot(3, 1, 3)
    axis3.imshow(np.zeros((100, 100), np.uint8))
    canvas.figure.subplots_adjust(left=0, bottom=0.1, right=1, top=0.95, wspace=0, hspace=0.4)
view1.window.add_dock_widget(canvas, area='right')
1 Like

you need to adjust the canvas widget itself (the dock_widget just obeys the sizing of the widgets it contains). The fastest way here would just be to set a maximumWidth on the canvas object:


However, that may not resize super well. So for maximum compatibility, you would need to adjust the sizePolicy of the canvas widget. Have a look through this previous answer if you’d like more guidance on that:

matplotlib itself may also offer an API to set size policies and margins on their widgets, so you can look through their docs for that as well.

@talley Reading the previous answers you gave me helped me figure out what I wanted to do! Thank you very much.

1 Like