-1

This is driving me nuts.

Using matplotlib in python, I am drawing a simple plot (twenty pixels of different color) as follows:

fig1, ax = plt.subplots()
color_rgb_list = np.random.rand(n_colors, 3)
ax.imshow(color_rgb_list[None, :], aspect='equal')
for label in train_y:
    ax.text(label - 1.5, 1, str(label))
plt.tight_layout()
plt.show()
# fig1.savefig("test.png", bbox_inches='tight')

However, the result I get has a lot of white space around it. I found out that I can use bbox_inches='tight' when actually saving the figure to a file, however that does not really help me, as instead I would like to remove the white spaces while drawing the actual image. (I am using this for an app, where I want to display several different plots in one window using tkinter with .pack())

This is the results I get: when drawing/showing my plot. Instead I would like to remove all white spaces, especially at the top/bottom

How can I remove the white spaces when drawing/showing my plot? I would basically like to achieve the same thing that bbox_inches='tight' does, only when using plt.show(), or when drawing on canvas using tkinter.

Thanks in advance.

Simon Mayrshofer
  • 1,264
  • 1
  • 11
  • 18

1 Answers1

1

Since you're making such an irregular-sized plot, you can specify a figsize **kwarg that will stretch the window accordingly.


fig1, ax = plt.subplots(figsize=(10, 2)) # figsize = (width, height)
color_rgb_list = np.random.rand(n_colors, 3)
ax.imshow(color_rgb_list[None, :], aspect='equal')
for label in train_y:
    ax.text(label - 1.5, 1, str(label))
plt.tight_layout()
plt.show()

see if playing with that helps

Michael Green
  • 719
  • 6
  • 15
  • i have already played with that, unfortunately without success... but thanks. any other ideas? – Simon Mayrshofer Jul 09 '20 at 22:07
  • 1
    I actually got this to work, not sure why it was not working before, thanks a lot, i feel bit like an idiot. thanks again! – Simon Mayrshofer Jul 10 '20 at 08:33
  • I'm still getting a thin white border around the figure when I run exactly that code. @SimonMayrshofer did you ever figure out what the issue is? – sh37211 Aug 06 '22 at 23:22