# -*- coding: utf-8 -*- """ Created on Mon Jul 8 12:24:07 2019 @author: finnh https://matplotlib.org/gallery/widgets/textbox.html#sphx-glr-gallery-widgets-textbox-py """ import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import TextBox # Function for updating plot for new value of param a: def fun_submit_a(dummy_arg): a = eval(text_box_a.text) b = eval(text_box_b.text) y_array = a*x_array + b line_1.set_ydata(y_array) # ax.set_ylim(np.min(y_array), np.max(y_array)) plt.draw() # Function for updating plot for new value of param b: def fun_submit_b(dummy_arg): a = eval(text_box_a.text) b = eval(text_box_b.text) y_array = a*x_array + b line_1.set_ydata(y_array) # ax.set_ylim(np.min(y_array), np.max(y_array)) plt.draw() # Opening new figure with one positioned plot plt.close('all') fig_width_inch = 24/2.54 fig_height_inch = 18/2.54 (fig, ax) = plt.subplots(num='Interactive plot', figsize=(fig_width_inch, fig_height_inch)) left=0.125; bottom=0.3; right=0.9; top=0.9 plt.subplots_adjust(left, bottom, right, top) plt.ylim(-2, 2) # Generating and plotting y_array using initial values of a and b: x_array = np.arange(0.0, 2.0, 0.001) a = 1 b = 0 y_array = a*x_array + b line_1, = plt.plot(x_array, y_array) plt.grid(which='both', color='grey') # Generating textboxes for a and b: left_a=0.1; bottom_a=0.15; width_a=0.1; height_a=0.05 left_b=0.1; bottom_b=0.05; width_b=0.1; height_b=0.05 axbox_a = plt.axes([left_a, bottom_a, width_a, height_a]) axbox_b = plt.axes([left_b, bottom_b, width_b, height_b]) caption_a = 'a = ' caption_b = 'b = ' initial_text_a = "1.0" initial_text_b = "0.0" text_box_a = TextBox(axbox_a, caption_a, initial_text_a) text_box_b = TextBox(axbox_b, caption_b, initial_text_b) # Detecting click on Enter button of a or b: # Invoking function fun_submit_a() on click: text_box_a.on_submit(fun_submit_a) # Invoking function fun_submit_b() on click: text_box_b.on_submit(fun_submit_b) plt.show() #a = 1 og b = 0: #plt.savefig('prog_interaktivt_plott_tekstboks1.pdf') #a = -1 og b = 1: #plt.savefig('prog_interaktivt_plott_tekstboks2.pdf')