matplotlib - 如何在 jupyter notebook 中排列多个交互式图形?
问题描述
我尝试使用 ipywidget 和 matplotlib 在 jupyter 笔记本中排列多个图形,但它没有按我预期的那样工作......当我单独运行单元格时,它运行良好,但是当我重新运行整个笔记本时,没有任何显示。您可以瞥见我尝试做的事情,并通过找出我整个笔记本中的问题来帮助我。
单元格 1
import ipywidgets as widgets
import numpy as np
import matplotlib.pyplot as plt
%matplotlib widget
pi = np.pi
t = np.linspace(0,2*pi,100)
fig1 = plt.figure(1,figsize=(6,2))
ax1 = fig1.add_subplot(1, 1, 1, aspect=1)
def update_plot(phase,amplitude):
ax1.clear()
ax1.plot(t,amplitude*np.sin(t+phase), color='r', label='Sinus')
ax1.legend(loc=0)
phase = widgets.FloatSlider(min=-2,max=2,value=0,description='Phase:')
amplitude = widgets.FloatSlider(min=0,max=8,value=1.5,description='Amplitude:')
widgets.interactive(update_plot, phase=phase, amplitude=amplitude)
单元格2
fig2 = plt.figure(2,figsize=(6,2))
ax2 = fig2.add_subplot(1, 1, 1, aspect=1)
t2 = np.linspace(0,2*pi,100)
def update_plot(phase,amplitude):
ax2.clear()
ax2.plot(t2,amplitude*np.cos(t2+phase), color='b', label='Cosinus')
ax2.legend(loc=0)
phase = widgets.FloatSlider(min=-2,max=2,value=0,description='Phase:')
amplitude = widgets.FloatSlider(min=0,max=8,value=1,description='Amplitude:')
widgets.interactive(update_plot, phase=phase, amplitude=amplitude)
整本笔记本
我不知道如何共享笔记本,所以我将 ipynb 文件粘贴到 pastebin 中。 https://pastebin.com/UdK4jzHK
解决方案
要查看图表,您可以在每个图形单元格的末尾添加 fig1 fig2 等。
笔记本示例
PS:您可以随时在 Collab 上分享您的笔记本:https ://colab.research.google.com/
推荐阅读
- r - How plot new point in ggplot with older color data?
- android - Algolia 和 Firestore 价格 + 评论
- python - pandas plot 聚合时间戳索引
- nginx - 如何使用 metricbeat 检查我的服务器是否还活着,这可能吗?
- angularjs - ng-repeat : 添加新元素的 ng-model 问题
- swift - 如果存在多个有效的登录用户,则无法调用 RealmSwift currentUser
- asp.net-core - 发送客户端请求后如何执行操作?
- r - 根据上年和今年汇总
- c# - 通过特定时间检查字符串值
- python - 如何更改列表中的元素位置