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. Anything helps and thank you!
CodePudding user response:
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()