小编Gua*_* Li的帖子

如何处理协同程序函数的多个结果?

我有一些生成器做一些搜索的东西,我使用另一个生成器包装它们:

def searching_stuff_1():
    # searching
    yield 1
    # and searching
    yield 2
    yield 3

def searching_stuff_2():
    yield 4
    yield 5


def gen():
    yield from searching_stuff_1()
    yield from searching_stuff_2()

for result in gen():
    print(result)
Run Code Online (Sandbox Code Playgroud)

所以现在我想知道如何将其重写为异步版本,这可以在searching_stuff_1和searching_stuff_2中产生多个值.

我在努力:

import asyncio

async def searching_stuff_1():
    f = asyncio.Future()
    result = []
    await asyncio.sleep(1)
    #searching
    result.append(1)
    #searching
    result.append(2)
    result.append(3)
    f.set_result(result)
    return f

async def searching_stuff_2():
    f = asyncio.Future()
    result = []
    await asyncio.sleep(1)
    result.append(4)
    result.append(5)
    f.set_result(result)
    return f

async def producer():
    coros = [searching_stuff_1(), searching_stuff_2()]
    for …
Run Code Online (Sandbox Code Playgroud)

python asynchronous generator coroutine python-3.x

8
推荐指数
1
解决办法
665
查看次数

在matplotlib图例中使用文本但不使用标记

我在我的图表中使用FontAwesome,每个数据点都是FontAwesome字体中的符号,显示为图标.因此,在图例中,我想使用文本(FontAwesome中的符号)来描述项目.

我使用的代码如下:

from matplotlib.patches import Patch
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

ax = plt.gca()
ax.axis([0, 3, 0, 3])
prop = fm.FontProperties(fname='FontAwesome.otf', size=18)
ax.text(x=0, y=0, s='\uf1c7', color='r', fontproperties=prop)
ax.text(x=2, y=0, s='\uf050', color='g', fontproperties=prop)

plt.legend(handles=[Patch(color='r', label='label1'), Patch(color='g', label='label2')])
Run Code Online (Sandbox Code Playgroud)

情节如下: 在此输入图像描述

所以我想要做的是将图例中的颜色条替换为与图中相同的图标.

我使用的句柄是补丁列表.但我发现在Patch中添加文本很难.我发现这里有一个很好的解决方案可以将图片添加到图例中:在matplotlib图例中插入图片

我已尝试在该答案中使用TextArea替换BboxImage,但它不起作用,TextArea不支持像axis.text这样的fontproperties.

那么有没有一种方法可以在图例中使用文字而不是标记?

python visualization matplotlib

4
推荐指数
1
解决办法
576
查看次数