Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a matplotlib.pyplot to a bokeh plot

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):

enter image description here

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:

enter image description here

like image 703
Hugo L.M Avatar asked Jun 07 '17 22:06

Hugo L.M


People also ask

Is bokeh based on matplotlib?

Matplotlib, seaborn, ggplot, and Pandas¶Uses bokeh to display a Matplotlib Figure. You can store a bokeh plot in a standalone HTML file, as a document in a Bokeh plot server, or embedded directly into an IPython Notebook output cell.

How do you save a Pyplot as an image?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

How do I save a Pyplot as a PNG?

To save plot figure as JPG or PNG file, call savefig() function on matplotlib. pyplot object. Pass the file name along with extension, as string argument, to savefig() function.

What is the difference between matplotlib and Pyplot?

The matplotlib. pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.


1 Answers

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".

like image 63
bigreddot Avatar answered Nov 15 '22 07:11

bigreddot