How to convert a matplotlib.pyplot to a bokeh plot

与世无争的帅哥 提交于 2019-12-06 07:55:06

问题


I have been reading today about how to render a matplotlib.pyplot in a Django template.

I found bokeh library and I was trying to convert my matplotib in a valid input to bokeh components. I read .to_boke method is deprecated.

        datos = np.random.randn(1000)
        ## Discretizamos el conjunto de valores en n intervalos,
        ## en este caso 8 intervalos
        datosbin = np.histogram(datos,
                                bins=np.linspace(np.min(datos), np.max(datos), 9))[0]
        ## Los datos los queremos en tanto por ciento
        datosbin = datosbin * 100. / len(datos)
        ## Los datos los queremos en n direcciones/secciones/sectores,
        ## en este caso usamos 8 sectores de una circunferencia
        sect = np.array([90, 45, 0, 315, 270, 225, 180, 135]) * 2. * math.pi / 360.
        nombresect = ['E', 'NE', 'N', 'NW', 'W', 'SW', 'S', 'SE']
        ## Dibujamos la rosa de frecuencias
        plt.axes([0.1, 0.1, 0.8, 0.8], polar=True)
        plt.bar(sect, datosbin, align='center', width=45 * 2 * math.pi / 360.,
                facecolor='b', edgecolor='k', linewidth=2, alpha=0.5)
        plt.thetagrids(np.arange(0, 360, 45), nombresect, frac=1.1, fontsize=10)
        plt.title(u'Procedencia de las nubes en marzo')
        script, div = components(plt, CDN)
        return render(request, 'consulta/resultado/imprimir.html', {'variables': variables,
                                                                    'respuesta3': peticion3.content,
                                                                    'lugar': lugar,
                                                                    'hora_actual': hora_actual,
                                                                    'hora_siguiente': hora_siguiente,
                                                                    'dias': horas,
                                                                    'Variables': variables_posibles,
                                                                    'latitud':latitud,
                                                                    'longitud': longitud,
                                                                    "the_script": script,
                                                                    "the_div": div})

I have a valueError (obviously matplotlib.pyplot is not a valid input):

I'm stack here. It's my first time with the library and matplot.

I appreciate any help. Thank you so much.

PS: the figure I have coded and I'm trying to print:


回答1:


What you are asking for is not supported and does not exist. There is no feature or function, in either Bokeh or Matplotlib, that will convert Matplotlib output to Bokeh output. Therefore, the answer to this question is:

What you are asking for is not possible.

(Speaking as the co-creator and lead maintainer of Bokeh) It is important for users to clearly and unambiguously understand that there is no "magic bullet" to convert MPL to Bokeh. Anything else is misinformation).

The only option for generating Bokeh output is to use native Bokeh APIs directly, e.g. the bokeh.plotting API. In particular, you might want to look at the wedge glyph, however be advised that as of 1.2.0, Bokeh does not have any built-in radial axis, so you would have to draw all the axis elements and labels "by hand".



来源:https://stackoverflow.com/questions/44423704/how-to-convert-a-matplotlib-pyplot-to-a-bokeh-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!