Python 通过键盘输入关闭plt.show()
我想通过按键盘上的任意键从plt.show()关闭窗口。现在,我不知道为什么,我需要按两次键才能关上窗户 这是我的密码:Python 通过键盘输入关闭plt.show(),python,matplotlib,Python,Matplotlib,我想通过按键盘上的任意键从plt.show()关闭窗口。现在,我不知道为什么,我需要按两次键才能关上窗户 这是我的密码: # -*- coding: utf-8 -*- import datetime import os import psycopg2 import matplotlib.pyplot as plt try: conn = psycopg2.connect("dbname='mydb' user='mysur' host='localhost' password='m
# -*- coding: utf-8 -*-
import datetime
import os
import psycopg2
import matplotlib.pyplot as plt
try:
conn = psycopg2.connect("dbname='mydb' user='mysur' host='localhost' password='mypw'")
except psycopg2.DatabaseError, ex:
print 'I am unable to connect the database: ' + str(ex)
cur = conn.cursor()
cur.execute("select day, avg_price from day_sumary where day > '2018-05-20' and coin_nome = 'LTC'")
records = cur.fetchall()
coin_nome_sql = 'LTC'
cur.execute("select day, amount from day_sumary where day > '2018-05-20' and coin_nome = '"+ coin_nome_sql+"'")
records = cur.fetchall()
print(coin_nome_sql)
day, amount = zip(*records)
# graph code
plt.plot(day, amount, label= coin_nome_sql)
# draw a grid
plt.grid()
conn.commit()
cur.close()
conn.close()
# set title, X/Y labels
plt.title("amount per day")
plt.xlabel("day")
plt.ylabel("amount")
plt.legend()
plt.show()
plt.waitforbuttonpress(0)
plt.close()
您可以使用plt.draw()
代替plt.show()
,如中所示
plt.draw()
plt.waitforbuttonpress(0)
plt.close()
注意:它还会在鼠标单击时关闭窗口
非优雅的方式:
plt.draw()
while True:
if plt.waitforbuttonpress(0) == True:
plt.close()
break
您可能会发现,不要将读者与一些数据库输入混淆是有益的,因为它们无论如何都不可能复制。看见