python 具有自定义颜色的散点图的 Matplotlib 图例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1435535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib Legend for Scatter with custom colours
提问by PhoebeB
I'm a bit of newbie at this and am trying to create a scatter chart with custom bubble sizes and colours. The chart displays fine but how do I get a legend saying what the colours refer to. This is as far as I've got:
我在这方面有点新手,正在尝试创建一个带有自定义气泡大小和颜色的散点图。图表显示良好,但我如何获得说明颜色所指内容的图例。就我而言,这是:
inc = []
out = []
bal = []
col = []
fig=Figure()
ax=fig.add_subplot(111)
inc = (30000,20000,70000)
out = (80000,30000,40000)
bal = (12000,10000,6000)
col = (1,2,3)
leg = ('proj1','proj2','proj3')
ax.scatter(inc, out, s=bal, c=col)
ax.axis([0, 100000, 0, 100000])
ax.set_xlabel('income', fontsize=20)
ax.set_ylabel('Expenditure', fontsize=20)
ax.set_title('Project FInancial Positions %s' % dt)
ax.grid(True)
canvas=FigureCanvas(fig)
response=HttpResponse(content_type='image/png')
canvas.print_png(response)
This thread was helpful, but couldn't get it to solve my problem: Matplotlib: Legend not displayed properly
该线程很有帮助,但无法解决我的问题:Matplotlib:图例未正确显示
采纳答案by wierob
Maybe this exampleis helpful.
也许这个例子有帮助。
In general, the items in the legend are related with some kind of plotted object. The scatter
function/method treats all circles as a single object, see:
一般来说,图例中的项目与某种绘图对象有关。该scatter
函数/方法将所有圆视为一个对象,请参阅:
print type(ax.scatter(...))
Thus the solution is to create multiple objects. Hence, calling scatter
multiple times.
因此,解决方案是创建多个对象。因此,scatter
多次调用。
Unfortunately, newer version of matplotlib seem not to use a rectangle in the legend. Thus the legend will contain very large circles, since you increased the size of your scatter plot objects.
不幸的是,较新版本的 matplotlib 似乎没有在图例中使用矩形。因此,图例将包含非常大的圆圈,因为您增加了散点图对象的大小。
The legend function as a markerscale
keyword argument to control the size of legend markers, but it seems to be broken.
图例函数作为markerscale
关键字参数来控制图例标记的大小,但它似乎被破坏了。
Update:
更新:
The Legend guiderecommends using Proxy Artistin similar cases. The Color APIexplains valid fc
values.
该传说指南建议使用代理艺术家在类似案件。该颜色API说明有效的fc
值。
p1 = Rectangle((0, 0), 1, 1, fc="b")
p2 = Rectangle((0, 0), 1, 1, fc="g")
p3 = Rectangle((0, 0), 1, 1, fc="r")
legend((p1, p2, p3), ('proj1','proj2','proj3'))
To get the colors used previously in a plot, use the above example like:
要获取以前在绘图中使用的颜色,请使用上面的示例,例如:
pl1, = plot(x1, y1, '.', alpha=0.1, label='plot1')
pl2, = plot(x2, y2, '.', alpha=0.1, label='plot2')
p1 = Rectangle((0, 0), 1, 1, fc=pl1.get_color())
p2 = Rectangle((0, 0), 1, 1, fc=pl2.get_color())
legend((p1, p2), (pl1.get_label(), pl2.get_label()), loc='best')
This example will make a plot like:
此示例将绘制如下图:
回答by Nope
Have a look into this:
看看这个:
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.legend
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.legend
Hope that helps. If not just ask for more :)
希望有帮助。如果不只是要求更多:)