Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas Data Reader figure not saving as image

I have successfully created a chart with stock info I am pulling from Yahoos API, but I am running into troubles when attempting to save said figure as a png.
For some reason, when I try to save the figure, it saves as just a blank png.
This is the code I am using on VS Code and Windows 10.

#Import Modules
import pandas as pd
import datetime
import pandas_datareader as pdr
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import style
import matplotlib as mpl
import plotly.express as px
#Setting Variables
start  = datetime.datetime(2010,12,31)
end = datetime.datetime(2022,9,20)


fnT = pdr.DataReader(['AMD','NVDA','INTC','TSM'],
                    'yahoo',
                    start = start,
                    end = end)['Adj Close']
#Graph 1
fnT = fnT.plot(figsize=(12,6))
plt.show()
plt.savefig('fnPlot1.png')

This code generates this image, but when it attempts to save, it just becomes blank.
There is no error message.
enter image description here
Anything helps and thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use pyplot.savefig before pyplot.show. At the end of (a blocking) show() the figure is closed and thus unregistered from pyplot. Calling pyplot.savefig afterwards would save a new and thus empty figure.

plt.savefig('fnPlot1.png')
plt.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading