使用值在 Python 中绘制图表
Plotting a graph in Python with Values
我想在图表上的箭头上显示诸如“Broader”、“Narrower”之类的值,但我不知道该怎么做。我已经阅读了以前的帖子,例如 NetworkX 的 how to draw directed graphs using networkx in python? and read the documentation (https://networkx.org/documentation/latest/_downloads/networkx_reference.pdf),但我无法做到这一点。
下面提到了我的代码;
# libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# Build a dataframe with your connections
df = pd.DataFrame({ 'from':['Plankton', 'Plankton', 'Plankton', 'Plankton', 'Plankton', 'Plankton', 'Plankton'], 'to':['aquatic communities', 'plankton surveys', 'zooplankton','phytoplankton', 'cryoplankton', 'nannoplankton', 'picoplankton'], 'value':['broader', 'related', 'narrower', 'narrower','narrower','narrower','narrower']})
# And I need to transform my categorical column in a numerical value typeA->1, typeB->2...
df['value']=pd.Categorical(df['value'])
df['value'].cat.codes
# Build your graph
G=nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.DiGraph(directed=True) )
# Custom the nodes:
nx.draw_networkx(G, font_size = 10, with_labels=True, arrows=True, node_color= 'skyblue', node_size= 500, width= 3.5, arrowstyle= '-|>', arrowsize= 12, edge_color=df['value'].cat.codes)
您可以使用 draw_networkx_edge_labels() 绘制边缘标签。
G = nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.DiGraph(directed=True) )
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, font_size = 10, with_labels=True, arrows=True, node_color= 'skyblue', node_size= 500, width= 3.5, arrowstyle= '-|>', arrowsize= 12, edge_color=df['value'].cat.codes)
nx.draw_networkx_edge_labels(G,pos,edge_labels=dict(zip(G.edges, df['value'].tolist())))
plt.show()
它有额外的可选参数label_pos
供您调整标签在边缘的位置。
- 0: 头
- 0.5: 中心
- 1: 尾巴
也支持0、1之间的其他浮点数。
我想在图表上的箭头上显示诸如“Broader”、“Narrower”之类的值,但我不知道该怎么做。我已经阅读了以前的帖子,例如 NetworkX 的 how to draw directed graphs using networkx in python? and read the documentation (https://networkx.org/documentation/latest/_downloads/networkx_reference.pdf),但我无法做到这一点。
下面提到了我的代码;
# libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# Build a dataframe with your connections
df = pd.DataFrame({ 'from':['Plankton', 'Plankton', 'Plankton', 'Plankton', 'Plankton', 'Plankton', 'Plankton'], 'to':['aquatic communities', 'plankton surveys', 'zooplankton','phytoplankton', 'cryoplankton', 'nannoplankton', 'picoplankton'], 'value':['broader', 'related', 'narrower', 'narrower','narrower','narrower','narrower']})
# And I need to transform my categorical column in a numerical value typeA->1, typeB->2...
df['value']=pd.Categorical(df['value'])
df['value'].cat.codes
# Build your graph
G=nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.DiGraph(directed=True) )
# Custom the nodes:
nx.draw_networkx(G, font_size = 10, with_labels=True, arrows=True, node_color= 'skyblue', node_size= 500, width= 3.5, arrowstyle= '-|>', arrowsize= 12, edge_color=df['value'].cat.codes)
您可以使用 draw_networkx_edge_labels() 绘制边缘标签。
G = nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.DiGraph(directed=True) )
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, font_size = 10, with_labels=True, arrows=True, node_color= 'skyblue', node_size= 500, width= 3.5, arrowstyle= '-|>', arrowsize= 12, edge_color=df['value'].cat.codes)
nx.draw_networkx_edge_labels(G,pos,edge_labels=dict(zip(G.edges, df['value'].tolist())))
plt.show()
它有额外的可选参数label_pos
供您调整标签在边缘的位置。
- 0: 头
- 0.5: 中心
- 1: 尾巴
也支持0、1之间的其他浮点数。