Skip to content

Commit

Permalink
Plotly visualizations 'first commit' squashed.
Browse files Browse the repository at this point in the history
  • Loading branch information
empet authored and Nathaniel Saul committed Sep 12, 2018
1 parent 289671e commit b50358e
Show file tree
Hide file tree
Showing 10 changed files with 70,105 additions and 0 deletions.
223 changes: 223 additions & 0 deletions examples/Plotly-colorscales.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotly colorscales"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To color the graph nodes or the bars in the node distribution we can choose a Plotly colorscale. The default colorscale\n",
"set in `kmapper.plotlyviz` is Viridis. `Viridis` is the choice over `Jet` colorscale, by the data visuzlization community, \n",
"because Jet doesn't meet scientific standards concerning luminance gradient, and more. \n",
"It is just colorful but doesn't convey right information.\n",
" \n",
"Viridis is the default colormap in Matplotlib and R. Matlab also changed Jet by Parula."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" A Plotly colorscale is a list of lists, and each inner list contains a float number in [0,1], and a color code.\n",
" The color codes\n",
"however are not tuples of float values in [0,1], like in `matplotlib`, but tuples of integers (`np.uint8`) between 0 and 255.\n",
"or hex colorcodes. \n",
"\n",
"Example of Plotly colorscale:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"pl_matter = [[0.0, 'rgb(253, 237, 176)'], #derived from cmocean.cm.matter https://matplotlib.org/cmocean/\n",
" [0.1, 'rgb(250, 202, 143)'],\n",
" [0.2, 'rgb(245, 166, 114)'],\n",
" [0.3, 'rgb(238, 132, 93)'],\n",
" [0.4, 'rgb(226, 97, 82)'],\n",
" [0.5, 'rgb(206, 67, 86)'],\n",
" [0.6, 'rgb(179, 46, 94)'],\n",
" [0.7, 'rgb(147, 31, 99)'],\n",
" [0.8, 'rgb(114, 25, 95)'],\n",
" [0.9, 'rgb(79, 21, 82)'],\n",
" [1.0, 'rgb(47, 15, 61)']]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"pl_brewer = [[0.0, '#006837'], #from green to red http://colorbrewer2.org/#type=diverging&scheme=RdYlGn&n=11\n",
" [0.1, '#1a9850'],\n",
" [0.2, '#66bd63'],\n",
" [0.3, '#a6d96a'],\n",
" [0.4, '#d9ef8b'],\n",
" [0.5, '#ffffbf'],\n",
" [0.6, '#fee08b'],\n",
" [0.7, '#fdae61'],\n",
" [0.8, '#f46d43'],\n",
" [0.9, '#d73027'],\n",
" [1.0, '#a50026']]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"pl_jet = [[0.0, 'rgb(0, 0, 127)'], #derived for matplotlib jet\n",
" [0.1, 'rgb(0, 0, 241)'],\n",
" [0.2, 'rgb(0, 76, 255)'],\n",
" [0.3, 'rgb(0, 176, 255)'],\n",
" [0.4, 'rgb(41, 255, 205)'],\n",
" [0.5, 'rgb(124, 255, 121)'],\n",
" [0.6, 'rgb(205, 255, 41)'],\n",
" [0.7, 'rgb(255, 196, 0)'],\n",
" [0.8, 'rgb(255, 103, 0)'],\n",
" [0.9, 'rgb(241, 7, 0)'],\n",
" [1.0, 'rgb(127, 0, 0)']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following function converts a matplotlib colormap to a Plotly colorscale with `n_entries` colors:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.cm as cm\n",
"import cmocean # https://matplotlib.org/cmocean/"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def mpl_to_plotly(cmap, n_entries):\n",
" h = 1.0 / (n_entries-1)\n",
" pl_colorscale = []\n",
" for k in range(n_entries):\n",
" C = list(map(np.uint8, np.array(cmap(k*h)[:3]) * 255))\n",
" pl_colorscale.append([round(k*h, 2), 'rgb' + str((C[0], C[1], C[2]))]) # Python 2.7+\n",
" # pl_colorscale.append([round(k*h, 2), f'rgb({C[0]}, {C[1]}, {C[2]})']) # Python 3.6+\n",
" return pl_colorscale"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0.0, 'rgb(165, 0, 38)'],\n",
" [0.1, 'rgb(214, 47, 38)'],\n",
" [0.2, 'rgb(244, 109, 67)'],\n",
" [0.3, 'rgb(252, 172, 96)'],\n",
" [0.4, 'rgb(254, 224, 144)'],\n",
" [0.5, 'rgb(254, 254, 192)'],\n",
" [0.6, 'rgb(224, 243, 247)'],\n",
" [0.7, 'rgb(169, 216, 232)'],\n",
" [0.8, 'rgb(116, 173, 209)'],\n",
" [0.9, 'rgb(68, 115, 179)'],\n",
" [1.0, 'rgb(49, 54, 149)']]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plotly_RdYlBu = mpl_to_plotly(cm.RdYlBu, 11)\n",
"plotly_RdYlBu"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0.0, 'rgb(16, 31, 63)'],\n",
" [0.1, 'rgb(35, 66, 150)'],\n",
" [0.2, 'rgb(34, 120, 163)'],\n",
" [0.3, 'rgb(76, 167, 174)'],\n",
" [0.4, 'rgb(172, 206, 197)'],\n",
" [0.5, 'rgb(254, 252, 203)'],\n",
" [0.6, 'rgb(216, 197, 96)'],\n",
" [0.7, 'rgb(140, 161, 10)'],\n",
" [0.8, 'rgb(50, 128, 30)'],\n",
" [0.9, 'rgb(16, 82, 43)'],\n",
" [1.0, 'rgb(23, 35, 18)']]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plotly_delta = mpl_to_plotly(cmocean.cm.delta, 11) \n",
"plotly_delta"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added examples/breast-cancer/breast-c-graph.pdf
Binary file not shown.
Loading

0 comments on commit b50358e

Please sign in to comment.