优化代码,添加十二长生的代码


import matplotlib.pyplot as plt
import matplotlib
import matplotlib.gridspec as gridspec
matplotlib.rcParams['font.family'] = 'FangSong'

from qimen3 import *
y = int(input("请输入年份: "))
m = int(input("请输入月份: "))
d = int(input("请输入日期: "))
h = int(input("请输入时间(24小时制):"))

jihe = Qimen(y,m,d,h).p

print(jihe)

gs = gridspec.GridSpec(3, 3)

def paipan(x):
    plt.xticks([])
    plt.yticks([])
    global dipan, tianpan, men

#天盘干
    tianpan = jihe['天盤'][0][x]
    plt.text(0.65, 0.3, tianpan)

    # 天盘十二长生
    if x == '兌':
        if tianpan == '戊' or tianpan == '丙':
            plt.text(0.8, 0.3, '死', fontsize=8, color='green')
        if tianpan == '庚':
            plt.text(0.8, 0.3, '帝旺', fontsize=8, color='red')
        if tianpan == '癸':
            plt.text(0.8, 0.3, '病', fontsize=8, color='green')
        if tianpan == '辛':
            plt.text(0.8, 0.3, '临官', fontsize=8, color='red')
        if tianpan == '壬':
            plt.text(0.8, 0.3, '沐浴', fontsize=8)
        if tianpan == '己' or tianpan == '丁':
            plt.text(0.75, 0.3, '长生', fontsize=8, color='red')   
    if x == '坎':
        if tianpan == '壬':
            plt.text(0.75, 0.3, '帝旺', fontsize=8, color='red')
        if tianpan == '丙' or tianpan == '戊':
            plt.text(0.75, 0.3, '胎', fontsize=8)
        if tianpan == '辛':
            plt.text(0.75, 0.3, '长生', fontsize=8, color='red')
        if tianpan == '庚':
            plt.text(0.75, 0.3, '死', fontsize=8, color='green')
        if tianpan == '丁' or tianpan == '己':
            plt.text(0.75, 0.3, '绝', fontsize=8, color='green')
        if tianpan == '癸':
            plt.text(0.75, 0.3, '临官', fontsize=8, color='red')
        if tianpan == '乙':
            plt.text(0.75, 0.3, '病', fontsize=8, color='green')
    if x == '離':
        if tianpan == '己' or tianpan == '丁':
            plt.text(0.75, 0.3, '临官', fontsize=8, color='red')   
        if tianpan == '庚':
            plt.text(0.75, 0.3, '沐浴', fontsize=8)
        if tianpan == '丙' or tianpan == '戊':
            plt.text(0.75, 0.3, '帝旺', fontsize=8, color='red')
        if tianpan == '癸':
            plt.text(0.75, 0.3, '绝', fontsize=8, color='green')
        if tianpan == '乙':
            plt.text(0.75, 0.3, '长生', fontsize=8, color='red') 
    if x == '震':
        if tianpan == '丁' or tianpan == '己':
            plt.text(0.75, 0.3, '病', fontsize=8, color='green')
        if tianpan == '壬':
            plt.text(0.75, 0.3, '死', fontsize=8, color='green')
        if tianpan == '丙' or tianpan == '戊':
            plt.text(0.75, 0.3, '沐浴', fontsize=8)
        if tianpan == '癸':
            plt.text(0.75, 0.3, '长生', fontsize=8, color='red')  
        if tianpan == '乙':
            plt.text(0.75, 0.3, '临官', fontsize=8, color='red')  
        if tianpan == '辛':
            plt.text(0.75, 0.3, '绝', fontsize=8, color='green')
    if x == '艮':
        if tianpan == '癸':
            plt.text(0.75, 0.3, '沐浴冠带', fontsize=5)
        if tianpan == '乙':
            plt.text(0.75, 0.3, '帝旺', fontsize=5, color='red')
            plt.text(0.85, 0.3, '衰', fontsize=5, color='green')
        if tianpan == '庚':
            plt.text(0.75, 0.3, '墓绝', fontsize=8, color='green')
        if tianpan == '丁' or tianpan == '己':
            plt.text(0.75, 0.3, '死墓', fontsize=8, color='green')
        if tianpan == '丙' or tianpan == '戊':
            plt.text(0.75, 0.3, '长生', fontsize=5, color='red')
            plt.text(0.85, 0.3, '养', fontsize=5)
        if tianpan == '壬':
            plt.text(0.75, 0.3, '衰病', fontsize=8, color='green')
    if x == '乾':
        if tianpan == '丙' or tianpan == '戊':
            plt.text(0.75, 0.3, '墓绝', fontsize=8, color='green')
        if tianpan == '丁' or tianpan == '己':
            plt.text(0.75, 0.3, '胎养', fontsize=8)
        if tianpan == '乙':
            plt.text(0.75, 0.3, '死墓', fontsize=8, color='green')
        if tianpan == '癸':
            plt.text(0.75, 0.3, '帝旺', fontsize=5, color='red')
            plt.text(0.85, 0.3, '衰', fontsize=5, color='green')
    if x == '巽':
        if tianpan == '乙':
            plt.text(0.75, 0.3, '冠带沐浴', fontsize=5)
        if tianpan == '庚':
            plt.text(0.75, 0.3, '长生', fontsize=5, color='red')
            plt.text(0.85, 0.3, '养', fontsize=5)
        if tianpan == '壬':
            plt.text(0.75, 0.3, '墓绝', fontsize=8, color='green')
        if tianpan == '辛':
            plt.text(0.75, 0.3, '死墓', fontsize=8, color='green')
        if tianpan == '丁' or tianpan == '己':
            plt.text(0.75, 0.3, '帝旺', fontsize=5, color='red')
            plt.text(0.85, 0.3, '衰', fontsize=5, color='green')
        if tianpan == '丙' or tianpan == '戊':
            plt.text(0.85, 0.3, '临官', fontsize=5, color='red')
            plt.text(0.75, 0.3, '冠带', fontsize=5)
    if x == '坤':
        if tianpan == '丁' or tianpan == '己':
            plt.text(0.75, 0.3, '冠带沐浴', fontsize=5)
        if tianpan == '壬':
            plt.text(0.75, 0.3, '长生', fontsize=5, color='red')
            plt.text(0.85, 0.3, '养', fontsize=5)
        if tianpan == '丙' or tianpan == '戊':
            plt.text(0.75, 0.3, '衰病', fontsize=8, color='green')
        if tianpan == '辛':
            plt.text(0.75, 0.3, '衰', fontsize=5, color='green')
            plt.text(0.85, 0.3, '帝旺', fontsize=5, color='red')
        if tianpan == '乙':
            plt.text(0.75, 0.3, '胎养', fontsize=8)
        if tianpan == '庚':
            plt.text(0.85, 0.3, '临官', fontsize=5, color='red')
            plt.text(0.75, 0.3, '冠带', fontsize=5)
        if tianpan == '癸':
            plt.text(0.75, 0.3, '死墓', fontsize=8, color='green')
    
    
    try:
        tianpan1 = jihe['天盤'][1][x]
        plt.text(0.01, 0.3, tianpan1)
        
        if x == '兌':
            if tianpan1 == '辛':
                plt.text(0.11, 0.3, '临官', fontsize=8, color='red')
            if tianpan1 == '丁' or tianpan == '己':
                plt.text(0.11, 0.3, '长生', fontsize=8, color='red')
        if x == '坎':
            if tianpan1 == '辛':
                plt.text(0.11, 0.3, '长生', fontsize=8, color='red')
            if tianpan1 == '丁' or jihe['天盤'][1][x] == '己':
                plt.text(0.11, 0.3, '绝', fontsize=8, color='green')
            if tianpan1 == '庚':
                plt.text(0.11, 0.3, '死', fontsize=8, color='green')
        if x == '乾':
            if tianpan1 == '戊':
                plt.text(0.11, 0.3, '死墓', fontsize=8, color='green')
        if x == '離':
            if tianpan1 == '戊':
                plt.text(0.11, 0.3, '帝旺', fontsize=8, color='red')
            if tianpan1 == '庚':
                plt.text(0.11, 0.3, '沐浴', fontsize=8)
        if x == '艮':
            if tianpan1 == '庚':
                plt.text(0.11, 0.3, '墓绝', fontsize=8, color='green')
    except KeyError as err:
        pass
    else:
        pass
    
    
