damyarou

python, GMT などのプログラム

matplotlib 構造物上にプロット

記事の最後に行く

はじめに

描画出力に示すような、構造物の形状を描き、その中にデータをプロットします。 入力データファイルはエクセルで作成しています。 エクセルファイルは、シートを持つことができるので、このようなプロットの入力ファイルとして便利に使うことができます。

描画出力

f:id:damyarou:20190506081005j:plain

プログラミング

データファイル

入力データファイルは、エクセルで作成されており、1つのデータファイルに5個のシートが含まれ、以下の情報が格納されています。

sheet namedescription
ground地表面座標
damダム外形座標
coreコア外形座標
bf埋戻部外系座標
mdセンサーNo, センサー座標、区分、計測データ

以下に、ファイル名:xls_md2.xls の、シート:damとシート:md の事例画像を示します。

sheet: damsheet: md
f:id:damyarou:20190506081036p:plain:w260 f:id:damyarou:20190506081057p:plain:w280

プログラム全文

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tick
import pandas as pd


fsz=16
xmin=-125
xmax=125
dx=25
ymin=50
ymax=100
dy=10
fsl=85


def shape1():
    df1 = pd.read_excel('xls_md1.xlsx', sheet_name='ground') # coordinates of ground
    df2 = pd.read_excel('xls_md1.xlsx', sheet_name='dam')    # coordinates of dam body
    df3 = pd.read_excel('xls_md1.xlsx', sheet_name='core')   # coordinates of cray core
    dfm = pd.read_excel('xls_md1.xlsx', sheet_name='md')     # coordinates of sensors and water heads
    sgx=np.array(df1['sgx']); sgy=np.array(df1['sgy']) # coordinates od ground
    sdx=np.array(df2['sdx']); sdy=np.array(df2['sdy']) # coordinates of dam body
    scx=np.array(df3['scx']); scy=np.array(df3['scy']) # coordinates of cray core
    mdx=np.array(dfm['mdx']); mdy=np.array(dfm['mdy']) # coordinates of sensors
    loc=np.array(dfm['loc'])  # 0=foundation, 1=upstream body, 2=downstream body, 3=cray core
    dat=np.array(dfm['data']) # water head
    return sgx,sgy,sdx,sdy,scx,scy,mdx,mdy,loc,dat


def shape2():
    df1 = pd.read_excel('xls_md2.xlsx', sheet_name='ground') # coordinates of ground
    df2 = pd.read_excel('xls_md2.xlsx', sheet_name='dam')    # coordinates of dam body
    df3 = pd.read_excel('xls_md2.xlsx', sheet_name='core')   # coordinates of cray core
    df4 = pd.read_excel('xls_md2.xlsx', sheet_name='bf')     # coordinates of backfill
    dfm = pd.read_excel('xls_md2.xlsx', sheet_name='md')     # coordinates of sensors and water heads
    sgx=np.array(df1['sgx']); sgy=np.array(df1['sgy']) # coordinates of ground
    sdx=np.array(df2['sdx']); sdy=np.array(df2['sdy']) # coordinates of dam body
    scx=np.array(df3['scx']); scy=np.array(df3['scy']) # coordinates of cray core
    sbx=np.array(df4['sbx']); sby=np.array(df4['sby']) # coordinates of backfill
    mdx=np.array(dfm['mdx']); mdy=np.array(dfm['mdy']) # coordinates of sensors
    loc=np.array(dfm['loc'])  # 0=foundation, 1=upstream body, 2=downstream body, 3=cray core
    dat=np.array(dfm['data']) # water head
    return sgx,sgy,sdx,sdy,scx,scy,sbx,sby,mdx,mdy,loc,dat


def main():
    plt.figure(figsize=(20,8),facecolor='w')
    plt.rcParams['font.size']=fsz
    plt.rcParams['font.family']='sans-serif'

    for nnn in [211,212]:
        plt.subplot(nnn)
        if nnn==211: tstr='Total Water Head Distribution: Line MD1 (25/Feb/2019)'
        if nnn==212: tstr='Total Water Head Distribution: Line MD2 (25/Feb/2019)'
        plt.xlim([xmin,xmax])
        plt.ylim([ymin,ymax])
        plt.xlabel('Distance (m)')
        plt.ylabel('Elevation (EL.m)')
        plt.xticks(np.arange(xmin,xmax+dx,dx))
        plt.yticks(np.arange(ymin,ymax+dy,dy))
        plt.gca().spines['right'].set_visible(False)
        plt.gca().spines['top'].set_visible(False)
        plt.gca().set_aspect('equal',adjustable='box')
        plt.title(tstr,loc='left',fontsize=fsz+2)

        if nnn==211: sgx,sgy,sdx,sdy,scx,scy,mdx,mdy,loc,dat=shape1()
        if nnn==212: sgx,sgy,sdx,sdy,scx,scy,sbx,sby,mdx,mdy,loc,dat=shape2()

        col=[]
        mk=[]
        _xr=[]
        _pr=[]
        _xd=[]
        _pd=[]
        for i,n in enumerate(loc):
            if n==0: # foundation
                col=col+['#000080']
                mk=mk+['s']
            if n==1 or n==2: # dam body
                col=col+['#ff00ff']
                mk=mk+['o']
            if n==3: # cray core
                col=col+['#ff00ff']
                mk=mk+['^']

            if n==0: # data in foundation
                _xr=_xr+[mdx[i]]
                _pr=_pr+[dat[i]]
            else: # data in dam body
                _xd=_xd+[mdx[i]]
                _pd=_pd+[dat[i]]
        xr=np.array(_xr)
        pr=np.array(_pr)
        xd=np.array(_xd)
        pd=np.array(_pd)
        i1=np.argsort(xr)
        i2=np.argsort(xd)
        plt.plot([sgx[0],sgx[0]+50],[fsl,fsl],'-',color='#0000ff',lw=1) # FSL
        plt.text(sgx[0],fsl,'FSL EL.{0:.0f}m'.format(fsl),color='#0000ff',va='bottom',ha='left',fontsize=fsz)
        xs=sgx[0]+27; ys=fsl; ds=3
        plt.plot([xs,xs+ds/2,xs-ds/2,xs],[ys,ys+ds,ys+ds,ys],'-',color='#0000ff',lw=1)
        if nnn==212: # filling backfill area
            plt.fill(sbx,sby,color='#cccccc')
            plt.text(105,67,'Backfill',va='center',ha='center',fontsize=fsz-2)
        plt.plot(sgx,sgy,'-',color='#000000',lw=1) # drawing ground surface
        plt.fill(sdx,sdy,facecolor='#ffffcc',edgecolor='#000000',ls='-',lw=1) # filling dam body area
        plt.fill(scx,scy,facecolor='#66ff99',edgecolor='#000000',ls='-',lw=1) # filling cray core area
        plt.plot(mdx,mdy,'+',color='#000000')      # plot of sensor location
        for i in range(len(mdx)):
            plt.plot(mdx[i],dat[i],color=col[i],marker=mk[i],ms=8) # plotting obseved data
        plt.plot(xr[i1],pr[i1],'--',color='#000080',lw=2)          # connecting obserbed data
        plt.plot(xd[i2],pd[i2],'-',color='#ff00ff',lw=2)           # connecting observed data
        
        plt.plot([0],[0],'+',color='#000000',label='Sensor location') # dummy for legend
        plt.plot([0],[0],'s',color='#000080',label='Foundation rock') # dummy for legend
        plt.plot([0],[0],'o',color='#ff00ff',label='Shoulder fill')   # dummy for legend
        plt.plot([0],[0],'^',color='#ff00ff',label='Cray core')       # dummy for legend
        plt.legend(loc='upper right',shadow=True,fontsize=fsz-2)      # drawing legend
        
    plt.tight_layout()
    fnameF='fig_dam_p.jpg'
    plt.savefig(fnameF, dpi=200, bbox_inches="tight", pad_inches=0.1)
    plt.show()


#==============
# Execution
#==============
if __name__ == '__main__': main()

Thank you.

記事の先頭に行く