ํจ์น ์ปฌ๋ ์
์ ์ผ๋ถ ํจ์น ํ๋กฏ์ผ๋ก .html์ ๋ง๋ค๊ณ ์ถ์ต๋๋ค.
" https://matplotlib.org/examples/api/patch_collection.html "์์ ์์ ๋ฅผ ์คํํ๊ณ
๋๊ตฐ๊ฐ ๋์ ์ฃผ์๊ฒ ์ต๋๊น?
๊ฐ์ฌ,
์ด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ญ ์ถ์ (๊ฐ์ฅ ์ต๊ทผ ํธ์ถ ๋ง์ง๋ง) :
ํ์ผ "ttt.py", ์ค 55,
htmlfil.write (mpld3.fig_to_html (๊ทธ๋ฆผ))
ํ์ผ "/usr/local/lib/python2.7/dist-packages/mpld3/_display.py", ์ค 251, fig_to_html
figure_json = json.dumps (figure_json, cls = NumpyEncoder),
๋คํ์์๋ ํ์ผ "/usr/lib/python2.7/json/__init__.py", 250 ํ
sort_keys = sort_keys, ** kw) .encode (obj)
ํ์ผ "/usr/lib/python2.7/json/encoder.py", 207 ํ, ์ธ์ฝ๋ฉ
์ฒญํฌ = self.iterencode (o, _one_shot = True)
iterencode์ ํ์ผ "/usr/lib/python2.7/json/encoder.py", 270 ํ
return _iterencode (o, 0)
ํ์ผ "/usr/local/lib/python2.7/dist-packages/mpld3/_display.py", ๋ผ์ธ 138, ๊ธฐ๋ณธ๊ฐ
return json.JSONEncoder.default (self, obj)
ํ์ผ "/usr/lib/python2.7/json/encoder.py", 184 ํ, ๊ธฐ๋ณธ๊ฐ
๋ฐ์ TypeError (repr (o) + "is not JSON serializable")
TypeError : array ([1.])๋ JSON ์ง๋ ฌํ ๊ฐ๋ฅํ์ง ์์ต๋๋ค.
๋น์ทํ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. matplotlib.collections.pathcollection
๋์
๋๋ฆฌ๋ฅผ ์ถฉ๋ถํ ์ญ์ ํ์ง ์์ ๋ง์ ๋งค๊ฐ ๋ณ์๊ฐ numpy ์ธ์คํด์ค ํํ๋ก ๋จ์์ต๋๋ค. ํ์ฌ mpld3๋ ์ด๋ฌํ numpy ์ธ์คํด์ค๋ฅผ ์ง๋ ฌํํ๊ธฐ ์ํด "NumpyEncoder"์ ์์กดํ๋ฉฐ array
์ธ์คํด์ค๊ฐ ๋์ด๋์ง ์์ผ๋ฉด ์ธ์ฝ๋๊ฐ ๋ถํํฉ๋๋ค.
๊ณ ์น๋ค:
_display.py
class NumpyEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
def default(self, obj):
if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,
numpy.int16, numpy.int32, numpy.int64, numpy.uint8,
numpy.uint16,numpy.uint32, numpy.uint64)):
return int(obj)
elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32,
numpy.float64)):
return float(obj)
elif isinstance(obj,(numpy.ndarray,)): #### This is the fix
return obj.tolist()
return json.JSONEncoder.default(self, obj)
๊ฐ์ฌํฉ๋๋ค! ๊ทํ์ ์์ ์ผ๋ก ๋ด save_html ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค. ์ฐ์ ๋๋ฅผ ์ ์ฅํ๋ ค๊ณ ํ ๋ "๋ฐฐ์ด์ด JSON ์ง๋ ฌํ ๊ฐ๋ฅํ html์ด ์๋๋๋ค"๋ผ๋ ๋ฉ์์ง๊ฐ ๋ํ๋ฉ๋๋ค.
์ด ๋ฌธ์ ๊ฐ ์ต์ ๋ฆด๋ฆฌ์ค์์ ํด๊ฒฐ๋์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค. ์ต์ ๋ฒ์ ์ด ์๊ณ ์๋์ผ๋ก ์์ ํด์ผํ์ต๋๋ค. ์ด์จ๋ ๊ฐ์ฌํฉ๋๋ค @shouldsee !
๋๊ตฐ๊ฐ ์์ฑํ๋ฉด ํ ๋ฆฌํ์คํธ๋ฅผ ๋ณํฉํ๊ฒ ์ต๋๋ค. ์๋ ํ ์คํธ๊ฐ ๋ชจ๋ ํต๊ณผํ๋ฉด ๋ณด๋์ค ํฌ์ธํธ!
์ ์ ๋ ์์ ์ฌํญ๋ ๋ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค.
javadba๋ ์ปค๋ฐ์์ PR์ ๋ง๋ค์ง ์์์ผ๋ฏ๋ก ๋ณต์ ํ์ต๋๋ค.
์ด๊ฒ์ ์ฌ์ ํ โโ๋ฌธ์ ์
๋๋ค
matplotlib==2.2.2
mpld3==0.3
๋ด ํ๋ก๊ทธ๋จ์์ mpld3._display.NumpyEncoder๋ฅผ ๋ฎ์ด ์จ์ ์๋์ผ๋ก ์์ ํฉ๋๋ค.
#mpld3 hack
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
import numpy as np
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
from mpld3 import _display
_display.NumpyEncoder = NumpyEncoder
์์ง ์ด์ ๋ํ ํ๋ก๋์ ์์ ์ด ์์ต๋๊น? Python ํ๋ก์ ํธ์ mpld3๋ฅผ ๋์ ํ๋๋ฐ ๋์ผํ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ๊ฑด๋ฐฐ!
์ด ์์ ์ master์์ ํด๊ฒฐํด์ผํฉ๋๋ค. ๊ฑฐ์ 2 ๋ ๋์ mpl3d๊ฐ ์ถ์๋์ง ์์์ต๋๋ค. ๋๋ ๊ทธ๊ฒ์ด ์ ๊ทน์ ์ผ๋ก ์ ์ง๋์ง ์๊ณ ์๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ๊ฒ์ด ์ธ์ / ์์์ง ๋ชจ๋ฅด๊ฒ ์ต๋๋ค ...
mpld3 ๋ง์คํฐ ๋ธ๋์น๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด
๋ค์๊ณผ ๊ฐ์ด ํ ์ ์์ต๋๋ค.
pip install git+git://github.com/mpld3/mpld3@master#egg=mpld3
๋๋ ์ถ๊ฐ
git+git://github.com/mpld3/mpld3@master#egg=mpld3
์ requirements.txt
ํ์ผ์ ์ถ๊ฐ
@aflaxman @jakevdp ์๋ง๋ ํ๋ก์ ํธ๊ฐ ์ ์ง๋์ง ์์์ ๋ ๋ช ํํ๊ฒ ๋ํ ๋ด๊ธฐ ์ํด์ด ์ ์ฅ์๋ฅผ ๋ณด๊ดํ๋ ๊ฒ์ ๊ณ ๋ คํ ๊ฐ์น๊ฐ์์ ๊ฒ์ ๋๋ค.
https://help.github.com/articles/archiving-a-github-repository/
๋๋ bokeh/bkcharts
์ด๊ฒ์ํ๋ค
@cliffckerr ์ ํ์ฌ๋์ด ํ๋ก์ ํธ๋ฅผ ๋ถํ์ํฌ ๊ณํ์ ๋๋ค! ๊ทธ๊ฒ์ด ๋ฐ๋์๋ค๋ฉด ์์นด์ด๋น์ด ์ข์ ์๊ฐ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค.
๋๋ mpld3 + Django์ ๋ ์ ๋์ณค์ต๋๋ค. ๋ด๊ฐ ์ด๋ค ๋์์ ์ค ์ ์์์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง ์ด๊ฑธ ์ด๋ ค ๋์ธ์!
๋ด iPhone์์ ๋ณด๋
2018 ๋ 7 ์ 14 ์ผ 00:01์ Abraham Flaxman < [email protected] [email protected] >์ด ์์ฑํ์ต๋๋ค.
@cliffckerr https://github.com/cliffckerr ์ ํ์ฌ๋์ด ํ๋ก์ ํธ๋ฅผ ๋ถํ์ํฌ ๊ณํ์ ๋๋ค! ๊ทธ๊ฒ์ด ๋ฐ๋์๋ค๋ฉด ์์นด์ด๋น์ด ์ข์ ์๊ฐ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค.
โ
๋น์ ์ด ์ธ๊ธ ๋์๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ ๋ฐ๊ณ ์์ต๋๋ค.
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub https://github.com/mpld3/mpld3/issues/434#issuecomment-404974279 ์์ ๋ณด๊ฑฐ๋ https://github.com/notifications/unsubscribe-auth/AHqXbA3CO- ์ค๋ ๋๋ฅผ ์์๊ฑฐํฉ๋๋ค.
๋ชจ๋ ๊ฐ์ฌํฉ๋๋ค. ๋๋ ์ฝ๋๊ฐ ์ ์ง๋์ง ์๋๋ค๋ ๊ฒ์ ๋ชฐ๋๋ค. ๋ด ํ์์ ์๋ฒฝํ๊ฒ ๋ค๋ ธ๋ค.
๊ฑด๋ฐฐ, ๋งํฌ.
@addinall @aflaxman @bryevdv ์ฐ๋ฆฌ๋ ์ค์ ๋ก ๊ทธ๊ฒ์ ๋ ์ด๋ฆฌ๊ณ ์์ต๋๋ค! ์ฐ๋ฆฌ๋ ํ์ฌ ํฌํฌ (https://github.com/optimamodel/mpld3)์์ ์์ ์ค์ด์ง๋ง ๋ชจ๋ ๊ฒ์ด ์์ ๋๋ฉด ์ฌ๊ธฐ๋ก ๋ค์ ๋ณํฉ ํ ๊ฒ์ ๋๋ค.
๐
fig_to_dict ()์์๋ ๋น์ทํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋ฐํ ๋ dict๋ ์ฌ๋ฐ๋ฅธ ์ธ์ฝ๋๊ฐ ์ ๋ฌ๋์ง ์์ผ๋ฉด ํฌํจ ๋ ndarray๋ก ์ธํด json์ผ๋ก ์ง๋ ฌํ ๋ ์ ์์ต๋๋ค. ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ํ ๊ฐ์ง ๋ฐฉ๋ฒ์ ๋ด๋ถ NumpyEncoder ํด๋์ค ์์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค. ์๋ฅผ ๋ค์ด.
from mpld3._display import NumpyEncoder
json.dumps(mpld3.fig_to_dict(fig), cls=NumpyEncoder)
์ด๊ฒ์ ์ฌ์ ํ โโ๋ฌธ์ ์ ๋๋ค
matplotlib==2.2.2 mpld3==0.3
๋ด ํ๋ก๊ทธ๋จ์์ mpld3._display.NumpyEncoder๋ฅผ ๋ฎ์ด ์จ์ ์๋์ผ๋ก ์์ ํฉ๋๋ค.
#mpld3 hack class NumpyEncoder(json.JSONEncoder): def default(self, obj): import numpy as np if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) from mpld3 import _display _display.NumpyEncoder = NumpyEncoder
์ด๊ฒ์ ์ฌ์ ํ โโ๋ฌธ์ ์ ๋๋ค
matplotlib==2.2.2 mpld3==0.3
๋ด ํ๋ก๊ทธ๋จ์์ mpld3._display.NumpyEncoder๋ฅผ ๋ฎ์ด ์จ์ ์๋์ผ๋ก ์์ ํฉ๋๋ค.
#mpld3 hack class NumpyEncoder(json.JSONEncoder): def default(self, obj): import numpy as np if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) from mpld3 import _display _display.NumpyEncoder = NumpyEncoder
๋๋จํ ๊ฐ์ฌํฉ๋๋ค!
๋น์ ์ ์ฒ์ฌ์
๋๋ค!
์ ๋ ํ์ด์ฌ์ ๋ํด ๊ฑฐ์ ์์ง ๋ชปํด์ ์ง์ ๊ณ ์น ์ ์์๊ธฐ ๋๋ฌธ์ ์๊ณ ์์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋น์ทํ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.
matplotlib.collections.pathcollection
๋์ ๋๋ฆฌ๋ฅผ ์ถฉ๋ถํ ์ญ์ ํ์ง ์์ ๋ง์ ๋งค๊ฐ ๋ณ์๊ฐ numpy ์ธ์คํด์ค ํํ๋ก ๋จ์์ต๋๋ค. ํ์ฌ mpld3๋ ์ด๋ฌํ numpy ์ธ์คํด์ค๋ฅผ ์ง๋ ฌํํ๊ธฐ ์ํด "NumpyEncoder"์ ์์กดํ๋ฉฐarray
์ธ์คํด์ค๊ฐ ๋์ด๋์ง ์์ผ๋ฉด ์ธ์ฝ๋๊ฐ ๋ถํํฉ๋๋ค.๊ณ ์น๋ค:
_display.py