Skip to content

Commit

Permalink
Added auto-colorbar. New version good to go.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmjl committed Sep 18, 2017
1 parent cc07c3e commit 95a83dd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
History
=======

0.3.2 (2017-09-18)
------------------

* All plots except for HivePlot are implemented.
* Implemented auto-colorbar for plots that have continuous node colors.

0.1.0 (2016-07-15)
------------------

Expand Down
10 changes: 9 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,21 @@ nxviz
* This is free software distributed under the MIT License.


Features
--------

* Declarative API.
* Works with NetworkX, one of the more popular graph libraries in Python.

Feature Requests
----------------

If you have a feature request, please post it as an issue on the GitHub repository issue_ tracker. Even better, put in a PR for it! I am more than happy to guide you through the codebase so that you can put in a contribution to the codebase - and I'll give you a digital `nxviz` contributor badge that you can put on your personal website, as a way of saying thanks!

Because nxviz is currently maintained by volunteers and has no fiscal support, any feature requests will be prioritized according to what maintainers encounter as a need in our day-to-day jobs. Please temper expectations accordingly.

.. _issue: https://github.com/ericmjl/nxviz/issues

Credits
---------

Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
- pip
- setuptools
- pycodestyle
- pydocstyle
- click
- pandas
- coverage
Expand Down
41 changes: 34 additions & 7 deletions nxviz/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from .utils import (cmaps, infer_data_type, is_data_diverging,
num_discrete_groups)

import logging

logging.basicConfig(level=logging.INFO)


def despine(ax):
for spine in ax.spines:
Expand Down Expand Up @@ -79,6 +83,8 @@ def __init__(self, graph, node_order=None, node_size=None,

# Set node colors
self.node_color = node_color
self.sm = None # sm -> for scalarmappable. See https://stackoverflow.com/questions/8342549/matplotlib-add-colorbar-to-a-sequence-of-line-plots # noqa
logging.debug(f'INIT: {self.sm}')
if self.node_color:
self.node_colors = []
self.compute_node_colors()
Expand Down Expand Up @@ -132,16 +138,25 @@ def check_data_types(self, data_types):
def draw(self):
"""
Draws the Plot to screen.
If there is a continuous datatype for the nodes, it will be reflected
in self.sm being constructed (in `compute_node_colors`). It will then
automatically add in a colorbar to the plot and scale the plot axes
accordingly.
"""
self.draw_nodes()
self.draw_edges()
logging.debug(f'DRAW: {self.sm}')
if self.sm:
self.figure.subplots_adjust(right=0.8)
cax = self.figure.add_axes([0.85, 0.2, 0.05, 0.6])
self.figure.colorbar(self.sm, cax=cax)
self.ax.relim()
self.ax.autoscale_view()
self.ax.set_aspect('equal')

def compute_node_colors(self):
"""
Computes the node colors.
"""
"""Compute the node colors. Also computes the colorbar."""
data = [self.graph.node[n][self.node_color] for n in self.nodes]
data_reduced = sorted(list(set(data)))
dtype = infer_data_type(data)
Expand All @@ -158,6 +173,17 @@ def compute_node_colors(self):
idx = data_reduced.index(d) / n_grps
self.node_colors.append(cmap(idx))

# Add colorbar if required.
logging.debug(f'length of data_reduced: {len(data_reduced)}')
logging.debug(f'dtype: {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 compute_node_positions(self):
"""
Computes the positions of each node on the plot.
Expand Down Expand Up @@ -436,6 +462,10 @@ def __init__(self, graph, node_order=None, node_size=None,
def draw(self):
"""
Draws the plot to screen.
Note to self: Do NOT call super(MatrixPlot, self).draw(); the
underlying logic for drawing here is completely different from other
plots, and as such necessitates a different implementation.
"""
matrix = nx.to_numpy_matrix(self.graph, nodelist=self.nodes)
self.ax.matshow(matrix, cmap=self.cmap)
Expand Down Expand Up @@ -517,10 +547,7 @@ def draw_edges(self):
self.ax.add_patch(patch)

def draw(self):
self.draw_nodes()
self.draw_edges()
super(ArcPlot, self).draw()
xlimits = (-1, len(self.nodes) + 1)
# halfwidth = len(self.nodes) + 1 / 2
# ylimits = (-halfwidth, halfwidth)
self.ax.set_xlim(*xlimits)
self.ax.set_ylim(*xlimits)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name='nxviz',
version="0.3.1",
version="0.3.2",
description="Graph Visualization Package",
long_description=readme + '\n\n' + history,
author="Eric J. Ma",
Expand Down

0 comments on commit 95a83dd

Please sign in to comment.