0

I want to save a plot in python as a PDF. My problem ist, when I run my code I get a plot that looks like this:enter image description here

When I now maximise the window I get a plot that looks like this:enter image description here

Note that the second one looks much better, because it is not so "cramped". If I now save the plot using plt.savefig('floors.pdf') the saved plot looks like the first picture. I need a way to save it so that it looks like in the second picture.

I found this: How to make savefig() save image for 'maximized' window instead of default size but it is not helping because I don't want to save a set resolution in pixels. I want to save it as an PDF, because that way it stays a vector graphic.

My code:

import os
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from uncertainties import ufloat

os.chdir('PlayGround/SAOIF floors')

# data ------------------------------------------
floors = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,20,25,27,35,40,47,48,50,55,61,68,74,75,77,81])
N = np.arange(len(floors))+1 

# plot raw data ---------------------------------
fig = plt.figure()
plt.suptitle('SAOIF Floors')
ax1 = plt.subplot(121)
ax1.plot(N,floors,'o-')
ax1.set_title('All Floors')
ax1.set_ylabel('Floor Number')
ax1.set_xticks(np.arange(1, len(N)+1, len(N)//8))
ax1.set_yticks(np.arange(floors[0], floors[-1]+1, (floors[-1] - floors[0])//8))
ax1.grid()

# fit data --------------------------------------
floors = floors[13::]
N = N[13::]

x = N
y = floors

linera_function = lambda x, m, b: m*x+b

popt, pcov = curve_fit(linera_function, x, y)
error = np.sqrt(np.diag(pcov))

m = round(popt[0],2)
b = round(popt[1],2)

dm = round(error[0],2)
db = round(error[1],2)

# plot fit --------------------------------------
ax2 = plt.subplot(122)
ax2.set_title('Floors 14-81')
ax2.plot(x, y,'o', label='data')                                                                              # plot data
ax2.plot(x, linera_function(x, *popt), label=f'fit: $f(x)=m \cdot x + b$ \n$m={m}\pm{dm}$ \n$b={b}\pm{db}$')  # plot fit

# predicting next floor -------------------------
m = ufloat(m, dm)
b = ufloat(b, db)
next_floor = linera_function(N[-1]+1, m, b)

print('m = {:L}'.format(m))
print('b = {:L}'.format(b))
print('Next floor: {:L}'.format(next_floor))

# plot prediction -------------------------------
x = list(x)
y = list(y)
x.append(x[-1]+1)
y.append(next_floor.n)
next_floor_n = int(round(next_floor.n))
next_floor_s = int(round(next_floor.s))

ax2.errorbar(N[-1]+1, next_floor.n, yerr=next_floor.s, label=f'predicted next floor \nnumber$={next_floor_n} \pm {next_floor_s}$', capsize=5,  fmt='o')
ax2.plot([N[-1], N[-1]+1.7], [linera_function(N[-1], m, b).n, linera_function(N[-1]+1.7, m, b).n],'--', color='#ff7f0e')
ax2.set_xticks(np.arange(x[0], x[-1]+1, round((x[-1] - x[0])/8)))
ax2.set_yticks(np.arange(y[0], y[-1]+1, round((y[-1] - y[0])/8)))
ax2.legend()
plt.gcf()
fig.supxlabel('common_x')
plt.savefig('floors.pdf')

plt.show()
Akut Luna
  • 191
  • 2
  • 10
  • 2
    Define your `figsize` as shown in the second image. How? We don't know because you did not provide a [minimal, complete, and reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) but usually one would start with `fig, ax = plt.subplots(figsize=(10, 8))` or similar. – Mr. T Mar 12 '22 at 17:59
  • I added my code now. As I said I want to change the figure so it is saved maximised, not to a set ratio. – Akut Luna Mar 14 '22 at 12:35
  • 1
    What does "maximized" mean? Your "maximized" output is defined by the ratio of your screen, so if you want to imitate this, you have to retrieve the ratio of your maximized window and define the figure size using the same ratio. The size is rather not relevant in this context. See for instance also the comments here: https://stackoverflow.com/q/70924435/8881141 – Mr. T Mar 14 '22 at 14:10

0 Answers0