2016-06-10 106 views
1

我是新来的蟒蛇世界。我试图创建一个图表,显示在给定时间段内所选城市的累计GDD(增长度天数)与时间的关系在Python中绘制累积图形

我写了一个函数来完成这个任务,但似乎无法使其工作对。我不断收到一个空的阴谋。还有一个尺寸错误。任何人都知道我可以如何解决这些问题。

任何帮助/建议表示赞赏!

下面是代码

import sys, argparse, csv 
import numpy as np 

import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
import datetime as dt 

def open_file(file_name, mode): 
    """Open a file.""" 
    try: 
     with open(sys.argv[1], 'r') as the_file: 
      filename=the_file.read() 

    except IOError as err: 
     print("Unable to open the file", file_name, "Ending program.\n", err) 
     input("\n\nPress the enter key to exit.") 
     sys.exit() 
    else: 
     return filename 

"""Cities.txt file as an argument"""  
try: 
    my_file = open_file(sys.argv[1], 'r') 
except: 
    print("GDD needs one argument", "\nEnding program...\n") 
    input("Press the enter key to exit.") 
    sys.exit() 


extension=".csv" 
for line in my_file.splitlines(): 
    file_name=line+extension 
    print(file_name) 
    with open('data/'+ file_name, "rU") as files: 
     val = list(csv.DictReader(files)) 
     l = [] 
    for i in range(0, len(val)): 
     row = val[i] 
     if row['Min Temp'] == '' or row['Max Temp'] == '': 
       pass 
     else: 
      GDD = ((float(row['Min Temp']) + float(row['Max Temp']))/2)-10 
      l.append(GDD) 

      val[i]['GDD'] = GDD 

      #print(row['Min Temp'], ' ' , row['Max Temp'], ' ', str(round(row['GDD'], 2))) 

      #plt.subplot(1,1,1)   
      #x = np.linspace(1, 12, 365, endpoint=True) 


    x = np.linspace(1, 12, 365, endpoint=True) 
    plt.plot(x,GDD, label = file_name.split(',')[0]) 
    plt.gcf().autofmt_xdate() 
    plt.legend(loc="upper left") 

    plt.xlabel('Months', color='black') 
    plt.ylabel('Cumulative GDD (>10°C)') 
    plt.title('Accumulated Growing Degree Days') 
    plt.draw()   

回答

0

一个简单而快速的解决方案可能是一个阴谋的呼叫后添加plt.hold(True)

一个更干净的解决方案是使用一个可以作为窗口绘制的图形。 just like in this example

所以你定义一个数字,保持其基准,加轴和这些轴执行所有动作

fig = plt.figure() 
ax = fig.add_axes([0.1, 0.1, 0.4, 0.7]) 
ax.plot(...) 
+0

感谢,得到它固定 – Jake