I am developing a graphing application using matplotlib and Tkinter. Added the function of changing the position of the axes:

ax = f.gca() ax.cla() ax.spines['left'].set_position('center') ax.spines['bottom'].set_position('center') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) f.canvas.draw() 

If there are no graphs in the program, at the time of redrawing the coordinate axes, then the axes change their positions and then the graphing proceeds as it should. But, if there are graphs in the program before the axes are redrawn, the axes are redrawn, and graphs built before are not displayed. How can I fix this point so that the axes are redrawn immediately with the available graphs? Thank you in advance.

    1 answer 1

    try this:

     In [27]: fig, ax = plt.subplots() In [28]: x = np.linspace(-5, 5, 100) In [29]: y = np.cos(x) In [30]: plt.plot(x, y) Out[30]: [<matplotlib.lines.Line2D at 0x28e00cd2cf8>] In [31]: ax.spines['left'].set_position('center') ...: ax.spines['bottom'].set_position('center') ...: ax.spines['top'].set_visible(False) ...: ax.spines['right'].set_visible(False) 

    enter image description here

    PS calling ax.cla() removes (clears) everything that was drawn on ax .

    • I use cla to clear the current position of the axes, since I have several buttons in the program. When you click, for example, on one of them, responsible for moving the axes to the center (as in this case), the previous position of the axes is cleared, and a new one is added. - Olga
    • In your example, you did not answer my question. Maybe you misunderstand me. The point is this: in the application window, in the input field, a function is entered (using gui) of the form y = f (x), then the user clicks the "build" button and the graph of the function is displayed on the canvas. - Olga
    • Also in the application there is a setting of the position of the axes of the graph. The user presses a button, for example, "Move axes to center" and the axes are shifted to the center, but the graph of the function entered earlier is no longer displayed. In the case when the position of the axes is first changed, and then the function is entered, the program works fine. I need a solution to this problem: so that the graph of the function, after changing the position of the axes, remains on the canvas. - Olga
    • @ Olga, ax.cla() - clears the axes and, accordingly, everything that was previously drawn on the ax is deleted. If it is absolutely necessary for you to call ax.cla() then simply redraw everything that has been painted before - MaxU
    • the fact of the matter is that I can’t get redrawing. In case I would enter functions in the program code itself, everything would be simpler than with input in the application. Any ideas? - Olga