Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
fix matplotlib crash
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Dec 6, 2014
1 parent 0f1744b commit aae4b82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion _unittests/ut_datasource/test_data_velib_offline.py
Expand Up @@ -61,7 +61,8 @@ def test_data_velib_animation(self) :
anime = DataVelibCollect.js_animation(df)

from JSAnimation import HTMLWriter
anime.save(os.path.join(fold,"out_animation.html"), writer=HTMLWriter(embed_frames=False))
wr = HTMLWriter(embed_frames=False)
anime.save(os.path.join(fold,"out_animation.html"), writer=wr)


if __name__ == "__main__" :
Expand Down
9 changes: 7 additions & 2 deletions _unittests/ut_finance/test_stock_graph.py
Expand Up @@ -37,26 +37,31 @@ def test_graph(self) :
]

if True:
fLOG("A", sys.executable)
fig, ax, plt = StockPrices.draw(stocks, figsize=(16,8), field = ["Adj Close", "Close"])
img = os.path.abspath(os.path.join(os.path.split(__file__)[0],"temp_image.png"))
if os.path.exists(img): os.remove(img)
fig.savefig(img)
assert os.path.exists(img)

if True and sys.version_info < (3,4) :
if True :
fLOG("B")
fig, ax, plt = StockPrices.draw(stocks, begin="2010-01-01")
img = os.path.abspath(os.path.join(os.path.split(__file__)[0],"temp_image2.png"))
if os.path.exists(img): os .remove(img)
fig.savefig(img)
assert os.path.exists(img)

if True and sys.version_info < (3,4):
if True :
fLOG("C")
fig, ax, plt = StockPrices.draw(stocks[:1], begin="2010-01-01")
img = os.path.abspath(os.path.join(os.path.split(__file__)[0],"temp_image3.png"))
if os.path.exists(img): os .remove(img)
fig.savefig(img)
assert os.path.exists(img)

fLOG("thisend")


if __name__ == "__main__" :
unittest.main ()
10 changes: 8 additions & 2 deletions src/pyensae/finance/astock.py
Expand Up @@ -4,8 +4,7 @@
"""
import os, urllib.request, urllib.error, datetime
import pandas, numpy
import matplotlib.pyplot as plt
import matplotlib.dates as mdates



class StockPrices:
Expand Down Expand Up @@ -439,6 +438,10 @@ def draw(listStockPrices, begin = None, end = None,

def price(x): return '%1.2f'%x

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

if existing is not None:
if not isinstance(existing,list) and not isinstance(existing,tuple):
raise Exception("existing must be a list or a tuple")
Expand Down Expand Up @@ -505,6 +508,9 @@ def price(x): return '%1.2f'%x
else :
ax.grid(True)
ax.legend( ex_l + tuple(data.columns))

# avoid matplotlib to crash later
plt.close('all')
return fig, ax, plt

def to_csv(self, filename, sep = "\t", index=False, **params):
Expand Down

0 comments on commit aae4b82

Please sign in to comment.