Issue
At first, I have to say that I'm a true beginner in Python (and in programming itself) so this may be a silly question but I couldn't find a solution.
I load data from 3 different .csv files then make some calculations and in the end i want to save 3 heatmaps. My problem is with saving. When i use plt.savefig('whatever.jpg')
it obviously saves only last heatmap in the loop. I tried to change it to plt.savefig('{}.jpg'.format(i))
but I got an error and I don't know how to fix. I'll appreciate any hints! (Python 3 from Anaconda)
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
instrumenty = ['gold','sp500','dax']
for i in instrumenty:
i = pd.read_csv(i+'_m.csv', sep=',')
i['Miesiąc'] = pd.DatetimeIndex(i['Data']).month
i['Rok'] = pd.DatetimeIndex(i['Data']).year
i['Zmiana']=i['Zamkniecie'].pct_change()
i_pt = i[1:].pivot_table(index='Rok',columns='Miesiąc',values='Zmiana')
sns.heatmap(i_pt, linewidths=1, cmap='PiYG')
plt.savefig('{}.png'.format(i))
plt.clf()
And the error:
FileNotFoundError Traceback (most recent call last)
<ipython-input-30-5729b6a0480f> in <module>()
14 i_pt = i[1:].pivot_table(index='Rok',columns='Miesiąc',values='Zmiana')
15 sns.heatmap(i_pt, linewidths=1, cmap='PiYG')
---> 16 plt.savefig('{}.png'.format(i))
17 plt.clf()
18
C:\Users\user\Anaconda3\lib\site-packages\matplotlib\pyplot.py in savefig(*args, **kwargs)
694 def savefig(*args, **kwargs):
695 fig = gcf()
--> 696 res = fig.savefig(*args, **kwargs)
697 fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
698 return res
C:\Users\user\Anaconda3\lib\site-packages\matplotlib\figure.py in savefig(self, *args, **kwargs)
1561 self.set_frameon(frameon)
1562
-> 1563 self.canvas.print_figure(*args, **kwargs)
1564
1565 if frameon:
C:\Users\user\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2230 orientation=orientation,
2231 bbox_inches_restore=_bbox_inches_restore,
-> 2232 **kwargs)
2233 finally:
2234 if bbox_inches and restore_bbox:
C:\Users\user\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
530 renderer.dpi = self.figure.dpi
531 if is_string_like(filename_or_obj):
--> 532 filename_or_obj = open(filename_or_obj, 'wb')
533 close = True
534 else:
Even without plt.savefig i am doing something wrong because the result is one strange chart with 3 legends instead of 3 charts.
Solution
You override i
here:
instrumenty = ['gold','sp500','dax']
for i in instrumenty:
i = pd.read_csv(i+'_m.csv', sep=',')
Better use name
:
for name in instrumenty:
# use `name`
i = pd.read_csv(name +'_m.csv', sep=',')
i['Miesiąc'] = pd.DatetimeIndex(i['Data']).month
i['Rok'] = pd.DatetimeIndex(i['Data']).year
i['Zmiana']=i['Zamkniecie'].pct_change()
i_pt = i[1:].pivot_table(index='Rok',columns='Miesiąc',values='Zmiana')
sns.heatmap(i_pt, linewidths=1, cmap='PiYG')
plt.savefig('{}.png'.format(name))
plt.clf()
This is the important part:
plt.savefig('{}.png'.format(name))
plt.clf()
Answered By - Mike Müller
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.