Mpld3: fig_to_html์˜ ๋ฒ„๊ทธ, TypeError : array ([1.])๋Š” JSON ์ง๋ ฌํ™” ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2017๋…„ 12์›” 05์ผ  ยท  8์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: mpld3/mpld3

Mac์—์„œ๋งŒ imshow๋กœ ์ƒ์„ฑ ๋œ 2D ์ด๋ฏธ์ง€์— ์ปฌ๋Ÿฌ ๋ฐ”๋ฅผ ์ถ”๊ฐ€ ํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

mpld3 .__ version __ = '0.3.1.dev1'(์ด์ „ ๋ฒ„์ „์—์„œ๋„ ์‹คํŒจ)

matplotlib .__ version __ = '2.1.0'

import matplotlib.pyplot as plt, mpld3 import numpy as np fig,ax=plt.subplots(1,1) im=ax.imshow(np.zeros((100,100))) fig.colorbar(im, ax=ax) mpld3.fig_to_html(fig)

bug possibly fixed

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ˆ˜์ • ์‚ฌํ•ญ์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. Lib \ site-packages \ mpld3์—์žˆ๋Š” _display.py ํŒŒ์ผ์„ ํŽธ์ง‘ํ•˜๊ณ  NumpyEncoder ํด๋ž˜์Šค๋ฅผ ๋‹ค์Œ์œผ๋กœ ๋Œ€์ฒด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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)
        try: # Added by ceprio 2018-04-25
            iterable = iter(obj)
        except TypeError:
            pass
        else:
            return list(iterable)
         # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj)

๋ชจ๋“  8 ๋Œ“๊ธ€

  • ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ๋‹น์‹ ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

๋‚˜๋Š” ๋˜ํ•œ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. Python 2.7.4, mpld3 v0.2 ๋ฐ matplotlib v2.1.2๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋ฌธ์ œ๊ฐ€ matplotlib์— ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ตœ์‹  ๋ฒ„์ „์˜ matplotlib์™€ ํ˜ธํ™˜๋˜๋„๋ก mpld3๋ฅผ ์—…๋ฐ์ดํŠธ ํ•  ๊ณ„ํš์ด ์žˆ์Šต๋‹ˆ๊นŒ?

์ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ˆ˜์ • ์‚ฌํ•ญ์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. Lib \ site-packages \ mpld3์—์žˆ๋Š” _display.py ํŒŒ์ผ์„ ํŽธ์ง‘ํ•˜๊ณ  NumpyEncoder ํด๋ž˜์Šค๋ฅผ ๋‹ค์Œ์œผ๋กœ ๋Œ€์ฒด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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)
        try: # Added by ceprio 2018-04-25
            iterable = iter(obj)
        except TypeError:
            pass
        else:
            return list(iterable)
         # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj)

matplotlib ๋ฒ„์ „ 2.2.3์˜ ๊ฒฝ์šฐ ์„ ํญ์„ numpy ๋ฐฐ์—ด์—์„œ mpld3.mpld3renderer.MPLD3Renderer.draw_path_collection์˜ ๋ชฉ๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜์Šต๋‹ˆ๋‹ค.

์ตœ์‹  ๋ฒ„์ „์˜ ์—ฌ์ „ํžˆ ๋ฌธ์ œ

@jonashaag @ceprio ๋‹˜์ด 2018 ๋…„ 4 ์›” 26 ์ผ ์œ„์— ๋Œ“๊ธ€์„ ๋‹ฌ์•˜

์•ˆ๋…•ํ•˜์„ธ์š” @jonashaag ๋ฐ @JupyterJones , https://github.com/sciris/mpld3 ์—์„œ๋„ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜

@ceprio ์˜ ์ฝ”๋ฉ˜ํŠธ๋„ ์ €์—๊ฒŒ

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰

๊ด€๋ จ ๋ฌธ์ œ

pranet picture pranet  ยท  6์ฝ”๋ฉ˜ํŠธ

jakevdp picture jakevdp  ยท  4์ฝ”๋ฉ˜ํŠธ

edvakf picture edvakf  ยท  29์ฝ”๋ฉ˜ํŠธ

ajasja picture ajasja  ยท  17์ฝ”๋ฉ˜ํŠธ

sheldonpark picture sheldonpark  ยท  7์ฝ”๋ฉ˜ํŠธ