#地盘干
    dipan = jihe['地盤'][x] 
    plt.text(0.65, 0.1, dipan)
    
    #地盘十二长生
    if x == '兌':
        if dipan == '癸':
            plt.text(0.75, 0.1, '病', fontsize=8, color='green')
        if dipan == '乙':
            plt.text(0.75, 0.1, '绝', fontsize=8, color='green')
        if dipan == '庚':
            plt.text(0.75, 0.1, '帝旺', fontsize=8, color='red')
        if dipan == '壬':
            plt.text(0.75, 0.1, '沐浴', fontsize=8)
    if x == '坎':
        if dipan == '乙':
            plt.text(0.75, 0.1, '病', fontsize=8, color='green')
        if dipan == '庚':
            plt.text(0.75, 0.1, '死', fontsize=8, color='green')
        if dipan == '癸':
            plt.text(0.75, 0.1, '临官', fontsize=8, color='red')
        if dipan == '丙' or dipan == '戊':
            plt.text(0.75, 0.1, '胎', fontsize=8)
    if x == '離':
        if dipan == '丙' or dipan == '戊':
            plt.text(0.75, 0.1, '帝旺', fontsize=8, color='red')
        if dipan == '己' or dipan == '丁':
            plt.text(0.75, 0.1, '临官', fontsize=8, color='red')    
        if dipan == '壬':
            plt.text(0.75, 0.1, '胎', fontsize=8)
    if x == '震':
        if dipan == '己' or dipan == '丁':
            plt.text(0.75, 0.1, '病', fontsize=8, color='green')
        if dipan == '丁' or dipan == '己':
            plt.text(0.75, 0.1, '病', fontsize=8, color='green')
        if dipan == '壬':
            plt.text(0.75, 0.1, '死', fontsize=8, color='green')
        if dipan == '丙' or dipan == '戊':
            plt.text(0.75, 0.1, '沐浴', fontsize=8)
            
    if x == '艮':
        if dipan == '丁':
            plt.text(0.75, 0.1, '死墓', fontsize=8, color='green')
        if dipan == '丙' or dipan == '戊':
            plt.text(0.75, 0.1, '长生', fontsize=5, color='red')
            plt.text(0.85, 0.1, '养', fontsize=5)
        if dipan == '辛':
            plt.text(0.75, 0.1, '胎养', fontsize=8)
        if dipan == '癸':
            plt.text(0.75, 0.1, '沐浴冠带', fontsize=5)
    if x == '乾':
        if dipan == '壬':
            plt.text(0.75, 0.1, '冠带', fontsize=5)
            plt.text(0.85, 0.1, '临官', fontsize=5, color='red')
        if dipan == '丙' or dipan == '戊':
            plt.text(0.75, 0.1, '墓绝', fontsize=8, color='green')
        if dipan == '丁' or dipan == '己':
            plt.text(0.75, 0.1, '胎养', fontsize=8)
        if dipan == '辛':
            plt.text(0.75, 0.1, '沐浴冠带', fontsize=5)
    if x == '巽':
        if dipan == '庚':
            plt.text(0.75, 0.1, '长生', fontsize=5, color='red')
            plt.text(0.85, 0.1, '养', fontsize=5)
        if dipan == '癸':
            plt.text(0.75, 0.1, '胎养', fontsize=8)
        if dipan == '乙':
            plt.text(0.75, 0.1, '沐浴冠带', fontsize=5)
        if dipan == '己':
            plt.text(0.75, 0.1, '衰', fontsize=5, color='green')
            plt.text(0.85, 0.1, '帝旺', fontsize=5, color='red')
    if x == '坤':
        if dipan == '丙' or dipan == '戊':
            plt.text(0.75, 0.1, '衰病', fontsize=8, color='green')
        if dipan == '辛':
            plt.text(0.75, 0.1, '衰', fontsize=5, color='green')
            plt.text(0.85, 0.1, '帝旺', fontsize=5, color='red')
        if dipan == '丁' or dipan == '己':
            plt.text(0.75, 0.1, '冠带沐浴', fontsize=5)
        if dipan == '壬':
            plt.text(0.75, 0.1, '长生', fontsize=5, color='red')
            plt.text(0.85, 0.1, '养', fontsize=5)
        if dipan == '乙':
            plt.text(0.75, 0.1, '胎养', fontsize=8)
    
    #凶格
    if tianpan == '庚' and dipan == '癸':
        plt.text(0.8, 0.85, '大格', fontsize=8, color='green')
    if tianpan == '辛' and dipan == '庚':
        plt.text(0.6, 0.85, '白虎出力', fontsize=8, color='green')
    if tianpan == '辛' and dipan == '乙':
        plt.text(0.6, 0.65, '白虎猖狂', fontsize=8, color='green')


    #八门
    men = jihe['門'][x]
    plt.text(0.4, 0.1, men)
    zhishi = jihe['值符值使']['值使門宮']
    if men == zhishi[0]:
        plt.text(0.25, 0.15, '值使', fontsize=8, color='red')
        #值使门入墓
        if (men == '杜' or men == '傷') and x == '坤':
            plt.text(0.27, 0.05, '入墓', fontsize=6, color='green')
        if men == '景' and x == '乾':
            plt.text(0.27, 0.05, '入墓', fontsize=6, color='green')
        if (men == '開' or men == '驚') and x == '艮':
            plt.text(0.27, 0.05, '入墓', fontsize=6, color='green')
        if (men == '休' or men == '死' or men == '生') and x == '巽':
            plt.text(0.27, 0.05, '入墓', fontsize=6, color='green')

            
            
    #九星
    xing = jihe['星'][x]
    plt.text(0.4, 0.3, xing)
    
    if xing == '禽':
        plt.text(0.5, 0.3, '芮')
    
    if xing == jihe['天乙']:
        plt.text(0.25, 0.3, '天乙', fontsize=8, color='red')
    
    #八神
    shen = jihe['神'][x]
    if shen == '勾':
        plt.text(0.4, 0.5, '虎')
    elif shen == '雀':
        plt.text(0.4, 0.5, '武')
    else:
        plt.text(0.4, 0.5, shen)
    

maxing = jihe['馬星']
kongwang = jihe['日空']
    
#巽四宫
gong4 = plt.subplot(gs[0, 0])  
plt.title("巽四宫叁肆伍捌")
paipan('巽')
if dipan == '壬' or tianpan == '壬':
    plt.text(0.05, 0.5, '壬六击')
if dipan == '癸' or tianpan == '癸':
    plt.text(0.05, 0.3, '癸六击')  
if dipan == '庚' or tianpan == '庚':
    plt.text(0.65, 0.7, '巳申刑', color='green')
    plt.text(0.75, 0.6, '无因之刑', fontsize=5)
#门迫
if men == '驚' or men == '開':
    plt.text(0.5, 0.05, '门迫', fontsize=9, color='green')
    
#马星
if maxing =='巳' or maxing =='辰':
    plt.text(0.85, 0.85, '马')

#震三宫
gong3 = plt.subplot(gs[1, 0])
plt.xlabel("震三宫叁肆捌", fontsize=8)
paipan('震')
if dipan == '戊' or tianpan == '戊':
    plt.text(0.05, 0.3, '戊六击')  

#门迫
if men == '驚' or men == '開':
    plt.text(0.5, 0.05, '门迫', fontsize=9, color='green')

#空亡
if '卯' in kongwang:
    plt.text(0.8, 0.85, '○')

#艮八宫
gong8 = plt.subplot(gs[2, 0])
plt.xlabel("艮八宫伍柒捌拾")
paipan('艮')
if dipan == '庚' or tianpan == '庚':
    plt.text(0.05, 0.3, '庚六击')

if men == '杜' or men == '傷':
    plt.text(0.5, 0.05, '门迫', fontsize=9, color='green')
#空亡
if '寅' in kongwang or '丑' in kongwang:
    plt.text(0.8, 0.85, '○')
    
if dipan == '己' or tianpan == '己':
    plt.text(0.65, 0.7, '丑戌刑', color='green')
    plt.text(0.75, 0.6, '持势之刑', fontsize=5)

#离九宫
gong9 = plt.subplot(gs[0, 1])
plt.title("离九宫贰叁柒玖")
paipan('離')
if dipan == '辛' or tianpan == '辛':
    plt.text(0.05, 0.3, '辛六击')
if men == '休':
    plt.text(0.5, 0.05, '门迫', fontsize=9, color='green')

#空亡
if '午' in kongwang:
    plt.text(0.8, 0.85, '○')

#中五宫
gong5 = plt.subplot(gs[1, 1])
plt.xticks([])
plt.yticks([])
plt.xlabel(jihe['局数'], fontsize=8)
plt.text(0.35,0.8, jihe['干支'][0:3])
plt.text(0.35,0.6, jihe['干支'][3:6])
plt.text(0.35,0.4, jihe['干支'][6:9], color='red')
plt.text(0.35,0.2, jihe['干支'][9:], color='red')
plt.text(0.02,0.85, jihe['節氣'], fontsize=9)
dipan5 = jihe['地盤']['中']
plt.text(0.85, 0.1, dipan5)

#坎一宫
gong1 = plt.subplot(gs[2, 1])
plt.xlabel("坎一宫壹陆")
paipan('坎')
if men == '死' or men == '生':
    plt.text(0.5, 0.05, '门迫', fontsize=9, color='green')
#空亡
if '子' in kongwang:
    plt.text(0.8, 0.85, '○')

#坤二宫
gong2 = plt.subplot(gs[0, 2])
plt.title("坤二宫贰伍捌拾")
paipan('坤')
#六仪击刑
if dipan == '己' or tianpan == '己':
    plt.text(0.05, 0.3, '己六击')

#门迫
if men == '杜' or men == '傷':
    plt.text(0.5, 0.05, '门迫', fontsize=9, color='green')
#空亡
if '未' in kongwang or '申' in kongwang:
    plt.text(0.8, 0.85, '○')

#兑七宫
gong7 = plt.subplot(gs[1, 2])
plt.xlabel("兑七宫贰肆柒玖", fontsize=8)
paipan('兌')
if men == '景':
    plt.text(0.5, 0.05, '门迫', fontsize=9, color='green')
#空亡
if '酉' in kongwang:
    plt.text(0.8, 0.85, '○')
    
#乾六宫
gong6 = plt.subplot(gs[2, 2])
plt.xlabel("乾六宫壹肆陆玖")
paipan('乾')

if men == '景':
    plt.text(0.5, 0.05, '门迫', fontsize=9, color='green')
if '戌' in kongwang or '亥' in kongwang:
    plt.text(0.75, 0.8, '○')
if maxing == '亥':
    plt.text(0.85, 0.8, '马', color='blue')  
    
plt.savefig('test', dpi=1200)
plt.show()

  效果如下: