Search code examples
pythonpandasimagematplotlibcapture

How to capture a plot into an image


I read and tried the answers from this question. However, it saves a picture in the given path and I can open it, but it is all white and I do not know how to save it properly. Mention: Initially the plot is made properly.

My code is:

import pandas as ps
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
%matplotlib inline

plt.figure(figsize=(10,8))
x = df.index
y = df.val
plt.plot(x, y, c='b',label='quality')

plt.xlabel('datetime')
plt.ylabel('values')
plt.legend(loc='best')
plt.title('Qualities')
plt.show()

plt.savefig('foo.png')

How can I make this work?


Solution

  • Try to write plt.show() at last , ie. after plt.savefig().