Forum Discussion
Slicers as input for Python script
- 6 years ago
Hey jmarcrum ,
this code is maybe doing what you are looking for:
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: # dataset = pandas.DataFrame(TmName, OppName, Selection1, Selection2) # dataset = dataset.drop_duplicates() # Paste or type your script code here: import networkx import matplotlib.pyplot as plt tm1_l = list(dataset.Selection1) tm1 = tm1_l[1] tm2_l = list(dataset.Selection2) tm2 = tm2_l[1] # load edges file from csv left = list(dataset.TmName) right = list(dataset.OppName) graph = networkx.Graph() graph.add_edges_from(list(zip(left, right))) # Test single path shortest_path = networkx.shortest_path(graph, tm1, tm2) edges_2team = dataset[dataset.TmName.isin(shortest_path)] left_2team= list(edges_2team['TmName']) right_2team = list(edges_2team['OppName']) graph_2team = networkx.Graph() graph_2team.add_edges_from(list(zip(left_2team, right_2team))) plt.title(tm1 + " " + tm2) networkx.draw(graph_2team, with_labels=True) plt.show()as it allows to create this chart:
Be aware of this line:
networkx.draw(graph_2team, with_labels=True)
Hopefully, this is what you are looking for.Regards,
Tom
Hey jmarcrum ,
I couldn't resist 🙂
this code
# Paste or type your script code here:
import networkx
import matplotlib.pyplot as plt
tm1_l = list(dataset.Selection1)
tm1 = tm1_l[1]
tm2_l = list(dataset.Selection2)
tm2 = tm2_l[1]
# load edges file from csv
left = list(dataset.TmName)
right = list(dataset.OppName)
graph = networkx.Graph()
graph.add_edges_from(list(zip(left, right)))
# Test single path
# shortest_path = networkx.shortest_path(graph, tm1, tm2)
#
#edges_2team = dataset[dataset.TmName.isin(shortest_path)]
#left_2team= list(edges_2team['TmName'])
#right_2team = list(edges_2team['OppName'])
#graph_2team = networkx.Graph()
#graph_2team.add_edges_from(list(zip(left_2team, right_2team)))
plt.title(tm1 + " " + tm2)
networkx.draw(graph, with_labels=False)
plt.show()
Creates this chart (the green rectangles are added using snagit 🙂
As you can see the measures are working to select something.
Please be aware that my code above is transforming the dataframe columns to a list, to select the 1st value to create the title.
I'm using plt.title as .draw() turns off the axis, where .draw_newtworkx() is not doing this.
Hopefully, this provides some ideas about what is causing the issues
Regards,
Tom
Hey jmarcrum ,
this code is maybe doing what you are looking for:
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# dataset = pandas.DataFrame(TmName, OppName, Selection1, Selection2)
# dataset = dataset.drop_duplicates()
# Paste or type your script code here:
import networkx
import matplotlib.pyplot as plt
tm1_l = list(dataset.Selection1)
tm1 = tm1_l[1]
tm2_l = list(dataset.Selection2)
tm2 = tm2_l[1]
# load edges file from csv
left = list(dataset.TmName)
right = list(dataset.OppName)
graph = networkx.Graph()
graph.add_edges_from(list(zip(left, right)))
# Test single path
shortest_path = networkx.shortest_path(graph, tm1, tm2)
edges_2team = dataset[dataset.TmName.isin(shortest_path)]
left_2team= list(edges_2team['TmName'])
right_2team = list(edges_2team['OppName'])
graph_2team = networkx.Graph()
graph_2team.add_edges_from(list(zip(left_2team, right_2team)))
plt.title(tm1 + " " + tm2)
networkx.draw(graph_2team, with_labels=True)
plt.show()
as it allows to create this chart:
Be aware of this line:
networkx.draw(graph_2team, with_labels=True)
Hopefully, this is what you are looking for.
Regards,
Tom
- Rohan2313 years agoRegular Visitor
Hey, what are Selection1 and Selection2? Are they column names or something else?