10 examples of 'save matplotlib figure as png' in Python

Every line of 'save matplotlib figure as png' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
94def save_figure(filename, extra_artists=None, tight=True):
95 filename = filename.replace('.', '-')
96 filename += '.pdf'
97 if extra_artists:
98 plt.savefig(
99 FIGURES_DIR + filename,
100 bbox_extra_artists=extra_artists,
101 bbox_inches='tight')
102 else:
103 if tight:
104 plt.tight_layout()
105 plt.savefig(FIGURES_DIR + filename)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

137def save_figure(filename, fig=None):
138 """Apply the figure options for saving and then save the supplied
139 figure to ``filename``.
140
141 The format of the output figure is set by ``options.plotting_outtype``.
142
143 **Parameters:**
144
145 filename : string
146 The output filename.
147 fig : figure object, optional
148 The figure to be saved.
149
150 **Returns:**
151
152 ``None``.
153 """
154 if fig is None:
155 fig = pylab.gcf()
156 fig.set_size_inches(*options.plotting_save_figsize)
157 pylab.savefig(filename, dpi=100, bbox_inches='tight',
158 format=options.plotting_outtype, pad=0.1)
159 fig.set_size_inches(*options.plotting_display_figsize)
4def ggsave(plot, filename, width=None, height=None):
5 plot.make()
6 w, h = plot.fig.get_size_inches()
7 if width:
8 w = width
9 if height:
10 h = height
11 plot.fig.set_size_inches(w, h)
12 plot.fig.savefig(filename)
1425def save_figure(
1426 figure, save_at="", name="fig", save_fmts=["png"], output=False, **kwargs
1427):
1428 """
1429 Save a figure at a specified location in a number of formats.
1430 """
1431 default_config = dict(dpi=600, bbox_inches="tight", transparent=True)
1432 config = default_config.copy()
1433 config.update(kwargs)
1434 for fmt in save_fmts:
1435 out_filename = os.path.join(str(save_at), name + "." + fmt)
1436 if output:
1437 logger.info("Saving " + out_filename)
1438 figure.savefig(out_filename, format=fmt, **config)
70def render_mpl(self, fig, path, **kwargs):
71 return fig.savefig(path, format=self.name, **kwargs)
36def savefig(save_as):
37 """Save the figure."""
38 plt.savefig(f"../figures/Fig1_{save_as}.pdf", dpi=600, bbox_inches='tight')
206def save(self, name):
207 self.fig.savefig(''.join((name, '.png')), dpi=300)
49def save_on_disk(plot):
50 plot.savefig("image.png")
173def export_figure(img, cmap='gray', output='tmp.png', vmin=0, vmax=1):
174 height, width = img.shape[:2]
175 fig = plt.figure()
176 fig.set_size_inches(width / height, 1, forward=False)
177 ax = plt.Axes(fig, [0., 0., 1., 1.])
178 ax.set_axis_off()
179 fig.add_axes(ax)
180 ax.imshow(img, cmap=cmap, vmin=vmin, vmax=vmax)
181 plt.savefig(output, dpi=height)
182 plt.close()
179def save_image(ax=None):
180 image_data = StringIO()
181 save_html(plt.gcf(), image_data)
182 image_data.seek(0)
183 return image_data

Related snippets