Skip to content

Instantly share code, notes, and snippets.

@Perishleaf
Created January 11, 2020 05:29
Show Gist options
  • Save Perishleaf/5e5bf65c0647bc44693795b032702b14 to your computer and use it in GitHub Desktop.
Save Perishleaf/5e5bf65c0647bc44693795b032702b14 to your computer and use it in GitHub Desktop.
def draw_linechart(Num_Year):
'''
This is the function to make a plot
'''
# since many figures will be generated for animation, old axes need to be cleared before drawing new axes
ax.clear()
ax2.clear()
# select data till the indicated year
df_tmp=df_sydney.loc[df_sydney['Year'] <= Num_Year]
# The frame of the whole figure, use the full data
# excluding from the automatic legend element selection by
# defining a label starting with an underscore.
ax.plot(df_sydney.Year, df_sydney.max_tmp_year*0, label='_1', color='#222222', zorder=1)
# The horizontal line to indicate 40 degree, use the full data
ax.plot(df_sydney.Year, (df_sydney.max_tmp_year**0)*40, label='_1', color='#fc280f', linestyle='dashed', alpha=0.1, linewidth=0.5, zorder=1)
# The horizontal line to indicate 20 days, use the full data
ax2.plot(df_sydney.Year, (df_sydney.hot_day**0)*20, label='_2', color='#eeb72b', linestyle='dashed', alpha=0.1, linewidth=0.5, zorder=1)
# line plot
ax.plot(df_tmp.Year, df_tmp.max_tmp_year, label='Yearly max. temp. ($^\circ$C)',
color='#fc280f', zorder=3)
ax2.plot(df_tmp.Year, df_tmp.hot_day, label='Yearly accumulated days above 30$^\circ$C',
color='#eeb72b', zorder=3)
# dot at the end of the line
ax.scatter(df_tmp.Year.values[-1], df_tmp.max_tmp_year.values[-1], color='#fc280f', s=60, edgecolor ='w', linewidth=1, zorder=4)
ax2.scatter(df_tmp.Year.values[-1], df_tmp.hot_day.values[-1], color='#eeb72b', s=60, edgecolor ='w', linewidth=1, zorder=4)
# text associated with the dot in the figure
ax.text(df_tmp.Year.values[-1], df_tmp.max_tmp_year.values[-1]+5,
s='Temp. {}'.format(df_tmp.max_tmp_year.values[-1]), size=10,
color='#fc280f', ha='center', va='bottom', zorder=5)
ax2.text(df_tmp.Year.values[-1], df_tmp.hot_day.values[-1]+0.7,
s='{} Days'.format(int(df_tmp.hot_day.values[-1])), size=10,
color='#eeb72b', ha='center', va='bottom', zorder=5)
# display year in the background
ax.text(0.5, 0.5, s=df_tmp.Year.values[-1], fontweight='bold', size=60,
color='#e3e3e3', ha='center', va="center",
alpha=0.5, transform=ax.transAxes, zorder=2)
# legend for red line
l1 = ax.legend(loc='upper left')
for text in l1.get_texts():
text.set_color('#e3e3e3')
# legend for yellow line
l2 = ax2.legend(loc='upper right')
for text in l2.get_texts():
text.set_color('#e3e3e3')
# set the y-axis to have enough space for other elements, such as legends
ax.set_ylim(0, df_sydney.max_tmp_year.max()*1.68)
# do not show the grid in the background
ax.grid(b=None)
ax.set_ylabel('Temperature ($^\circ$C)', color='#e3e3e3')
ax2.grid(b=None) # Don't show grid of ax2
ax2.set_ylim(df_tmp.hot_day.min()-5, df_tmp.hot_day.max()*1.5)
ax2.set_ylabel('Day', color='#e3e3e3')
# set color for axes frame
ax2.spines['bottom'].set_color('#e3e3e3')
ax2.spines['right'].set_color('#e3e3e3')
ax2.spines['left'].set_color('#e3e3e3')
ax2.spines['top'].set_color('#e3e3e3')
# Retrieve an element of a plot and set properties
for tick in ax.xaxis.get_ticklabels():
tick.set_fontsize('x-small')
ax.margins(x=0.05)
ax.text(0.00, 1.05, 'Sydney Annual Maximum Temperature From 1859 to 2019', color='#e3e3e3',
transform=ax.transAxes, size=17, weight='light', ha='left')
ax.text(0.00, -0.1, 'Data source from http://www.bom.gov.au', color='#e3e3e3',
transform=ax.transAxes, size=5, weight='light', ha='left')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment