我正在尝试使用滑块更改条形数以及条形大小。如果我们只打印滑块值的输出,滑块工作得很好。但是,当我尝试使用控制台崩溃时更改绘图。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
// I have a function to generate two list bins and bins_count
// which I use to plot bar chart.
// I am just writing the part in which I am having issue.
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.3)
pl = plt.bar(bins, bins_count, align = 'edge')
axcolor = 'lightgoldenrodyellow'
mzlowerslide = plt.axes([0.25, 0.02, 0.65, 0.03], facecolor=spcolor)
mzupperslide = plt.axes([0.25, 0.07, 0.65, 0.03], facecolor=spcolor)
binslide = plt.axes([0.25, 0.12, 0.65, 0.03], facecolor=spcolor)
low_mz = Slider(mzlowerslide, 'Lower Limit', 0, max(mass), valinit=lower_range)
high_mz = Slider(mzupperslide, 'Upper Limit', 0, max(mass), valinit=upper_range)
numbins = Slider(binslide, 'No. of Bins', 0, 100, valinit=binsize)
def update(val):
lower = int(low_mz.val)
upper = int(high_mz.val)
binn = int(numbins.val)
bins, bins_count = chart_values(mass, upper, lower, binn)
pl.set_xdata(bins)
pl.set_height(bins_count)
fig.canvas.draw_idle()
low_mz.on_changed(update)
high_mz.on_changed(update)
numbins.on_changed(update)
plt.show()
解
我添加了一个子图并在再次绘制之前清除该子图 代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
我有一个生成两个列表箱和bins_count的功能 我用来绘制条形图。 我只是在撰写我遇到问题的部分。
fig = plt.figure()
ax = fig.add_subplot (111)
fig.subplots_adjust(left=0.25, bottom=0.3)
ax.bar(bins, bins_count, align = 'edge')
axcolor = 'lightgoldenrodyellow'
mzlowerslide = plt.axes([0.25, 0.02, 0.65, 0.03], facecolor=spcolor)
mzupperslide = plt.axes([0.25, 0.07, 0.65, 0.03], facecolor=spcolor)
binslide = plt.axes([0.25, 0.12, 0.65, 0.03], facecolor=spcolor)
low_mz = Slider(mzlowerslide, 'Lower Limit', 0, max(mass), valinit=lower_range)
high_mz = Slider(mzupperslide, 'Upper Limit', 0, max(mass), valinit=upper_range)
numbins = Slider(binslide, 'No. of Bins', 0, 100, valinit=binsize)
def update(val):
lower = int(low_mz.val)
upper = int(high_mz.val)
binn = int(numbins.val)
bins, bins_count = chart_values(mass, upper, lower, binn)
ax.clear ()
ax.bar (bins, bins_count, align = 'edge')
fig.canvas.draw()
low_mz.on_changed(update)
high_mz.on_changed(update)
numbins.on_changed(update)
plt.show()