matplotlib学习笔记(7)—双y轴图
import matplotlib as mpl
import matplotlib. pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
from matplotlib. lines import Line2D
import matplotlib. patches as patches
import pandas as pd
import seaborn as sns
import matplotlib. pyplot as plt
mpl. rc( 'font' , family= 'Times New Roman' )
fig, ax = plt. subplots( figsize= ( 15 , 7 ) )
ax_cur = ax
ax. set_axisbelow( True )
ax_cur. grid( ls= '-.' , axis= 'y' )
ax_cur. bar( df_avg[ 'Year' ] , df_avg[ 'Year_aver_Precip' ] , color= '#6699CC' )
ax_cur. set_ylim( 150 , 200 )
ax_cur. set_xticks( range ( 2001 , 2021 , 1 ) )
ax_cur. set ( ylim= ( 150 , 200 ) , ylabel= 'precipitation(mm)' )
ax_cur. spines[ 'top' ] . set_visible( False )
ax_cur. spines[ 'right' ] . set_visible( False )
ax_cur. set_xlabel( 'Year' )
ax_cur. set_ylabel( "precipitation(mm)" , fontsize = 20 )
ax_cur. set_xlabel( 'Year' , fontsize = 20 )
axR = ax_cur. twinx( )
h= sns. lineplot( x= 'Year' , y= 'Year_aver_Temper' , data= df_avg, ax= axR, color= '#990033' , marker= 'o' , markersize = 10 )
h. spines[ 'top' ] . set_visible( False )
axR. set_yticks( np. arange( 23.5 , 30.5 , 0.5 ) )
axR. set ( ylabel= 'Temperature(℃)' )
axR. set_ylabel( "Temperature(℃)" , fontsize = 20 )
legend_elements = [
patches. Rectangle( ( 0 , 0 ) , 1 , 1 , facecolor= '#6699CC' , label= 'precipitation' ) ,
Line2D( [ ] , [ ] , marker= 'o' , color= '#990033' , label= 'Temperature(℃)' , ls= '-' )
]
h. legend( handles= legend_elements, loc= 'upper left' )