创建两个下降气泡图并根据在同一数据集中找到的值连接它们
Create two descending bubble plots and connect them based on values found in same data set
我有以下数据,我想在其中使用气泡图(较大的值具有较大的气泡)按降序在 Animal 和 Food 之间使用分别连接它们的线创建并排比较。
数据Table表格:
Animal Food
Cat Hard_Food
Cat Hard_Food
Cat Hard_Food
Cat Hard_Food
Cat Soft_Food
Cat Soft_Food
Cat Soft_Food
Cat Mouse
Cat Soft_Food
Cat Soft_Food
Dog Hard_Food
Dog Hard_Food
Dog Hard_Food
Dog Hard_Food
Dog Soft_Food
Dog Soft_Food
Dog Soft_Food
Dog Soft_Food
Dog Meat
Snake Mouse
Snake Meat
Snake Meat
Snake Meat
汇总Table形式:
Hard_Food Meat Soft_Food Mouse Grand Total
Cat 4 0 5 1 10
Dog 4 1 4 0 9
Snake 0 3 0 1 4
GrandTotal 8 4 9 2 23
Python 数据框:
ani_foo = {'Animal': ['Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Snake','Snake','Snake','Snake'],
'Food': ['Hard_Food','Hard_Food','Hard_Food','Hard_Food','Soft_Food','Soft_Food','Soft_Food','Mouse','Soft_Food','Soft_Food','Hard_Food','Hard_Food','Hard_Food','Hard_Food','Soft_Food','Soft_Food','Soft_Food','Soft_Food','Meat','Mouse','Meat','Meat','Meat']
}
df = pd.DataFrame(ani_foo, columns = ['Animal', 'Food'])
期望的输出(通过Excel手动创建):
您可以尝试使用在二分网络上创建的 networkx 库:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
ani_foo = {'Animal': ['Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Snake','Snake','Snake','Snake'],
'Food': ['Hard_Food','Hard_Food','Hard_Food','Hard_Food','Soft_Food','Soft_Food','Soft_Food','Mouse','Soft_Food','Soft_Food','Hard_Food','Hard_Food','Hard_Food','Hard_Food','Soft_Food','Soft_Food','Soft_Food','Soft_Food','Meat','Mouse','Meat','Meat','Meat']
}
df = pd.DataFrame(ani_foo, columns = ['Animal', 'Food'])
fig, ax = plt.subplots(figsize=(15,8))
G = nx.from_pandas_edgelist(df, 'Animal', 'Food')
G.add_nodes_from(df['Animal'], bipartite=0)
G.add_nodes_from(df['Food'], bipartite=1)
s = df.stack().value_counts()
s1 = s.index +'\n'+ s.astype(str)
pos = {node:[0, i] for i, node in enumerate(df['Animal'])}
pos.update({node:[1,i] for i, node in enumerate(df['Food'])})
color_dict = {'Cat':'g', 'Dog':'b', 'Snake':'y'}
ec = [color_dict[c] for i in G.edges for c in i if c in color_dict.keys()]
nx.draw_networkx(G,
node_size=[s[i]*250 for i in G.nodes],
pos=pos,
labels = s1.to_dict(),
node_color='lightblue',
edge_color=ec)
plt.axis('off')
输出:
我有以下数据,我想在其中使用气泡图(较大的值具有较大的气泡)按降序在 Animal 和 Food 之间使用分别连接它们的线创建并排比较。
数据Table表格:
Animal Food
Cat Hard_Food
Cat Hard_Food
Cat Hard_Food
Cat Hard_Food
Cat Soft_Food
Cat Soft_Food
Cat Soft_Food
Cat Mouse
Cat Soft_Food
Cat Soft_Food
Dog Hard_Food
Dog Hard_Food
Dog Hard_Food
Dog Hard_Food
Dog Soft_Food
Dog Soft_Food
Dog Soft_Food
Dog Soft_Food
Dog Meat
Snake Mouse
Snake Meat
Snake Meat
Snake Meat
汇总Table形式:
Hard_Food Meat Soft_Food Mouse Grand Total
Cat 4 0 5 1 10
Dog 4 1 4 0 9
Snake 0 3 0 1 4
GrandTotal 8 4 9 2 23
Python 数据框:
ani_foo = {'Animal': ['Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Snake','Snake','Snake','Snake'],
'Food': ['Hard_Food','Hard_Food','Hard_Food','Hard_Food','Soft_Food','Soft_Food','Soft_Food','Mouse','Soft_Food','Soft_Food','Hard_Food','Hard_Food','Hard_Food','Hard_Food','Soft_Food','Soft_Food','Soft_Food','Soft_Food','Meat','Mouse','Meat','Meat','Meat']
}
df = pd.DataFrame(ani_foo, columns = ['Animal', 'Food'])
期望的输出(通过Excel手动创建):
您可以尝试使用在二分网络上创建的 networkx 库:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
ani_foo = {'Animal': ['Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Cat','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Dog','Snake','Snake','Snake','Snake'],
'Food': ['Hard_Food','Hard_Food','Hard_Food','Hard_Food','Soft_Food','Soft_Food','Soft_Food','Mouse','Soft_Food','Soft_Food','Hard_Food','Hard_Food','Hard_Food','Hard_Food','Soft_Food','Soft_Food','Soft_Food','Soft_Food','Meat','Mouse','Meat','Meat','Meat']
}
df = pd.DataFrame(ani_foo, columns = ['Animal', 'Food'])
fig, ax = plt.subplots(figsize=(15,8))
G = nx.from_pandas_edgelist(df, 'Animal', 'Food')
G.add_nodes_from(df['Animal'], bipartite=0)
G.add_nodes_from(df['Food'], bipartite=1)
s = df.stack().value_counts()
s1 = s.index +'\n'+ s.astype(str)
pos = {node:[0, i] for i, node in enumerate(df['Animal'])}
pos.update({node:[1,i] for i, node in enumerate(df['Food'])})
color_dict = {'Cat':'g', 'Dog':'b', 'Snake':'y'}
ec = [color_dict[c] for i in G.edges for c in i if c in color_dict.keys()]
nx.draw_networkx(G,
node_size=[s[i]*250 for i in G.nodes],
pos=pos,
labels = s1.to_dict(),
node_color='lightblue',
edge_color=ec)
plt.axis('off')
输出: