1. 程式人生 > 實用技巧 >出圖

出圖

import pandas as  pd
import numpy as np
import glob
import datetime
from matplotlib import pyplot as plt
import copy
import re
import time
from pylab import *
import matplotlib.dates as mdate
import matplotlib.patches as patches
time_parse = lambda date: datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
# time_parse1 = lambda date: datetime.datetime.strptime(date, '%Y-%m-%d %H:%M')
df = pd.read_csv(r'.\28_日t_1.csv',encoding='utf-8',parse_dates=['date'], date_parser=time_parse)

df1 = pd.read_csv(r'.\28ssc.csv',encoding='utf-8') # 讀取泥沙資料

df1['date'] = df1['date'].apply(lambda x:datetime.datetime.strptime(x,'%Y-%m-%d %H:%M'))

a = df.groupby('hb') # 畫水深圖所需 獲取剖面水深
b = df.groupby('date')
# 流速圖
pt = []
for date,b1 in b:
pt.append(np.array(b1['value']))
c= []
for i in range(0,len(pt)): # 判斷矩陣的維數
c.append(len(pt[i]))

s= np.array(c)

c1 = np.max(s)
# 增加維數,變成同維
for i in range(0,len(pt)): # 增加nan值補充值,變為同維。
if len(pt[i])!=c1:
pt[i] = np.append(pt[i], np.nan)
if len(pt[i])!=c1:
pt[i] = np.append(pt[i], np.nan)
if len(pt[i]) != c1:
pt[i] = np.append(pt[i], np.nan)
if len(pt[i]) != c1:
pt[i] = np.append(pt[i], np.nan)
if len(pt[i]) != c1:
pt[i] = np.append(pt[i], np.nan)
if len(pt[i]) != c1:
pt[i] = np.append(pt[i], np.nan)
if len(pt[i]) != c1:
pt[i] = np.append(pt[i], np.nan)
if len(pt[i]) != c1:
pt[i] = np.append(pt[i], np.nan)
if len(pt[i]) != c1:
pt[i] = np.append(pt[i], np.nan)
if len(pt[i]) != c1:
pt[i] = np.append(pt[i], np.nan)


else:
pt[i] = pt[i]

for i in range(len(pt)):
pt[i]=pt[i].reshape(len(pt[i]),1)

dt = pt[0]
for i in range(1,len(pt)):
dt = np.hstack((dt,pt[i]))
z = dt

a4 = a.get_group(2.18)
a5 =pd.DataFrame(a4)
a5.set_index(a5.date,inplace=True)

x1 = np.array(a5.index) # 重置時間序列
op = []
for et,val in a:
# print(et)
op.append(et)
y1 = np.array(op) * -1
X,Y= np.meshgrid(x1,y1)
print(X)
print(Y)
a3 =df1.groupby('hb') # 按照剖面進行獲取資料
s1 = []
for t,i in a3:
# print(t)
i1 = pd.DataFrame(i)
i1.reset_index(inplace=True,drop=True)
s= i1['ssc']
s1.append(s)

#
#

op1 =a3.get_group(1)
op2 = pd.DataFrame(op1)
op2.reset_index(inplace=True,drop=True)
op2.set_index(op2.date,inplace=True)

a1 = a.get_group(2.18) # 畫水深圖
a2 = pd.DataFrame(a1)
a2.reset_index(inplace=True,drop=True)
a2.set_index(a2.date,inplace=True)

s = a2['h']
font1 = {'family': 'Microsoft YaHei',
'weight': 'normal',
'size': 5.5,
}
fig = plt.figure(figsize=(16,8))
# 設定總圖的字型,能顯示中文
plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
# 設定子圖之間的間隔
subplots_adjust(left=0.15,bottom=0.2,top=0.8,right=0.95,hspace=0.18,wspace=0.25)
ax1 = fig.add_subplot(131)
ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S')) # 設定x軸的刻度
ax1.set_xticks(pd.date_range(a2.index[0],a2.index[-1],freq='3H'))
for tick in ax1.get_xticklabels():
tick.set_rotation(45)
x= a2.index[6]
x2 = a2.index[10]
y=s[6]
y2 = s[10]
arrowprops_1=dict(facecolor='#74C476',
shrink=1,#箭頭的收縮比
alpha=0.6,
width=7,#箭身寬
headwidth=40,#箭頭寬
hatch='--',#填充形狀
#身與頭比
#其它參考matplotlib.patches.Polygon中任何引數
)
ax1.annotate('開始取樣',xy=(x,y),xytext=(x,y+0.1),arrowprops =arrowprops_1)
ax1.annotate('結束取樣',xy=(x2,y2),xytext=(x2,y2-0.1),arrowprops =arrowprops_1)

ax1.set_ylabel('深度 (m)',fontsize=15)

ax1.plot(a2.index,s,color='r', label = '深度')
ax2 = fig.add_subplot(132)
ax2.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M')) # 設定x軸的刻度
ax2.set_xticks(pd.date_range(op2.index[0], op2.index[-1], freq='3H'))
ax2.set_ylabel('泥沙濃度',fontsize=15)
for tick in ax2.get_xticklabels():
tick.set_rotation(45)
ax2.plot(op2.index,np.array(s1[0]),label ='0h')
ax2.plot(op2.index,np.array(s1[1]),label ='0.2h')
ax2.plot(op2.index,np.array(s1[2]),label ='0.4h')
ax2.plot(op2.index,np.array(s1[3]),label ='0.6h')
ax2.plot(op2.index,np.array(s1[4]),label ='0.8h')
ax2.plot(op2.index,np.array(s1[5]),label ='1h')
ax3 = fig.add_subplot(133)
ax3.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S'))
ax3.set_xticks(pd.date_range(a5.index[0],a5.index[-1],freq='3H'))
for tick in ax3.get_xticklabels():
tick.set_rotation(45)
cb = ax3.contourf(X,Y,z, 75, cmap = 'rainbow')
ax3.set_ylabel('深度 (m)',fontsize=15)
cb1 =fig.colorbar(cb,ax =ax3) #給子圖配圖
ax1.legend(prop=font1, loc=2, ncol=1)
ax2.legend(prop=font1, loc=2, ncol=6)
cb1.ax.set_ylabel('流速 (m/s)')
# plt.show()
#
#
# #
fig_1 = plt.gcf()
fig_1.savefig('28.png', dpi=1000)
# fig_1.savefig(r'.\1.eps',format='eps',dpi = 1000)