【发布时间】:2022-06-22 05:17:49
【问题描述】:
我正在尝试使用 python 为正弦函数创建一个滑块演示轮廓,但我不知道如何编写我们将为轮廓图滑块操作的线。我感谢任何帮助我的人。这是我的代码:
`import matplotlib.pyplot as plt`
`import numpy as np`
`from matplotlib.widgets import Slider, Button`
`plt.style.use('_mpl-gallery-nogrid')`
`# make data`
`t_1, t_2 = np.meshgrid(np.linspace(-2, 2, 256), np.linspace(-2, 2, 256))`
`def f(t_1, t_2, amplitude, frequency):`
`f = amplitude * np.sin(2 * np.pi * frequency * (t_1 + t_2))`
`return f`
`init_frequency = 3`
`init_amplitude = 3`
`axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])`
`freq_slider = Slider(`
`ax=axfreq,`
` label='Frequency [Hz]',`
`valmin=0.1,`
`valmax=30,`
`valinit=init_frequency,`
`)`
`# The function to be called anytime a slider's value changes`
`def update(val):`
`line.set_ydata(f(t_1, t_2, freq_slider.val))`
`fig.canvas.draw_idle()`
`# register the update function with each slider`
`freq_slider.on_changed(update)`
`# plot`
`fig, ax = plt.subplots()`
`plt.contourf(t_1, t_2, f(t_1, t_2, init_amplitude, init_frequency), 100, cmap='turbo')`
`#create the line that we will manipulate`
`#line, = plt.plot(t_1, t_2, f(t_1, t_2, init_amplitude, init_frequency), lw=2)`
`cbar=plt.colorbar(orientation='vertical');`
`cbar.set_label(label="E_c",size=15)`
`plt.show()`
【问题讨论】:
-
该代码格式看起来不太正确。您可以编辑您的帖子以使代码格式正确吗?
标签: python slider line contour demo