简体   繁体   中英

how do I update the x limits in a matplotlib figure in tkinter

I made a GUI with TKinter that reads a scope trace from a agilent scope. I want the x axis to update when I change time/div. To update the x and y data I use set_xdata and set_ydata. Is there a similar method for updating the x limits?

You need to understand a bit of the object hierarchy. You are calling set_xdata on a Line2D object, which is an Artist which is associated with an Axes object (which handles things like log vs linear, the x/y limits, axis label, tick location and labels) which is associated with a Figure object (which groups together a bunch of axes objects, deals with the window manager (for gui), ect) and a canvas object (which actually deals with translating all of the other objects to a picture on the screen).

If you are using Tkinter, I assume that you have an axes object, (that I will call ax ).

ax = fig.subplot(111) # or where ever you want to get you `Axes` object from.
my_line = ax.plot(data_x, data_y)
# whole bunch of code
#
# more other code


# update your line object
my_line.set_xdata(new_x_data)
my_line.set_ydata(new_y_data)

# update the limits of the axes object that you line is drawn on.
ax.set_xlim([top, bottom])
ax.set_ylim([left, right])

so to update the data in the line, you need to update my_line , to update the axes limits, you need to update ax .

set_xlim doc and set_ylim doc

The pyplot.xlim() function changes the range of the x axis (of the current Axes).

The set_xticks() method of Axes sets the ticks. The current Axes can be obtained for instance with gca() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM