气象绘图加强版(二十五)——辅助线型
本节提要:主要讲解matplotlib日常绘制中常用的辅助线型系统。
fig=plt.figure(figsize=(4,3),dpi=550)
ax=fig.add_axes([0,0,1,1])
ax.plot(np.arange(0,30+31,1),df['日降水量(mm)'][0:30+31],lw=1,c='k',label='2018')
ax.plot(np.arange(0,30+31,1),df['日降水量(mm)'][30+31:30+31+31+30],lw=1,c='tab:blue',label='2019')
ax.plot(np.arange(0,30+31,1),df['日降水量(mm)'][30+31+31+30:],lw=1,c='tab:orange',label='2020')
ax.legend()
ax.set_xlabel('日期')
ax.set_ylabel('降雨量(mm)')
ax.axhline(y=30,c='r',ls='-',lw=1)
ax.text(-1,31,'30mm',color='r')
ax.axhspan(0,9.9,facecolor='#A6F28F')
ax.axhspan(9.9,25,facecolor='#3DBA3D')
ax.axhspan(25,50,facecolor='#61BBFF')
ax.axhspan(50,100,facecolor='#0000FF')
ax.axhspan(100,250,facecolor='#FA00FA')
ax.axvspan(20,40,facecolor='grey')
from matplotlib.patches import Rectangle
rectangle=Rectangle([-10,0],90,50,facecolor='tab:blue',
alpha=0.75,
edgecolor='tab:blue',
lw=0.3,ls='-')
ax.add_patch(rectangle)
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.ticker as mticker
import numpy as np
from matplotlib.patches import ConnectionPatch
plt.rcParams['font.sans-serif']=['FangSong']
filepath=r'C:\Users\lenovo\Desktop\2018-2020降水特征.xlsx'
df=pd.read_excel(filepath)
fig=plt.figure(figsize=(4,4),dpi=550)
ax1=fig.add_axes([0,0,1,0.33],zorder=1)
ax2=fig.add_axes([0,0.33,1,0.33],zorder=1)
ax3=fig.add_axes([0,0.66,1,0.33],zorder=0)
ax1.plot(np.arange(0,30+31,1),df['日降水量(mm)'][0:30+31],lw=1,c='purple')
ax2.plot(np.arange(0,30+31,1),df['日降水量(mm)'][30+31:30+31+31+30],lw=1,c='tab:blue')
ax3.plot(np.arange(0,30+31,1),df['日降水量(mm)'][30+31+31+30:],lw=1,c='tab:orange')
########################################################################
ax3.set_xticks(np.arange(0,61,30))
ax3.xaxis.tick_top()
ax3.spines['left'].set_bounds(25,125)
ax3.spines['top'].set_bounds(0,60)
ax3.spines['right'].set_visible(False)
ax3.spines['bottom'].set_visible(False)
ax3.yaxis.set_major_locator(mticker.FixedLocator([25,50,75,100,125]))
ax3.tick_params(axis='y',which='major',direction='in')
#######################################################################
ax2.spines['top'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.spines['right'].set_bounds(0,40)
ax2.yaxis.set_major_locator(mticker.FixedLocator([0,20,40]))
ax2.tick_params(axis='y',which='major',direction='in')
ax2.yaxis.set_ticks_position('right')
ax2.set_xticks([])
ax2.spines['bottom'].set_visible(False)
####################################################################
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['left'].set_bounds(0,30)
ax1.yaxis.set_major_locator(mticker.FixedLocator([0,10,20,30]))
ax1.tick_params(axis='y',which='major',direction='in')
ax1.set_xlabel('日期')
ax1.set_xticks([0,30,60])
ax1.set_xticklabels(['6月1日','7月1日','8月1日'])
ax1.spines['bottom'].set_bounds(0,60)
##################################################################
con = ConnectionPatch(xyA=(20,-1.9), coordsA=ax1.transData,
xyB=(20,148), coordsB=ax3.transData)
con.set_color('grey')
ax3.add_artist(con)
con.set_linewidth(50)
con.set_capstyle('butt')
con.set_alpha(0.5)
#################################################################
con2 = ConnectionPatch(xyA=(50,-1.9), coordsA=ax1.transData,
xyB=(50,148), coordsB=ax3.transData)
con2.set_color('grey')
ax3.add_artist(con2)
con2.set_linewidth(50)
con2.set_capstyle('butt')
con2.set_alpha(0.5)
for ax in[ax1,ax2]:
ax.patch.set_visible(False)
################################################
ax3.set_ylabel('降雨量')
ax2.text(68,20,'降雨量',rotation=90)
ax1.set_ylabel('降雨量')
plt.show()
赞 (0)