Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas save plot to file and don't show on the screen

Tags:

python

pandas

I'm making some plots of fitted data and saving them to files nicely with.

# Make the main plot.
ax = df.plot(x=df.x, y=['counts', 'fit', 'res'], title=save_file_name,
        style=['b', 'g--', 'r'], xlim=xlim, ylim=ylim)
# Over plot some individual gaussians from my fit_gaussians dataframe.
for i, fit_gaus in fit_gaussians.iterrows():
    plt.plot(df.x, gaussian(df.x, fit_gaus.h, fit_gaus.mu,
            fit_gaus.sigma), 'y', linestyle='--')
# Save to file.
fig = ax.get_figure()
fig.savefig('test.ps')

This works great, but it also opens a window and plots to the screen. I know I can easily use matplotlib directly to recreate what I'm doing with df.plot, and just never call plt.show, but I feel like there should be some way create plot in a file with pandas with out also opening a window.

like image 788
TristanMatthews Avatar asked Jan 19 '26 18:01

TristanMatthews


1 Answers

If you only don't want the pop ups you can do %matplotlib inline otherwise before you define ax just do:

plt.ioff()

Then whenever you want to actually show it you can do plt.show()

like image 118
rontho1992 Avatar answered Jan 21 '26 07:01

rontho1992



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!