gpt4 book ai didi

python - 将 slider 添加到 matplotlib 图表

转载 作者:行者123 更新时间:2023-11-28 22:02:37 26 4
gpt4 key购买 nike

我有一个生成简单二维图表的 matplotlib 代码。我想为 hte 和 hre 变量(它们是数组)向它添加 slider 小部件,以便可以交互地增加或减少 hte 和 hre 值。有没有办法(我确定有,因为我在 matplotlib 网站上看到过这样的 something,但我无法将它与我的代码集成)?任何帮助将不胜感激。这是代码:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
from pylab import *

hte=np.array([10,11,12,13,15,20,21,22,25,30])
hre=np.array([1,2,3,4,5,6,7,8,9,10])

k=20*hte
n4=10*hre
t=6
w4=25

x=arange(1,100,10)
d=(log(x)/log(10))/10
y= k + n4 * (d) + t + w4 + 8

matplotlib.pyplot.jet()
lines=plot(x,y)
setp(lines, linewidth=2, color='r')
xlabel('X - Title')
ylabel('Y - title')
title('$Our Chart$')
grid(True)
show()

here is the chart that it generates

最佳答案

根据您的评论,我选择了 slider ,它们使数组中的值相乘。您应该应用您的特定算法。

from pylab import *
from matplotlib.widgets import Slider
import numpy as np

hte = np.array([10, 11, 12, 13, 15, 20, 21, 22, 25, 30])
hre = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

k = 20 * hte
n4 = 10 * hre
t, w4 = 6, 25
x = arange(1, 100, 10)
d = log10(x) / 10

y = k + n4 * d + t + w4 + 8

ax = subplot(111)
subplots_adjust(left=0.15, bottom=0.25)
line, = plot(x, y, linewidth=2, color='r')

xlabel('X - Title')
ylabel('Y - title')
title('$Our Chart$')
grid(True)

axcolor = 'lightgoldenrodyellow'
axhte = axes([0.15, 0.1, 0.65, 0.03], axisbg=axcolor)
axhre = axes([0.15, 0.15, 0.65, 0.03], axisbg=axcolor)

shte = Slider(axhte, 'hte', 0.1, 30.0, valinit=1)
shre = Slider(axhre, 'hre', 0.1, 10.0, valinit=1)

def update(val):
k = 20 * hte * shte.val
n4 = 10 * hre * shre.val

y= k + n4 * d + t + w4 + 8

line.set_ydata(y)
ax.set_ylim(y.min(), y.max())
draw()

shte.on_changed(update)
shre.on_changed(update)

show()

enter image description here

关于python - 将 slider 添加到 matplotlib 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11069944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com