我今天一直在阅读有关如何在 Django 模板中渲染 matplotlib.pyplot 的内容。
我找到了 Bokeh 库,我试图将我的 matplotib 在有效输入中转换为 Bokeh 组件。我读到 .to_boke 方法是 已弃用 .
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})
我有一个 值错误 (显然 matplotlib.pyplot 不是有效输入):
![enter image description here](https://i.sstatic.net/QRS0v.png)
我在这里堆。这是我第一次使用图书馆和 matplot。
我很感激任何帮助。非常感谢。
PS:我编码的数字,我正在尝试打印:
![enter image description here](https://i.sstatic.net/4FFpj.png)
最佳答案
您要求的内容不受支持且不存在。在 Bokeh 或 Matplotlib 中,没有将 Matplotlib 输出转换为 Bokeh 输出的特性或功能。因此,这个问题的答案是:
What you are asking for is not possible.
(作为 Bokeh 的共同创造者和主要维护者发言)重要的是,用户必须清楚而明确地了解将 MPL 转换为 Bokeh 没有“ Elixir ”。其他是 错误信息 )。
生成 Bokeh 输出的唯一选择是直接使用 native Bokeh API,例如
bokeh.plotting
API。特别是,您可能想查看 wedge glyph ,但请注意,从 1.2.0
开始, Bokeh 没有任何内置的径向轴,因此您必须“手动”绘制所有轴元素和标签。
关于django - 如何将 matplotlib.pyplot 转换为 Bokeh 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44423704/