Bokeh和Matplotlib在同一个Jupyter笔记本上-Matplotlip没有显示



我在运行Jupyter Notebook时遇到了一个问题,其中Bokeh和Matplotlib位于同一笔记本中(Bokeh用于交互,Matplotllib用于Bokeh不支持的特定绘图类型(。

一旦我执行了一个将运行Bokeh的单元格,那么Matplotlib将不再显示在输出单元格中。让我们考虑以下示例:

在第一个单元格中:

import yaml
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure
from bokeh.themes import Theme
from bokeh.io import show, output_notebook
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature
output_notebook()
def bkapp(doc):
df = sea_surface_temperature.copy()
source = ColumnDataSource(data=df)
plot = figure(x_axis_type='datetime', y_range=(0, 25),
y_axis_label='Temperature (Celsius)',
title="Sea Surface Temperature at 43.18, -70.43")
plot.line('time', 'temperature', source=source)
def callback(attr, old, new):
if new == 0:
data = df
else:
data = df.rolling('{0}D'.format(new)).mean()
source.data = ColumnDataSource.from_df(data)
slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days")
slider.on_change('value', callback)
doc.add_root(column(slider, plot))
doc.theme = Theme(json=yaml.load("""
attrs:
Figure:
background_fill_color: "#DDDDDD"
outline_line_color: white
toolbar_location: above
height: 500
width: 800
Grid:
grid_line_dash: [6, 4]
grid_line_color: white
""", Loader=yaml.FullLoader))
show(bkapp)

在第二个单元格中:

import matplotlib.pyplot as plt
import numpy as np
x = y = np.linspace(-2, 2, 100)
x, y = np.meshgrid(x, y)
z = np.cos(x**2 + y**2)
f = plt.figure()
ax = f.add_subplot(1, 1, 1)
ax.contourf(x, y, z)
plt.show()

如您所见,Matplotlib绘图将不会显示。我能尝试什么?

您的两个代码看起来都很好。你可以在这里试试它可能会起作用。

最新更新