Matplotlib.pyplot.savefig() i Python

Matplotlib.pyplot.savefig() i Python

Matplotlib er svært nyttig visualiseringsbibliotek i Python. Det er et multi-plattform datavisualiseringsbibliotek bygget på NumPy-matriser og designet for å fungere med den bredere SciPy-stakken. Visualisering spiller en veldig viktig rolle siden den hjelper oss å forstå store databiter og trekke ut kunnskap.

Matplotlib.pyplot.savefig()

Som navnet antyder brukes savefig()-metoden for å lagre figuren opprettet etter plotting data. Figuren som er opprettet kan lagres på våre lokale maskiner ved å bruke denne metoden.

Syntaks: 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=Ingen)

Parametere:

PARAMETRE BESKRIVELSE
fnavn Filnavn .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()>

Utgang:

Eksempel 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()>

Utgang: