gpt4 book ai didi

python - 如何刷新 Matplotlib 中的文本?

转载 作者:太空狗 更新时间:2023-10-29 18:03:38 26 4
gpt4 key购买 nike

我编写这段代码是为了从 excel 文件中读取数据并绘制它们。对于某个 x 值,我想知道所有行的 y 值,因此我创建了一个 slider 来更改此 x 值,但我无法刷新打印 y 值的文本。

代码是这样的

import numpy as np
from openpyxl import load_workbook as ld
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

wb = ld(filename='example.xlsx')
data = wb['data']
time = wb['time']
row = data.max_row
column = data.max_column
x = np.ones((row, column))
y = np.ones((row, column))
result = np.ones(row)
for i in range(0, row):
for j in range(0, column):

x[i][j] = time.cell(row=i+1, column=j+1).value
y[i][j] = data.cell(row=i+1, column=j+1).value


fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
plt.plot(x[0], y[0], label='line1')
plt.plot(x[1], y[1], label='line2')
plt.plot(x[2], y[2], label='line3')
line, = plt.plot((np.amin(x), np.amin(x)), (np.amin(y), np.amax(y)))
plt.legend()
plt.grid(True)


axtime = plt.axes([0.25, 0.1, 0.65, 0.03])
stime = Slider(axtime, 'time', np.amin(x), np.amax(x), valinit=np.amin(x))


def y_text(r):
ax.text(10, 8, str(r), style='italic')


def find(t):
global x, y, result

for i in range(0, row):
for j in range(0, column):

if x[i][j] == t or (t < x[i][j] and j == 0) or (t > x[i][j] and j == column):
result[i] = y[i][j]
elif x[i][j] < t < x[i][j+1]:
result[i] = ((t-x[i][j])/(x[i][j+1]-x[i][j]))*(y[i][j+1]-y[i][j])+y[i][j]
return result


def update(val):
line.set_xdata(stime.val)
find(stime.val)
y_text(stime.val)
fig.canvas.draw()
stime.on_changed(update)


plt.show()

结果是这样的

enter image description here

如您所见,文本已被覆盖。

最佳答案

对于 matplotlib 小部件,如果您创建艺术家对象并调整其值(就像您在代码中使用 line 一样),则更新方法效果最佳。对于文本对象,您可以使用 set_textset_position 方法来更改它显示的内容和位置。例如,

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

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
sax = plt.axes([0.25, 0.1, 0.65, 0.03])

x = np.linspace(0,2.*np.pi,100)
f = np.sin(x)
l, = ax.plot(x, f)

slider1 = Slider(sax, 'amplitude', -0.8, 0.8, valinit=0.8)

tpos = int(0.25*x.shape[0])
t1 = ax.text(x[tpos], f[tpos], str(slider1.val))

tpos = int(0.75*x.shape[0])
t2 = ax.text(x[tpos], f[tpos], str(slider1.val))

def update(val):
f = slider1.val*np.sin(x)
l.set_ydata(f)

# update the value of the Text object
tpos = int(0.25*x.shape[0])
t1.set_position((x[tpos], f[tpos]))
t1.set_text(str(slider1.val))

tpos = int(0.75*x.shape[0])
t2.set_position((x[tpos], f[tpos]))
t2.set_text(str(slider1.val))

plt.draw()

slider1.on_changed(update)
plt.show()

看起来像,

enter image description here

另一种方法可能是每次都清除并重新绘制文本,但这样会更慢并且会更麻烦。

关于python - 如何刷新 Matplotlib 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39223286/

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