matplotlib savefig format parameter is not working

Viewed 569

I would like to save a figure in three formats at the same time. So the code I used is like this

import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,1)
y=x/(1+x)
fig, ax =plt.subplots()
ax.plot(x, y)
for fmt in ['jpg', 'pdf', 'svg']:
    fig.savefig('test', format='pdf')

However, only a file 'test' without extension is created. How to solve this problem?

2 Answers

Because you need to change:

fig.savefig('test', format='pdf')

To:

fig.savefig('test.%s' % fmt, format=fmt)

This is an example:

fname = 'Figure' 
for file_format in ['eps', 'pdf', 'png']:
    fig.savefig(fname + '.%s' % file_format, dpi=400, format=file_format)
Related