Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib窗口内回归线的输出值_Python_Matplotlib_Textbox_Regression - Fatal编程技术网

Python Matplotlib窗口内回归线的输出值

Python Matplotlib窗口内回归线的输出值,python,matplotlib,textbox,regression,Python,Matplotlib,Textbox,Regression,我有一个工作代码,它根据我的输入生成一个带有错误条和回归线的图形。没关系。现在我想做的是在下面添加一个文本框,一旦用户输入一个数字,例如12,它就会输出相应的值(重新回归线) 我用这种方法试过。虽然不成功:但我无法让它获取值并将其输出到matplotlib提供的GUI界面上。每个输入都需要检查该字段。Matplotlib在文本更改(self,func)或提交(self,func)时提供,因此这可能有效-但如何输出? 有人有什么想法吗?我会用一个简单的文本艺术家来显示结果。但是,出于想象,我还会在

我有一个工作代码,它根据我的输入生成一个带有错误条和回归线的图形。没关系。现在我想做的是在下面添加一个文本框,一旦用户输入一个数字,例如12,它就会输出相应的值(重新回归线)

我用这种方法试过。虽然不成功:但我无法让它获取值并将其输出到matplotlib提供的GUI界面上。每个输入都需要检查该字段。Matplotlib在文本更改(self,func)或提交(self,func)时提供
,因此这可能有效-但如何输出?

有人有什么想法吗?

我会用一个简单的
文本
艺术家来显示结果。但是,出于想象,我还会在图形上显示线,显示输入和输出值

left, bottom, width, height = 0.15, 0.02, 0.7, 0.10

plt.subplots_adjust(left=left, bottom=0.25) # Make space for the slider


input_field = plt.axes([left, bottom, width, height])
box = TextBox(input_field, 'value')

很显然,您只能输入一次值-无法识别更改。此外,(如果可以重新输入值字段),图形中的正方形会保留还是会被删除?除此之外,还可以输入y值并获得相应的x值?很抱歉,忘记标记您无法复制。我可以用上面的代码输入多个值,那么把它改为输入y值来获得x值输出怎么样?你知道怎么做吗
left, bottom, width, height = 0.15, 0.02, 0.7, 0.10

plt.subplots_adjust(left=left, bottom=0.25) # Make space for the slider


input_field = plt.axes([left, bottom, width, height])
box = TextBox(input_field, 'value')
import matplotlib.pyplot as plt
import numpy as np

x = np.array([6, 15, 24, 33, 41, 52, 59, 66, 73, 81])
y = np.array([5, 10, 15, 20, 25, 30, 35, 40, 45, 50])

coef = np.polyfit(x, y, 1)
poly1d_fn = np.poly1d(coef)  # to create a linear function with coefficients


def submit(val):
    try:
        x = float(val)
        y = poly1d_fn(x)
        ax.annotate('', xy=(x,0), xycoords=('data','axes fraction'),
                        xytext=(x,y), textcoords='data',
                    arrowprops=dict(arrowstyle='-', ls='--'))
        ax.annotate(f'{x:.2f}', xy=(x,0), xycoords=('data','axes fraction'))
        ax.annotate('', xy=(0,y), xycoords=('axes fraction','data'),
                        xytext=(x,y), textcoords='data',
                    arrowprops=dict(arrowstyle='-', ls='--'))
        ax.annotate(f'{y:.2f}', xy=(0,y), xycoords=('axes fraction','data'))
        output_box.set_text(f'Result = {y:.2f}')
        plt.draw()
    except ValueError:
        pass

fig, ax = plt.subplots()
ax.plot(x, y, 'ro', x, poly1d_fn(x), '-b')
ax.errorbar(x, poly1d_fn(x), yerr=poly1d_fn(x) - y, fmt='.k')

left, bottom, width, height, pad = 0.15, 0.02, 0.3, 0.10, 0.1
fig.subplots_adjust(left=left, bottom=0.25) # Make space for the slider
input_field = fig.add_axes([left, bottom, width, height])
text_box = matplotlib.widgets.TextBox(input_field, 'value')
text_box.on_submit(submit)
output_box = fig.text(left+width+pad, bottom+height/2, s='Result = ', va='center')