Python、matplotlib - 基于条件变量的图例 -
Python, matplotlib - legend based on a conditional variable -
我有一个数据,可以用来做散点图。
我也有相同数据的标签。所以我正在使用条件着色:
# 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:用户警告:图例不支持实例。
可以改用代理艺术家。
参见: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])
我有一个数据,可以用来做散点图。
我也有相同数据的标签。所以我正在使用条件着色:
# 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:用户警告:图例不支持实例。 可以改用代理艺术家。 参见: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])