用于具有自定义颜色的散点图的 Matplotlib 图例

发布于 2024-08-05 09:18:27 字数 751 浏览 1 评论 0 原文

我对此有点新手,正在尝试创建具有自定义气泡大小和颜色的散点图。图表显示良好,但我如何获得说明颜色所指含义的图例。据我所知:

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)

该线程很有帮助,但无法解决我的问题: Matplotlib:图例未正确显示

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2024-08-12 09:18:27

也许这个示例很有帮助。

一般来说,图例中的项目与某种绘制对象相关。 scatter 函数/方法将所有圆视为单个对象,请参阅:

print type(ax.scatter(...))

因此解决方案是创建多个对象。因此,多次调用scatter

不幸的是,新版本的 matplotlib 似乎没有在图例中使用矩形。因此,图例将包含非常大的圆圈,因为您增加了散点图对象的大小。

legend 函数作为 markerscale 关键字参数来控制图例标记的大小,但它似乎被破坏了。

更新:

图例指南建议使用代理艺术家Color 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'))

要获取先前在绘图中使用的颜色,请使用上面的示例,例如:

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')

此示例将制作如下绘图:

Maybe this example is 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:

print type(ax.scatter(...))

Thus the solution is to create multiple objects. Hence, calling scatter multiple times.

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.

The legend function as a markerscale keyword argument to control the size of legend markers, but it seems to be broken.

Update:

The Legend guide recommends using Proxy Artist in similar cases. The Color API explains valid fc values.

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:

Matplotlib with custom legend

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文