Python,matplotlib - 基于条件变量的图例 -

标签 python matplotlib scatter-plot

我有一个数据,可以用在散点图中。

我也有相同数据的标签。所以我正在使用条件着色:

# import needed things
fig = plt.figure()
r = fig.add_subplot(121)
r.scatter(np.arange(500), X[ :500, 0] c = Y[:500]
# x and y labels set here

g = fig.add_subplot(122)
g.scatter(np.arange(500), X[ :500, 1] c = Y[:500]
# x and y labels set here

plt.show()    

我还需要一个图例,提示哪种类型有哪种颜色。我试过这个:

plt.legend((r, g), ("one", "zero"), scatterpoints = 1, loc = "upper left")

但是我收到警告

.../site-packages/matplotlib/legend.py:633: UserWarning: Legend does not support <matplotlib.axes._subplots.AxesSubplot object at 0x7fe37f460668> instances.
A proxy artist may be used instead.

并且不显示图例。

最佳答案

我可以通过替换来运行你的代码

r.scatter(np.arange(500), np.arange(500), c= np.arange(500))
g.scatter(np.arange(500), np.arange(500), c= np.arange(500))

我遇到了一个类似的错误,将我指向 matplotlib.org 上的一个页面,见下文:

/Users/sdurant/anaconda/lib/python2.7/site-packages/matplotlib/legend.py:611: UserWarning: Legend 不支持实例。 可以改用代理艺术家。 请参阅:http://matplotlib.org/users/legend_guide.html#using-proxy-artist “#using-proxy-artist”.format(orig_handle))

我不太明白您希望您的图例看起来像什么,但该页面有几种类型的示例,希望对您有所帮助。

编辑:这更有意义,这就是您正在寻找的大致内容吗?

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

red_patch = mpatches.Patch(color='red', label='one')
blue_patch = mpatches.Patch(color='blue', label='zero')

area = np.pi *30
fig = plt.figure()
r = fig.add_subplot(121)
r.scatter(np.arange(10), np.arange(10), c= [random.randint(2) for k in range(10)], s=area)
# x and y labels set here
plt.legend(handles=[red_patch,blue_patch])
g = fig.add_subplot(122)
g.scatter(np.arange(10), np.arange(10), c= [random.randint(2) for k in range(10)], s=area)
# x and y labels set here

plt.legend(handles=[red_patch,blue_patch])

enter image description here

关于Python,matplotlib - 基于条件变量的图例 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35313489/

相关文章:

python - 清理 python 切片?

python - Wonder-twin power "Zoom to rectangle"激活?

javascript - Highcharts 网格线宽度(或颜色)- 单线

arrays - 时间与风速的误差散点图(数据来自表格)MATLAB

python - 类型错误 : delete_dog() missing 1 required positional argument: 'Dog_id' How do I get around this?

python - 等待 shutil.copyfile 完成

python - 绘制存储在 panda 数据框中已排序的直方图数据

python - pylab/networkx;更新后没有显示节点标签

d3.js - d3 更改和更新 Axis 域散点图

python - Pandas :更改具有多级列的数据框中的特定列名