'calling plt.plot() shrinks pygame window

I have this example code:

import pygame
import numpy as np
import matplotlib.pyplot as plt

pygame.init()
pygame.display.set_caption('Test')

################## Globals ######################

FONT = pygame.font.SysFont('Cambria', 20)
CLOCK = pygame.time.Clock()

SCREEN_WIDTH, SCREEN_HEIGHT = 600, 700
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])

################ Functions ######################
def draw():
    pygame.event.pump()
    screen.fill((200,200,200))

    text = 'TEST'
    msg = FONT.render(text,True,(0,0,0))
    text_width,text_height = FONT.size(text)
    screen.blit(msg,( (SCREEN_WIDTH-text_width)//2, (SCREEN_HEIGHT-text_height)//2 ))

    # update
    pygame.display.update()



if __name__ == '__main__':
    # main pygame loop
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    x = np.linspace(0,10,200)
                    y = np.sin(x)
                    plt.plot(x,y)
                    plt.savefig('plot.pdf')
        draw()
        CLOCK.tick(60)

When I start the program it looks like this: enter image description here

But as soon as I hit SPACE (and therefor call plt.plot()) the window shrinks like this: enter image description here

Can some one explain why this is happening and maybe how to fix it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source