简体   繁体   English

在matplotlib中的散点图上未正确放置图例

[英]misplaced legend on a scatter plot in matplotlib

I am using matplotlib 1.10.1. 我正在使用matplotlib 1.10.1。 The following code plots a scatter plot and adds a legend in the "best" location. 以下代码绘制了散点图,并在“最佳”位置添加了图例。

import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(1.1,2.8)
for i in range(0, 5):
    plt.scatter(x, x, label='Scatter%d' % i)
#plt.plot(x, x)
#plt.legend(loc='upper left')
plt.legend(loc='best')
plt.show()

However, the legend appears in the upper right corner covering some points. 但是,图例显示在右上角,覆盖了某些点。 I was expecting it to appear in the upper left corner where there is no data. 我期望它会出现在没有数据的左上角。 Plotting a line instead of a scatter plot seems to produce the expected behaviour. 绘制一条线而不是散点图似乎会产生预期的行为。

Is there a way to fix the "best" legend location for scatter plots? 有没有办法解决散点图的“最佳”图例位置?

Thanks 谢谢

I think you have encountered this issue and the patch suggested in the thread there has not made it into the master branch. 我认为您已经遇到了这个问题 ,那里的线程中建议的补丁还没有进入master分支。

So I think your only option is to apply it manually. 因此,我认为您唯一的选择是手动应用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM