0

I'm making a GUI based Tkinter project, where after the home screen I must open the following window with a figure having a bar graph plotted. Following code is just a part of my project(and a command of one of many buttons in the project// And screen2 is the Toplevel() window from another root window)

    qy4=Toplevel(screen2)
    qy4.title("4th Query")
    adjustWindow(qy4)


    f1=Figure(figsize=(5,5),dpi=100).add_subplot(111).plot(q4())

    canvas=FigureCanvasTkAgg(f1,master=canvas)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.TOP,fill=tk.BOTH,expand=True)

    toolbar=NavigationToolbar2Tk(f1,master=canvas)
    toolbar.update()
    canvas._tkcanvas.pack(side=tk.TOP,fill=tk.BOTH,expand=True) 

But I get this:enter image description here I want to have the same figure on my "qy4" window So that I can add more labels or textboxes above or below the bar graph figure I heard tried n number of things, but still can't find what is wrong in the code.

enter image description here

Also, the above figure's x-axis labels are not visible, how do I show them in the following manner(Mind you below code in the screenshot is edited for showing how it should be displayed): enter image description here

Actual code that I'm calling as q4() from other module :

enter image description here I have not uploaded the whole project code but just a part of it, I'm quite new to Python and Tkinter, I'm following the procedural method in my project, I know it is a bad way to code.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Aditya Patil
  • 123
  • 1
  • 1
  • 9
  • 1
    ***`canvas=FigureCanvasTkAgg(f1,master=canvas)`***: This should throw an error, because `canvas` is used before defined? Should read: `canvas=FigureCanvasTkAgg(f1,master=qy4)` – stovfl Dec 23 '19 at 10:53
  • I made the changes. Still, the figure is displaying outside of the canvas window. – Aditya Patil Dec 23 '19 at 19:03
  • Read [You are creating two figures](https://stackoverflow.com/a/55026214/7414759) – stovfl Dec 23 '19 at 20:07
  • 1
    "I'm following the procedural method in my project, I know it is a bad way to code" -- Au contraire! There's nothing inherently wrong with procedural programming. – Jonathan Hall Dec 25 '19 at 21:59

1 Answers1

1

You need to specify the name of the Toplevel window as master window for the plot in the FigureCanvasTkAgg method.

You can use the plt.xticks(x_ticks_data, rotation=90) to make your x-axis labels print vertically.

Here is a simple illustration:

Code:

import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

window = tk.Tk()

def plot():
    top_win = tk.Toplevel(window)

    x = ['Col A', 'Col B', 'Col C']

    y = [50, 20, 80]

    fig = plt.figure(figsize=(4, 5))
    plt.bar(x=x, height=y)

    # You can make your x axis labels vertical using the rotation
    plt.xticks(x, rotation=90)

    # specify the toplevel window as master
    canvas = FigureCanvasTkAgg(fig, master=top_win)
    canvas.draw()
    canvas.get_tk_widget().grid(row=0, column=0, ipadx=40, ipady=20)

btn = tk.Button(window, text='Show plot', command=plot)
btn.grid(row=0, column=0, padx=20, pady=10)

window.mainloop()

Output:

tkinter with plot

Somraj Chowdhury
  • 983
  • 1
  • 6
  • 14
  • Thanks, about the xticks(). But I found this solution better here [link](https://stackoverflow.com/questions/32244019/how-to-rotate-x-axis-tick-labels-in-pandas-barplot) – Aditya Patil Dec 23 '19 at 19:44
  • But after making changes for the master window. Still, the figure is displayed outside of the canvas. – Aditya Patil Dec 23 '19 at 19:46