matplotlib的图例(scatterplot) [英] Legends with matplotlib (scatterplot)

查看:82
本文介绍了matplotlib的图例(scatterplot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看第二个图这里,我目前正在使用散点图 并且当我绘制图例时,我得到两个样本点"(在链接中它们是两颗蓝星).我怎样才能只显示其中之一?为什么matplotlib会绘制其中的2个?

Look at the second figure here, I'm currently working with a scatterplot and when I plot the legend I get two "sample points" (in the link they are the two blue stars). How can I show just one of them? Why does matplotlib plot 2 of them?

我使用的代码类型是

plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), mode="expand", borderaxespad=0.)

我对此并不陌生,所以我仍然不使用传奇句柄".

I'm new to this so I still don't use 'legend handles'.

预先感谢

推荐答案

对于散点图,您需要设置散点.

For scatter plots you need to set scatterpoints.

import matplotlib.lines as mlines
import matplotlib.pyplot as plt

plt.figure(figsize=(6, 2))

plt.subplot(1, 2, 1)
plt.scatter([1, 2, 3, 4, 5], [1, 2, 5, 3, 4])
plt.legend(['text'])

plt.subplot(1, 2, 2)
plt.scatter([1, 2, 3, 4, 5], [1, 2, 5, 3, 4])
plt.legend(['text'], scatterpoints=1)

plt.show()

这篇关于matplotlib的图例(scatterplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