[英]MatPlotLib text position strange behavior
I have a function to display scatter plots:我有一个显示散点图的功能:
def critics_and_users_score_corr_with_total_sales(_df, platform):
critic_score_not_na = df['critic_score'].notna() & (df['platform'] == platform)
total_sales_for_critic_score = df[critic_score_not_na]['total_sales']
critic_score = df[critic_score_not_na]['critic_score']
user_score_not_na = df['user_score'].notna() & (df['platform'] == platform)
total_sales_for_user_score = df[user_score_not_na]['total_sales']
user_score = df[user_score_not_na]['user_score']
info_bbox_props = dict(boxstyle='round', facecolor='white', alpha=0.2)
fig = plt.figure(figsize=(16, 7))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.grid(True)
ax1.set_xlim(0,25)
ax1.set_title('{} Critics score & Total sales correlation'.format(platform))
ax1.set_xlabel('Total sales')
ax1.set_ylabel('Critics score')
ax1.scatter(total_sales_for_critic_score, critic_score, s = 10)
ax1.text(0.345, 0.88, 'Corelation: {:.2f}'.format(total_sales_for_critic_score.corr(critic_score)),
transform=ax.transAxes, verticalalignment='top', horizontalalignment='right', bbox=info_bbox_props)
ax2.grid(True)
ax2.set_xlim(0,25)
ax2.set_title('{} Users score & Total sales correlation'.format(platform))
ax2.set_xlabel('Total sales')
ax2.set_ylabel('Users score')
ax2.scatter(total_sales_for_user_score, user_score, s = 10)
ax2.text(0.892, 0.88, 'Corelation: {:.2f}'.format(total_sales_for_user_score.corr(user_score)),
transform=ax.transAxes, verticalalignment='top', horizontalalignment='right', bbox=info_bbox_props)
plt.show()
I call this function further in code:我在代码中进一步调用了这个函数:
critics_and_users_score_corr_with_total_sales(df, top_platforms.index[0])
And here is the result:结果如下:
As we can see, text fields are placed outside the plots.
正如我们所看到的,文本字段放置在绘图之外。
But if I run this cell by Shift+Enter
the second and subsequent time - text the fields are placed in other location:但是,如果我通过
Shift+Enter
第二次和后续时间运行此单元格 - 文本字段将放置在其他位置:
If I restart the Kernel and run all cells, the text fields are placed outside the plot again.如果我重新启动内核并运行所有单元格,文本字段将再次放置在绘图之外。
What kind of magic is this?这是什么魔法?
Problem was in following lines:问题出在以下几行:
ax1.text(0.345, 0.88, 'Corelation: {:.2f}'.format(total_sales_for_critic_score.corr(critic_score)),
transform=ax.transAxes, verticalalignment='top', horizontalalignment='right', bbox=info_bbox_props)
My ax has a ax1
name, but I send to parameters transform=ax.transAxes
.我的 ax 有一个
ax1
名称,但我发送到参数transform=ax.transAxes
。 Obviously, ax
was declared in other place.显然,
ax
是在其他地方声明的。
My bad.我的错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.