Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] The window shrink after using the matplotlib.pyplot.show() #4561

Closed
7 tasks
CaffreyR opened this issue Jul 28, 2021 · 10 comments
Closed
7 tasks

[Bug] The window shrink after using the matplotlib.pyplot.show() #4561

CaffreyR opened this issue Jul 28, 2021 · 10 comments
Labels
question Further information is requested

Comments

@CaffreyR
Copy link

Type of Issue (Enhancement, Error, Bug, Question)

Bug

Operating System

Win10

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkinter

Versions

Version information can be obtained by calling sg.main_get_debug_data()
Or you can print each version shown in ()

Python version (sg.sys.version)

3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 12:30:02) [MSC v.1900 64 bit (AMD64)]

PySimpleGUI Version (sg.__version__)

PySimpleGUI version: 4.45.0

GUI Version (tkinter (sg.tclversion_detailed), PySide2, WxPython, Remi)

tkinter version: 8.6.6


Your Experience In Months or Years (optional)

Years Python programming experience

Years Programming experience overall

Have used another Python GUI Framework? (tkinter, Qt, etc) (yes/no is fine)

Anything else you think would be helpful?


Troubleshooting

These items may solve your problem. Please check those you've done by changing - [ ] to - [X]

  • Searched main docs for your problem www.PySimpleGUI.org
  • Looked for Demo Programs that are similar to your goal Demos.PySimpleGUI.org
  • If not tkinter - looked for Demo Programs for specific port
  • For non tkinter - Looked at readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • Run your program outside of your debugger (from a command line)
  • Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released

Detailed Description

I've defined a button that, when clicked, will call a drawing function and draw a ’plt‘ image. However when the image has been drawn, the entire window will be smaller than it was before, and the font will be smaller.But the function will still work except for the size.Thank you .

Code To Duplicate

A short program that isolates and demonstrates the problem (Do not paste your massive program, but instead 10-20 lines that clearly show the problem)

This pre-formatted code block is all set for you to paste in your bit of code:

# Paste your code here
`def draw_graph(G,alpha,node_scale,figsize):
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.figure(figsize=figsize)
    pos = nx.spring_layout(G)
    nx.draw_networkx_nodes(G,pos,node_size=[G.degree[x]*node_scale for x in G.nodes])
    nx.draw_networkx_edges(G,pos,alpha=alpha)
    nx.draw_networkx_labels(G,pos)
    plt.axis("off")
    plt.show()

layout3=[[sg.Button('Social Network'),sg.Button('Exit')]]
window = sg.Window("Social Network demo", layout3)
while True:
     XXXX
     if event =='XXX':
               draw_graph(G_sub, alpha=0.5, node_scale=10, figsize=(6, 4))`

Screenshot, Sketch, or Drawing

image
image

@ghost
Copy link

ghost commented Jul 28, 2021

The PySimpleGUI GitHub Issues bot has determined there is a problem with your Issue's form.

Please fix the problems by editing the first comment or the title and then reopen your issue to have it checked again.

More detailed information:

Title must be ONE of:
Bug
Question
Enhancement
Error

@ghost ghost closed this as completed Jul 28, 2021
@ghost ghost added Fill issue form or you will be REJECTED You MUST use the supplied template to submit a request. PySimpleGUI Issues Bot Has Detected an Error labels Jul 28, 2021
@CaffreyR CaffreyR changed the title The window shrink after using the matplotlib.pyplot.show() [Bug] The window shrink after using the matplotlib.pyplot.show() Jul 28, 2021
@ghost ghost removed Fill issue form or you will be REJECTED You MUST use the supplied template to submit a request. PySimpleGUI Issues Bot Has Detected an Error labels Jul 28, 2021
@ghost ghost reopened this Jul 28, 2021
@jason990420
Copy link
Collaborator

jason990420 commented Jul 28, 2021

No executable short code to show what the issue.
I guess the diemnesion of window reduced by a scale when call matplotlib.pyplot.show.

Basically, PySimpleGUI use backend tkinter and Matplotlib use backend Qt5.
You use two different GUIs at the same time, do it only if you know how to handle both of them.

IMO, they use different DPI or Scale for GUI, so you got the odd issue.
Following code may work for you,

import numpy as np
import matplotlib.pyplot as plt
import PySimpleGUI as sg

def set_scale(scale):

    root = sg.tk.Tk()
    root.tk.call('tk', 'scaling', scale)
    root.destroy()

def show():

    x = np.random.randint(low=1, high=11, size=50)
    y = x + np.random.randint(1, 5, size=x.size)
    data = np.column_stack((x, y))

    ax1.scatter(x=x, y=y, marker='o', c='r', edgecolor='b')
    ax1.set_title('Scatter: $x$ versus $y$')
    ax1.set_xlabel('$x$')
    ax1.set_ylabel('$y$')

    ax2.hist(data, bins=np.arange(data.min(), data.max()), label=('x', 'y'))
    ax2.legend(loc=(0.65, 0.8))
    ax2.set_title('Frequencies of $x$ and $y$')
    ax2.yaxis.tick_right()
    plt.show()

sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 80))

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
set_scale(fig.dpi/72)       # Set DPI of PySimpleGUI/tkinter to be same as it of Matplotlib/Qt5

layout = [[sg.Button("Show")]]
window = sg.Window('Title', layout, location=(0, 0) , finalize=True)

while True:

    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == "Show":
        show()

window.close()

@jason990420 jason990420 added the question Further information is requested label Jul 28, 2021
@CaffreyR
Copy link
Author

Not executable short code to show what the issue.
I guess the diemnesion of window reduced by a scale when call matplotlib.pyplot.show.

Basically, PySimpleGUI use backend tkinter and Matplotlib use backend Qt5.
You use two different GUIs at the same time, do it only if you know how to handle both of them.

IMO, they use different DPI or Scale for GUI, so you got the odd issue.
Following code may work for you,

import numpy as np
import matplotlib.pyplot as plt
import PySimpleGUI as sg

def set_scale(scale):

    root = sg.tk.Tk()
    root.tk.call('tk', 'scaling', scale)
    root.destroy()

def show():

    x = np.random.randint(low=1, high=11, size=50)
    y = x + np.random.randint(1, 5, size=x.size)
    data = np.column_stack((x, y))

    ax1.scatter(x=x, y=y, marker='o', c='r', edgecolor='b')
    ax1.set_title('Scatter: $x$ versus $y$')
    ax1.set_xlabel('$x$')
    ax1.set_ylabel('$y$')

    ax2.hist(data, bins=np.arange(data.min(), data.max()), label=('x', 'y'))
    ax2.legend(loc=(0.65, 0.8))
    ax2.set_title('Frequencies of $x$ and $y$')
    ax2.yaxis.tick_right()
    plt.show()

sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 80))

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
set_scale(fig.dpi/72)       # Set DPI of PySimpleGUI/tkinter to be same as it of Matplotlib/Qt5

layout = [[sg.Button("Show")]]
window = sg.Window('Title', layout, location=(0, 0) , finalize=True)

while True:

    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == "Show":
        show()

window.close()

Thanks for replying. My code runs well when I added the ’set_scale‘ function. At least the size of PysimpleGUI window is stable.I'd be appreciate if you could tell me what the function exactly does in this case. I guess it probably locked the setting of tkinter?Thank you again.
def set_scale(scale): root = sg.tk.Tk() root.tk.call('tk', 'scaling', scale) root.destroy() fig=plt.figure(figsize=(8, 4)) set_scale(fig.dpi/36)

@jason990420
Copy link
Collaborator

jason990420 commented Jul 28, 2021

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
scale = fig.dpi/72            # why you changed it to fig.dpi/36 ?
root = sg.tk.Tk()
root.tk.call('tk', 'scaling', scale)
root.destroy()

Just to set the setting of DPI same in both GUI libraries.
I get the default DPI setting in matplotlib/Qt5, then set the DPI of PySimpleGUI/tkinter to it.

Dots per inch (DPI, or dpi[1]) is a measure of spatial printing, video or image scanner dot density, in particular the number of individual dots that can be placed in a line within the span of 1 inch (2.54 cm). Similarly, the more newly introduced[2] dots per centimetre (d/cm or dpcm) refers to the number of individual dots that can be placed within a line of 1 centimetre (≈ 0.393 in).

or, you can get the DPI of PySimpleGUI/tkinter, then set the DPI of Matplotlib/Qt5 to it.
I am not sure how to set DPI of Matplotlib/Qt5.

@PySimpleGUI
Copy link
Owner

I always thought matplotlib was entirely tkinter based internally. I appreciate the education!

@jason990420
Copy link
Collaborator

jason990420 commented Jul 28, 2021

In following code, you can see there're 21 backends in matplotlib 3.4.2/python 3.9.5 on my WIN10, not only 'TkAgg'.

>>> import matplotlib.rcsetup as rcsetup
>>>
>>> print(rcsetup.interactive_bk)
['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo']
>>> print(rcsetup.non_interactive_bk)
['agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']
>>> print(rcsetup.all_backends)
['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']
>>> len(rcsetup.all_backends)
21

@CaffreyR
Copy link
Author

CaffreyR commented Jul 28, 2021

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
scale = fig.dpi/72            # why you changed it to fig.dpi/36 ?
root = sg.tk.Tk()
root.tk.call('tk', 'scaling', scale)
root.destroy()

Just to set the setting of DPI same in both GUI libraries.
I get the default DPI setting in matplotlib/Qt5, then set the DPI of PySimpleGUI/tkinter to it.

Dots per inch (DPI, or dpi[1]) is a measure of spatial printing, video or image scanner dot density, in particular the number of individual dots that can be placed in a line within the span of 1 inch (2.54 cm). Similarly, the more newly introduced[2] dots per centimetre (d/cm or dpcm) refers to the number of individual dots that can be placed within a line of 1 centimetre (≈ 0.393 in).

or, you can get the DPI of PySimpleGUI/tkinter, then set the DPI of Matplotlib/Qt5 to it.
I am not sure how to set DPI of Matplotlib/Qt5.

Thank you very much! The reason why i changed the parameter is it looks bigger.HAHA! Is it important?
image

@jason990420
Copy link
Collaborator

For the scale factor, you can refer here jason990420/PySimpleGUI-Solution#80

@CaffreyR
Copy link
Author

For the scale factor, you can refer here jason990420/PySimpleGUI-Solution#80

Thanks again.I will ask for help if I encounter other problems.

@CaffreyR
Copy link
Author

I always thought matplotlib was entirely tkinter based internally. I appreciate the education!

Thank you for the open source code that provides me a learning platform!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants