如何避免在matplotlib子图中剪切AnnotationBbox图像

时间:2016-06-19 23:16:43

标签: python matplotlib

我正在绘制纽约市的水消费加班时间,并试图用红色的热浪图像来诠释这个地块,每年都有一个干旱的地方徘徊。但是,热波图像位于图表下方并被剪裁。

如何“提前”热波图像,使其完全可见并且不会被剪裁?

这是我的代码:

# 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:\Users\Will\Downloads\heat-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)

结果如下: plot

0 个答案:

没有答案