When trying to save plot image created with 'pandas.DataFrame.plot' from ' pandas.core.series.Series' object :
%matplotlib inline type(class_counts) # pandas.core.series.Series class_counts.plot(kind='bar', figsize=(20, 16), fontsize=26)
Like this:
import matplotlib.pyplot as plt plt.savefig('figure_1.pdf', dpi=300)
results in empty pdf file. How to save image created with 'pandas.DataFrame.plot'?
To save a pandas DataFrame table as a png with Python, we can use the data frame export method in the dataframe_image package. Then we call df. style. background_gradient to style the data frame table with a background gradient.
Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.
Pass your normal or styled DataFrame to the export function along with a file location to save it as an image. You may also export directly from the DataFrame or styled DataFrame using the dfi. export and export_png methods, respectively. Here, an example of how exporting a DataFrame would look like in a notebook.
Try this :
fig = class_counts.plot(kind='bar', figsize=(20, 16), fontsize=26).get_figure() fig.savefig('test.pdf')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With