Matplotlib.pyplot.savefig() i Python

Matplotlib.pyplot.savefig() i Python

Matplotlib är ett mycket användbart visualiseringsbibliotek i Python. Det är ett multiplattformsdatavisualiseringsbibliotek byggt på NumPy-arrayer och designat för att fungera med den bredare SciPy-stacken. Visualisering spelar en mycket viktig roll eftersom den hjälper oss att förstå enorma databitar och extrahera kunskap.

Matplotlib.pyplot.savefig()

Som namnet antyder används savefig()-metoden för att spara figuren som skapades efter plottning data. Den skapade figuren kan sparas på våra lokala maskiner genom att använda denna metod.

Syntax: savefig(fname, dpi=Ingen, facecolor='w', edgecolor='w', orientation='portrait', papertype=Ingen, format=Ingen, transparent=False, bbox_inches=Ingen, pad_inches=0.1, frameon=Ingen, metadata=Inga)

Parametrar:

PARAMETRAR BESKRIVNING
fname Filnamn .webp'code-block'>




# importing required modules> import> matplotlib.pyplot as plt> > # creating plotting data> xaxis> => [> 1> ,> 4> ,> 9> ,> 16> ,> 25> ,> 36> ,> 49> ,> 64> ,> 81> ,> 100> ]> yaxis> => [> 1> ,> 2> ,> 3> ,> 4> ,> 5> ,> 6> ,> 7> ,> 8> ,> 9> ,> 10> ]> > # plotting> plt.plot(xaxis, yaxis)> plt.xlabel(> 'X'> )> plt.ylabel(> 'Y'> )> > # saving the file.Make sure you> # use savefig() before show().> plt.savefig(> 'squares.webp'> )> > plt.show()>

Utgång:

Exempel 2:




# importing the modules> import> matplotlib.pyplot as plt> > > # creating data and plotting a histogram> x> => [> 1> ,> 4> ,> 9> ,> 16> ,> 25> ,> 36> ,> 49> ,> 64> ,> 81> ,> 100> ]> plt.hist(x)> > # saving the figure.> plt.savefig(> 'squares1.webp'> ,> > bbox_inches> => 'tight'> ,> > pad_inches> => 1> ,> > transparent> => True> ,> > facecolor> => 'g'> ,> > edgecolor> => 'w'> ,> > orientation> => 'landscape'> )> > plt.show()>

Utgång: