I'm plotting NYC water consumption overtime and am trying to annotate the plot with red heat wave images hovering over each year where there was a drought. However, the heat wave image is layered beneath the plot and is being clipped.
How can I "bring forward" the heatwave image so that is fully visible and not being clipped?
Here is my code:
# create the plot
fig, ax = plt.subplots(figsize=(11, 8))
# plot total consumption as a vertical bar chart
ax1 = fig.add_subplot(111)
bar = sns.barplot(x = df['year'], y = df['nyc_consumption_million_gallons_per_day'], color = 'b')
ax1.set_ylim([600, 1600])
ax1.grid(False)
ax1.set_ylabel('Total Consumption (Millions of Gallons per Day)', fontsize=11)
ax1.set_xlabel('')
# plot per capital consumption as a line
ax2 = ax1.twinx()
line = plt.plot(df['per_capita_gallons_per_person_per_day'], color='g')
ax2.set_ylim(100,240)
ax2.grid(False)
ax2.set_ylabel('Per Capita Consumption (Gallons per Person per Day)', fontsize=11)
# design properties
ax.get_yaxis().set_visible(False) # removes y axis from underlying figure
ax.get_xaxis().set_visible(False) # removes x axis from underlying figure
# make a legend
leg1 = plt.Rectangle((0,0),1,1,fc='b', edgecolor='none')
leg2 = plt.Rectangle((0,0),1,1,fc='g', edgecolor='none')
l = plt.legend([leg1, leg2], ['Total Consumption', 'Per Capita Consumption'],
bbox_to_anchor=(1.0,1.01), ncol = 4, prop={'size':14})
# add title
title = ax.annotate("Water Consumption in New York City is Decreasing",
(0,0), (-75, 530), textcoords='offset points', color='gray', fontsize=26, fontweight='heavy')
# add subtitle
sub = ax.annotate("History of average daily water consumption in the New York City Water Supply System",
(0,0), (-75, 505), textcoords='offset points', color='gray', fontsize=16, style='italic')
# add heatwave symbols on top of years that had a drought
arr_hand = read_png(r"C:UsersWillDownloadsheat-symbol-hi.png")
imagebox = OffsetImage(arr_hand, zoom=0.9)
xy = [0.25, 0.45] # coordinates to position this image
ab = AnnotationBbox(imagebox, xy,
xybox=(30., 30.),
xycoords='data',
boxcoords="offset points", frameon=False)
ax.add_artist(ab)
Here is the result: plot
Aucun commentaire:
Enregistrer un commentaire