Resize Spyder and Matplotlib shapes for saved plots only

I would like to look at the matplotlib plots inside the Spyders IPython console in one size and save the numbers in a multi-page PDF in a different size.

I am currently setting the shape size as follows:

plt.rc('axes', grid=True)
plt.rc('figure', figsize=(12, 8))
plt.rc('legend', fancybox=True, framealpha=1)

Then I draw some numbers and save them in a list to save the PDF file later. This works great when used alone. Charts of the appropriate size for viewing in the IPython Spyder console.

At the end of my script, I have a loop to view each shape I want to save. Here I want to set the format and size of the shape exactly for better printing on A3 paper.

with PdfPages('multi.pdf') as pdf:
    for fig in figs:
        fig.tight_layout()
        fig.set_size_inches(420/25.4, 297/25.4)
        pdf.savefig(figure=fig)

PDF , , , Spyder. , Spyder. A3 .

, : PDF , Spyder?

+4
2

@ImportanceOfBeingErnest .

, plt.rc , PDF.

, :

plt.rc('axes', grid=True)
plt.rc('figure', figsize=(12, 8))
plt.rc('legend', fancybox=True, framealpha=1)

, , . PDF :

with PdfPages('multi.pdf') as pdf:
    for fig in figs:
        fig.set_size_inches(420/25.4, 297/25.4)
        pdf.savefig(figure=fig, bbox_inches='tight')
        fig.set_size_inches(plt.rcParams.get('figure.figsize'))

fig.tight_layout() , .

0

@ImportanceOfBeingErnest, , , .

, , , , , pdf, , IPython. , pdf, , IPython , :

enter image description here

, IPython, , , PDF IPython, :

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np
from IPython.display import Image, display
try:  # Python 2
    from cStringIO import StringIO as BytesIO
except ImportError:  # Python 3
    from io import BytesIO

# Generate a matplotlib figures that looks good on A3 format :

fig, ax = plt.subplots()
ax.plot(np.random.rand(150), np.random.rand(150), 'o', color='0.35', ms=25,
        alpha=0.85)

ax.set_ylabel('ylabel', fontsize=46, labelpad=25)
ax.set_xlabel('xlabel', fontsize=46, labelpad=25)
ax.tick_params(axis='both', which='major', labelsize=30, pad=15,
               direction='out', top=False, right=False, width=3, length=10)
for loc in ax.spines:
    ax.spines[loc].set_linewidth(3)

# Save figure to pdf in A3 format:

w, h = 420/25.4, 297/25.4
with PdfPages('multi.pdf') as pdf:
    fig.set_size_inches(w, h)
    fig.tight_layout()
    pdf.savefig(figure=fig)
    plt.close(fig)

# Display in Ipython a sclaled bitmap using a buffer to save the png :

buf = BytesIO()
fig.savefig(buf, format='png', dpi=90)
display(Image(data=buf.getvalue(), format='png', width=450, height=450*h/w,
              unconfined=True))

IPython : enter image description here

+3

Source: https://habr.com/ru/post/1674126/


All Articles