Comments (2)
Hi Eric,
Thank you for this awesome nxviz package.
I have a bipartite graph of "technical-tickets" and "teams" (that work on tickets) as nodes. So "Ticket"s and "Team"s have different attributes. When I tried to Color,Order and Group the ticket-nodes based on attributes that absent in Team-nodes, faced issues due to missing keys in Team-nodes.
I have overwritten the class methods in my code with below 2-functions. you may verify and update the code as appropriate.
Below code helps :
- Coloring, grouping and ordering the nodes irrespective of missing keys in few nodes.
- Overrides the CMAP dictionary to take unlimited values of categories in any key, original one has a limit of 8-categories for any key.
def new_compute_node_colors(self):
import logging
import nxviz as nv
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import networkx as nx
from matplotlib.cm import get_cmap
from matplotlib.path import Path
from nxviz.geometry import circos_radius, get_cartesian, node_theta
from nxviz.utils import (cmaps, infer_data_type, is_data_diverging, num_discrete_groups)
"""Compute the node colors. Also computes the colorbar."""
#data = [self.graph.node[n][self.node_color] if self.node_color in self.graph.node[n].keys() else 'no_key' for n in self.nodes]
data = [self.graph.node[n][self.node_color] if self.node_color in self.graph.node[n].keys() else 'zzz_no_key' for n in self.nodes]
data_reduced = sorted(list(set(data)))
dtype = infer_data_type(data)
n_grps = num_discrete_groups(data)
if dtype == 'categorical' or dtype == 'ordinal':
#cmap = get_cmap(cmaps['Accent_{0}'.format(n_grps)].mpl_colormap)
cmap = get_cmap(cmaps['Paired_12'].mpl_colormap)
elif dtype == 'continuous' and not is_data_diverging(data):
cmap = get_cmap(cmaps['continuous'].mpl_colormap)
elif dtype == 'continuous' and is_data_diverging(data):
cmap = get_cmap(cmaps['diverging'].mpl_colormap)
for d in data:
idx = data_reduced.index(d) / n_grps
self.node_colors.append(cmap(idx))
# Add colorbar if required.
logging.debug('length of data_reduced: {0}'.format(len(data_reduced)))
logging.debug('dtype: {0}'.format(dtype))
if len(data_reduced) > 1 and dtype == 'continuous':
self.sm = plt.cm.ScalarMappable(cmap=cmap,
norm=plt.Normalize(vmin=min(data_reduced), # noqa
vmax=max(data_reduced) # noqa
)
)
self.sm._A = []
def new_group_and_sort_nodes(self):
"""
Groups and then sorts the nodes according to the criteria passed into
the Plot constructor.
"""
if self.node_grouping and not self.node_order:
#self.nodes = [n for n, d in sorted(self.graph.nodes(data=True), key=lambda x: x[1][self.node_grouping])]
self.nodes = [n for n,d in sorted([(n,self.graph.node[n][self.node_grouping]) if self.node_grouping in self.graph.node[n].keys() else (n,'zzz_no_key') for n in self.graph.nodes],key= lambda x: x[1])]
elif self.node_order and not self.node_grouping:
#self.nodes = [n for n, _ in sorted(self.graph.nodes(data=True), key=lambda x: x[1][self.node_order])]
self.nodes = [n for n, _ in sorted([(n,self.graph.node[n][self.node_order]) if self.node_order in self.graph.node[n].keys() else (n,'zzz_no_key') for n in self.graph.nodes],key= lambda x: x[1])]
elif self.node_grouping and self.node_order:
#self.nodes = [n for n, d in sorted(self.graph.nodes(data=True), key=lambda x: (x[1][self.node_grouping], x[1][self.node_order]))]
nodes = []
for n in self.nodes:
if self.node_grouping in self.graph.node[n].keys():
grouping = self.graph.node[n][self.node_grouping]
else:
grouping = 'zzz_no_key'
if self.node_order in self.graph.node[n].keys():
order = self.graph.node[n][self.node_order]
else:
order = 'zzz_no_key'
nodes.append((n,grouping,order))
self.nodes = [n for n,group,order in sorted(nodes,key= lambda x: (x[1], x[2]))]
new_cmaps = {
'Accent_2': qualitative.Accent_3,
'Accent_3': qualitative.Accent_3,
'Accent_4': qualitative.Accent_4,
'Accent_5': qualitative.Accent_5,
'Accent_6': qualitative.Accent_6,
'Accent_7': qualitative.Accent_7,
'Accent_8': qualitative.Accent_8,
'Paired_12': qualitative.Paired_12, #added colorbrewer set ref:https://jiffyclub.github.io/palettable/colorbrewer/qualitative/#paired_12
'continuous': sequential.YlGnBu_9,
'diverging': diverging.RdBu_11,
}
nv.plots.BasePlot.compute_node_colors = new_compute_node_colors
nv.plots.BasePlot.group_and_sort_nodes = new_group_and_sort_nodes
nv.utils.cmaps = new_cmaps
from nxviz.
Pasting my code again, did not paste properly above.
def new_compute_node_colors(self):
import logging
import nxviz as nv
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import networkx as nx
from matplotlib.cm import get_cmap
from matplotlib.path import Path
from nxviz.geometry import circos_radius, get_cartesian, node_theta
from nxviz.utils import (cmaps, infer_data_type, is_data_diverging, num_discrete_groups)
"""Compute the node colors. Also computes the colorbar."""
#data = [self.graph.node[n][self.node_color] if self.node_color in self.graph.node[n].keys() else 'no_key' for n in self.nodes]
data = [self.graph.node[n][self.node_color] if self.node_color in self.graph.node[n].keys() else 'zzz_no_key' for n in self.nodes]
data_reduced = sorted(list(set(data)))
dtype = infer_data_type(data)
n_grps = num_discrete_groups(data)
if dtype == 'categorical' or dtype == 'ordinal':
#cmap = get_cmap(cmaps['Accent_{0}'.format(n_grps)].mpl_colormap)
cmap = get_cmap(cmaps['Paired_12'].mpl_colormap)
elif dtype == 'continuous' and not is_data_diverging(data):
cmap = get_cmap(cmaps['continuous'].mpl_colormap)
elif dtype == 'continuous' and is_data_diverging(data):
cmap = get_cmap(cmaps['diverging'].mpl_colormap)
for d in data:
idx = data_reduced.index(d) / n_grps
self.node_colors.append(cmap(idx))
# Add colorbar if required.
logging.debug('length of data_reduced: {0}'.format(len(data_reduced)))
logging.debug('dtype: {0}'.format(dtype))
if len(data_reduced) > 1 and dtype == 'continuous':
self.sm = plt.cm.ScalarMappable(cmap=cmap,
norm=plt.Normalize(vmin=min(data_reduced), # noqa
vmax=max(data_reduced) # noqa
)
)
self.sm._A = []
def new_group_and_sort_nodes(self):
"""
Groups and then sorts the nodes according to the criteria passed into
the Plot constructor.
"""
if self.node_grouping and not self.node_order:
#self.nodes = [n for n, d in sorted(self.graph.nodes(data=True), key=lambda x: x[1][self.node_grouping])]
self.nodes = [n for n,d in sorted([(n,self.graph.node[n][self.node_grouping]) if self.node_grouping in self.graph.node[n].keys() else (n,'zzz_no_key') for n in self.graph.nodes],key= lambda x: x[1])]
elif self.node_order and not self.node_grouping:
#self.nodes = [n for n, _ in sorted(self.graph.nodes(data=True), key=lambda x: x[1][self.node_order])]
self.nodes = [n for n, _ in sorted([(n,self.graph.node[n][self.node_order]) if self.node_order in self.graph.node[n].keys() else (n,'zzz_no_key') for n in self.graph.nodes],key= lambda x: x[1])]
elif self.node_grouping and self.node_order:
#self.nodes = [n for n, d in sorted(self.graph.nodes(data=True), key=lambda x: (x[1][self.node_grouping], x[1][self.node_order]))]
nodes = []
for n in self.nodes:
if self.node_grouping in self.graph.node[n].keys():
grouping = self.graph.node[n][self.node_grouping]
else:
grouping = 'zzz_no_key'
if self.node_order in self.graph.node[n].keys():
order = self.graph.node[n][self.node_order]
else:
order = 'zzz_no_key'
nodes.append((n,grouping,order))
self.nodes = [n for n,group,order in sorted(nodes,key= lambda x: (x[1], x[2]))]
new_cmaps = {
'Accent_2': qualitative.Accent_3,
'Accent_3': qualitative.Accent_3,
'Accent_4': qualitative.Accent_4,
'Accent_5': qualitative.Accent_5,
'Accent_6': qualitative.Accent_6,
'Accent_7': qualitative.Accent_7,
'Accent_8': qualitative.Accent_8,
'Paired_12': qualitative.Paired_12, #added colorbrewer set ref:https://jiffyclub.github.io/palettable/colorbrewer/qualitative/#paired_12
'continuous': sequential.YlGnBu_9,
'diverging': diverging.RdBu_11,
}
nv.plots.BasePlot.compute_node_colors = new_compute_node_colors
nv.plots.BasePlot.group_and_sort_nodes = new_group_and_sort_nodes
nv.utils.cmaps = new_cmaps
from nxviz.
Related Issues (20)
- I have vertex.txt and triangle.txt, how to create a networkx graph? HOT 2
- Is it possible to make arcplot directional? HOT 2
- Downgrading pandas, matplotlib, networkx packages when installing nxviz HOT 4
- Broken example in new API documentation
- version mismatch on packages distributed by conda and pypi HOT 3
- Not an issue but a veli easy question
- Having trouble re-creating graph HOT 1
- Feature Request: Edge direction by source or target node colors. HOT 6
- Feature Request: Node label layout for circus plot HOT 2
- Question regarding parameters when creating MatrixPlot or CircosPlot HOT 6
- Changing the default node colors in Circosplot
- 'LinearSegmentedColormap' object has no attribute 'colors' with divergent network edges HOT 1
- draw() thakes 0 positional arguments but 1 was given HOT 6
- Module nxviz has no attribute 'annotate'
- Annotate node_colormapping legend_kwargs are not applied
- Error importing and executing Circosplot. HOT 1
- Matrix Plot Overlay Bug
- Customize node size and colormap in circosplot
- how show the node_labels on nxviz version:0.7.4 HOT 2
- ImportError: cannot import name 'CircosPlot' from 'nxviz.plots HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nxviz.