首页 > 解决方案 > 在同一页面上绘制多个图,每个图中重复一组数据

问题描述

我有一个代表 17 个雨量计的数据集。我有代码可以在一个特定的时间段内将所有 17 个都绘制在一个图上。数据是从 postgres 数据库中提取的,因为它的大小每天都在增加。下面显示的代码有效。雨量计以四个为一组,其中一个作为对照。我想在一个子图上绘制每组四个(连同控件),一页上有四个图。代码从 postgres 中提取每个仪表数据并将其添加到数组“raindata”中。for 循环然后将每个仪表绘制在同一个图上,给我一个 17 行的图。

我看不到如何循环遍历数组以取出 0、1、2、3、4,然后取出 0、5、6、7、8 等并将它们绘制在单独的图上。


from datetime import datetime, timedelta
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

#connect to postgres
exec(open('connect.py').read())
#define device id's
rgn = ['highpoint','md1','md2','md3','md4','mc1','mc2','mc3',
       'mc4','ic1','ic2','ic3','ic4','imd1','imd2','imd3','imd4']
#define todays date, date three days ago and potghes tablename
# date= datetime.now().strftime('%Y-%m-%d')
# fdate=(datetime.now()-timedelta(3)).strftime('%Y-%m-%d')
date2=datetime.now().strftime('%d%m%y')
fdate = str(input ("Date from as yyyy-mm-dd: "))
tdate= str(input("Date to as yyy-mm-dd: "))
tabname = 'raindatav3'+(date2)

#create lists of each rainfall readings over last three days
rainmm=[]
raindata=[]
for k in range(len(rgn)):
    rg=rgn[k]
    idcall = "select device_id, datetime, rainmm -lag(rainmm,1)over \
        (order by datetime) as diff from %s where device_id = '%s' and \
        datetime between '%s' and '%s';"% (tabname, rg, fdate, tdate);
    cur.execute(idcall)
    data = cur.fetchall()
    rainmm=np.array(data)
    rainmm[0,2]=0
    mv=rainmm[np.argmin(rainmm[:, 2]),2]
    if mv < 0:
        rainmm=np.where(rainmm==mv, 0, rainmm)
    
    raindata.append(rainmm)
    continue
cur.close()
conn.close()
    
fontP = FontProperties()
fontP.set_size('xx-small')

for i in range(len(rgn)):
   
    
   plt.plot(raindata[i][:,1],raindata[i][:,2], label = rgn[i])
   plt.xticks(rotation=90)
   continue
plt.legend(title ='gauge name', bbox_to_anchor=(1.05, 1), loc='upper left', prop=fontP)
plt.title('Rain Gauge rain in period (5 minute intervals) ' + fdate +' to '+ tdate)
plt.show()

标签: matplotlib

解决方案


推荐阅读