【问题标题】:Bokeh and Matplotlib on the same Jupyter Notebook - Matplotlib not showing同一 Jupyter Notebook 上的散景和 Matplotlib - Matplotlib 未显示
【发布时间】:2021-06-29 17:42:18
【问题描述】:

我在同一个笔记本中运行带有 Bokeh 和 Matplotlib 的 Jupyter Notebook 时遇到了一个问题(Bokeh 用于交互性,Matplotlib 用于 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 绘图不会显示。我可以尝试什么?

【问题讨论】:

  • 嗯,适合我。

标签: python matplotlib bokeh


【解决方案1】:

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

【讨论】:

  • 点击您的链接时出现错误 404。我认为因此您的答案不会被接受。
  • 感谢您的告知。我已经编辑了它,现在它可以工作了。
  • 链接仍然无效。也许这个功能被阻止了。
猜你喜欢
  • 2017-05-11
  • 2020-12-12
  • 2016-12-30
  • 2023-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 2019-02-04
相关资源
最近更新 更多