简体   繁体   中英

PyGraphviz draws networkx graph labels as boxes

I am using pygraphviz to draw a networkx graph. Unfortunately, the node and edge labels are displayed as boxes. I am drawing the graph as follows:

        G.graph['edge'] = {'arrowsize': '0.6', 'splines': 'curved'}
        G.graph['graph'] = {'scale': '3'}

        for a, b, data in G.edges(data=True):
            data['label'] = str(data['edge_info'])
        for _, data in G.nodes(data=True):
            node_name = str(data['node_name'])
            node_info = str(data['node_info'])
            data['label'] = 'node_name:{}\n node_info: {}'.format(node_name, node_info)

        A = to_agraph(G)
        A.layout('dot')
        file_name = file_name + '.' + file_format
        A.draw(path.join(out_folder, file_name), format=file_format)

The code gets executed in a Docker Container running Debian. The dockerfile looks as follows:

FROM abc/broker-docker:1.3.3

# These should be build-deps and could be uninstalled after pip3 install
RUN apk add --no-cache g++ python3-dev graphviz-dev

RUN pip3 install --upgrade \
    setuptools \
    wheel

WORKDIR /opt/graph_drawer
ENV PYTHONPATH $PYTHONPATH:/opt/graph_drawer

COPY broker_base ./broker_base
COPY elasticsearch_client ./elasticsearch_client
COPY graph_drawer/* ./

RUN pip3 install -r elasticsearch_client/requirements.txt \
    && pip3 install -r requirements.txt

CMD ["python3", "-u", "graph_drawer.py", "./config.json"]

The following graph gets drawn:

在此处输入图片说明

This is clearly not the intended result, but I do not know what I have done wrong... maybe someone of you knows it, any help appreciated!

I managed to reproduce your error.

Seems like you have problem with fonts that are not installed on Docker Alpine/Debian image.

You can install ttf-freefont with apk add :

RUN apk add --no-cache ttf-freefont

Before :

在此处输入图片说明

After :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM