0

This is important because it seems so simple that it can't be so difficult.

The problem: I would like to create a plot that has a previously defined size that does not change regardless of the content of the plot and is retained when saving. The plot itself doesn't matter but I try to do a scatter plot right now so that's why I show this example:

import matplotlib.pyplot as plt
#create the plot
#yes the last points are outside the picture so there're lost
x = [279, 118, 350] 
y = [302, 167, 550]
fig, ax = plt.subplots()
ax.set_ylim(ymin=0, ymax=528, auto=False)
ax.set_xlim(xmin=0, xmax=320, auto=False)
ax.axis('off') 
plt.xlim(0, 320)
plt.ylim(0, 528)
plt.plot(x, y, 'o')
plt.subplots_adjust(left=0, right=1, top=1, bottom=0),
plt.show()
#save it
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig('new2Dplot', format='tif', transparent=True, bbox_inches=extent)

The saved tif-file have the size of [496, 369] insteed of [320, 528] so it's also unusable. I think this problem have to do with margin, borders and the internal resize calculation, but why can't I just set everything to 0?

I think this posting is long enough so I won't write everything I tried. But things like: fig.tight_layout(), ax.get_tightbox(fig.canvas.get_renderer), bbox_inches='tight', of course I tried. My question seems very similar to this one Reduce left & right margins, but the solution they posted don't work for me.

  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – Patrick Artner Jul 28 '20 at 18:53
  • How about `plt.subplots(figsize=(320, 528))`? – Ynjxsjmh Jul 29 '20 at 03:36
  • I'm sorry #Patrick Artner, I had no intention of making it sound urgent, because it is not. Thank you for your advice I change the posting. – Kanshi Lovelace Jul 29 '20 at 15:34
  • #Ynjxsjmh nice approach: When I use ```figsize=(320, 528)``` an very large figure is open up (larger than my monitor size) and then the kernel breaks but when I use: ```plt.subplots(figsize=(320,528), dpi=1)``` the output file and the plot are perfect but all dots are gone. But I think this could be a first step. – Kanshi Lovelace Jul 29 '20 at 15:48

1 Answers1

0

With some help of @Ynjxsjmh I found a solution myself. I don't know why it's working and how. It seems so overcomplicated but at least it works:

import matplotlib.pyplot as plt
#create the plot
x = [279, 118, 350] 
y = [302, 167, 550]
fig, ax = plt.subplots(figsize=(320, 528), dpi=1)
ax.set_ylim(ymin=0, ymax=528, auto=False)
ax.set_xlim(xmin=0, xmax=320, auto=False)
ax.axis('off') 
plt.xlim(0, 320)
plt.ylim(0, 528)
plt.plot(x, y, 'o', markersize=100)
plt.subplots_adjust(left=0, right=1, top=1, bottom=0),
plt.show()
#save it
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig('new2Dplot', format='tif', transparent=True, bbox_inches=extent)