{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"#### New to Plotly? \n",
"Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n",
"
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n",
"
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!\n",
"\n",
"#### Version Check\n",
"Plotly's python package is updated frequently. To upgrade, run `pip install plotly --upgrade`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'2.0.2'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"#### Simple Annotated Heatmap"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"z = [[.1, .3, .5, .7, .9], \n",
" [1, .8, .6, .4, .2],\n",
" [.2, 0, .5, .7, .9], \n",
" [.9, .8, .4, .2, 0],\n",
" [.3, .4, .5, .7, 1]] \n",
"\n",
"fig = ff.create_annotated_heatmap(z)\n",
"py.iplot(fig, filename='annotated_heatmap')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"#### Defined Colorscale"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"z = [[.1, .3, .5, .7], \n",
" [1, .8, .6, .4],\n",
" [.6, .4, .2, .0], \n",
" [.9, .7, .5, .3]] \n",
"\n",
"fig = ff.create_annotated_heatmap(z, colorscale='Viridis')\n",
"py.iplot(fig, filename='annotated_heatmap_color')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"#### Custom Colorscale"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"z = [[.1, .3, .5, .7], \n",
" [1.0, .8, .6, .4],\n",
" [.6, .4, .2, 0.0], \n",
" [.9, .7, .5, .3]] \n",
"\n",
"colorscale = [[0, '#66475e'], [1, '#ecbfe0']]\n",
"font_colors = ['#efecee', '#3c3636']\n",
"fig = ff.create_annotated_heatmap(z, colorscale=colorscale, font_colors=font_colors)\n",
"py.iplot(fig, filename='annotated_heatmap_custom_color')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"#### Custom Text and X & Y Labels \n",
"set `annotation_text` to a matrix with the same dimmensions as `z`"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"\n",
"z = [[.1, .3, .5], \n",
" [1.0, .8, .6],\n",
" [.6, .4, .2]]\n",
"\n",
"x = ['Team A', 'Team B', 'Team C']\n",
"y = ['Game Three', 'Game Two', 'Game One']\n",
"\n",
"z_text = [['Win', 'Lose', 'Win'], \n",
" ['Lose', 'Lose', 'Win'],\n",
" ['Win', 'Win', 'Lose']]\n",
"\n",
"fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')\n",
"py.iplot(fig, filename='annotated_heatmap_text')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"#### Annotated Heatmap with numpy"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as ff\n",
"import numpy as np\n",
"\n",
"z = np.random.randn(20, 20)\n",
"z_text = np.around(z, decimals=2) # Only show rounded value (full value on hover)\n",
"\n",
"fig = ff.create_annotated_heatmap(z, annotation_text=z_text, colorscale='Greys', hoverinfo='z')\n",
"\n",
"# Make text size smaller\n",
"for i in range(len(fig.layout.annotations)):\n",
" fig.layout.annotations[i].font.size = 8\n",
" \n",
"py.iplot(fig, filename='annotated_heatmap_numpy')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"#### Custom Hovertext"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Add Periodic Table Data\n",
"symbol = [['H', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'He'],\n",
" ['Li', 'Be', '', '', '', '', '', '', '', '', '', '', 'B', 'C', 'N', 'O', 'F', 'Ne'],\n",
" ['Na', 'Mg', '', '', '', '', '', '', '', '', '', '', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar'],\n",
" ['K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr'],\n",
" ['Rb ', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe' ],\n",
" ['Cs', 'Ba', '', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn' ],\n",
" ['Fr', 'Ra', '', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn', 'Uut', 'Fl', 'Uup', 'Lv', 'Uus', 'Uuo'],\n",
" ['', '', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', ''],\n",
" ['', '', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr', '' ],\n",
" ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],\n",
" ['', 'Alkali Metal', '', '', 'Transition Metal', '', '', 'Actinide', '', '', 'Semimetal', '', '', 'Halogen', '', '', '', ''],\n",
" ['', 'Alkaline Metal', '', '', 'Lanthanide', '', '', 'Basic Metal', '', '', 'Nonmetal', '', '', 'Noble Gas', '', '', '', '']]\n",
"\n",
"element = [['Hydrogen', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Helium'],\n",
" ['Lithium', 'Beryllium', '', '', '', '', '', '', '', '', '', '', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', 'Neon'],\n",
" ['Sodium', 'Magnesium', '', '', '', '', '', '', '', '', '', '', 'Aluminium', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', ' Argon'],\n",
" ['Potassium', ' Calcium', ' Scandium', ' Titanium', ' Vanadium', ' Chromium', 'Manganese', 'Iron', 'Cobalt', 'Nickel', 'Copper', 'Zinc', 'Gallium', 'Germanium', 'Arsenic', 'Selenium', 'Bromine', 'Krypton'],\n",
" ['Rubidium', 'Strontium', 'Yttrium', 'Zirconium', 'Niobium', 'Molybdenum', 'Technetium', 'Ruthenium', 'Rhodium', 'Palladium', 'Silver', 'Cadmium', 'Indium', 'Tin', 'Antimony', 'Tellurium', 'Iodine', 'Xenon'],\n",
" [' Cesium', ' Barium', '', 'Hafnium', 'Tantalum', 'Tungsten', 'Rhenium', 'Osmium', 'Iridium', 'Platinum', 'Gold', 'Mercury', 'Thallium', 'Lead', 'Bismuth', 'Polonium', 'Astatine', 'Radon'],\n",
" [' Francium', ' Radium', '', 'Rutherfordium','Dubnium','Seaborgium','Bohrium','Hassium','Meitnerium','Darmstadtium','Roentgenium','Copernicium','Ununtrium','Ununquadium','Ununpentium','Ununhexium','Ununseptium','Ununoctium'],\n",
" ['', '', 'Lanthanum', 'Cerium', 'Praseodymium', 'Neodymium', 'Promethium', 'Samarium', 'Europium', 'Gadolinium', 'Terbium', 'Dysprosium', 'Holmium', 'Erbium', 'Thulium', 'Ytterbium', 'Lutetium', ''],\n",
" ['', '', 'Actinium', 'Thorium', 'Protactinium', 'Uranium', 'Neptunium', 'Plutonium', 'Americium', 'Curium', 'Berkelium', 'Californium', 'Einsteinium','Fermium' ,'Mendelevium', 'Nobelium', 'Lawrencium', '' ],\n",
" ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],\n",
" ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],\n",
" ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']]\n",
"\n",
"atomic_mass = [[ 1.00794, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 4.002602],\n",
" [ 6.941, 9.012182, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 10.811, 12.0107, 14.0067, 15.9994, 18.9984032, 20.1797],\n",
" [ 22.98976928, 24.3050, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 26.9815386, 28.0855, 30.973762, 32.065, 35.453, 39.948], \n",
" [ 39.0983, 40.078, 44.955912, 47.867, 50.9415, 51.9961, 54.938045, 55.845, 58.933195, 58.6934, 63.546, 65.38, 69.723, 72.64, 74.92160, 78.96, 79.904, 83.798],\n",
" [ 85.4678, 87.62, 88.90585, 91.224, 92.90638, 95.96, 98, 101.07, 102.90550, 106.42, 107.8682, 112.411, 114.818, 118.710, 121.760, 127.60, 126.90447, 131.293],\n",
" [ 132.9054519, 137.327, .0, 178.49, 180.94788, 183.84, 186.207, 190.23, 192.217, 195.084, 196.966569, 200.59, 204.3833, 207.2, 208.98040, 209, 210, 222],\n",
" [223, 226, .0, 267, 268, 271, 272, 270, 276, 281, 280, 285, 284, 289, 288, 293, 'unknown', 294],\n",
" [.0, .0, 138.90547, 140.116, 140.90765, 144.242, 145, 150.36, 151.964, 157.25, 158.92535, 162.500, 164.93032, 167.259, 168.93421, 173.054, 174.9668, .0],\n",
" [.0, .0, 227, 232.03806, 231.03588, 238.02891, 237, 244, 243, 247, 247, 251, 252, 257, 258, 259, 262, .0],\n",
" [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0],\n",
" [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0],\n",
" [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0]]\n",
"\n",
"z = [[.8, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, 1.],\n",
" [.1, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .7, .8, .8, .8, .9, 1.],\n",
" [.1, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .6, .7, .8, .8, .9, 1], \n",
" [.1, .2, .3, .3, .3, .3, .3, .3, .3, .3, .3, .3, .6, .7, .8, .8, .9, 1.],\n",
" [.1, .2, .3, .3, .3, .3, .3, .3, .3, .3, .3, .3, .6, .6, .7, .7, .9, 1.],\n",
" [.1, .2, .4, .3, .3, .3, .3, .3, .3, .3, .3, .3, .6, .6, .6, .7, .9, 1.],\n",
" [.1, .2, .5, .3, .3, .3, .3, .3, .3, .3, .3, .3, .6, .6, .6, .6, .9, 1.],\n",
" [.0, .0, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .4, .0],\n",
" [.0, .0, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .0],\n",
" [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0],\n",
" [.1, .1, .1, .3, .3, .3, .5, .5, .5, .7, .7, .7, .9, .9, .9, .0, .0, .0],\n",
" [.2, .2, .2, .4, .4, .4, .6, .6, .6, .8, .8, .8, 1., 1., 1., .0, .0, .0]]\n",
"\n",
"# Display element name and atomic mass on hover\n",
"hover=range(len(symbol))\n",
"for x in range(len(symbol)):\n",
" hover[x] = [i + '
' + 'Atomic Mass: ' + str(j) for i, j in zip(element[x], atomic_mass[x])]\n",
"\n",
"# Invert Matrices\n",
"symbol = symbol[::-1]\n",
"hover = hover[::-1]\n",
"z = z[::-1]\n",
"\n",
"# Set Colorscale\n",
"colorscale=[[0.0, 'rgb(255,255,255)'], [.2, 'rgb(255, 255, 153)'], \n",
" [.4, 'rgb(153, 255, 204)'], [.6, 'rgb(179, 217, 255)'], \n",
" [.8, 'rgb(240, 179, 255)'],[1.0, 'rgb(255, 77, 148)']]\n",
"\n",
"# Make Annotated Heatmap\n",
"pt = ff.create_annotated_heatmap(z, annotation_text=symbol, text=hover,\n",
" colorscale=colorscale, font_colors=['black'], hoverinfo='text')\n",
"pt.layout.title = 'Periodic Table'\n",
"\n",
"py.iplot(pt, filename='periodic_table')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"#### Reference\n",
"For more info on Plotly heatmaps, see: https://plotly.com/python/reference/#heatmap.
For more info on using colorscales with Plotly see: https://plotly.com/python/heatmap-and-contour-colorscales/
For more info on annotated_heatmaps, see:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function create_annotated_heatmap in module plotly.figure_factory._annotated_heatmap:\n",
"\n",
"create_annotated_heatmap(z, x=None, y=None, annotation_text=None, colorscale='RdBu', font_colors=None, showscale=False, reversescale=False, **kwargs)\n",
" BETA function that creates annotated heatmaps\n",
" \n",
" This function adds annotations to each cell of the heatmap.\n",
" \n",
" :param (list[list]|ndarray) z: z matrix to create heatmap.\n",
" :param (list) x: x axis labels.\n",
" :param (list) y: y axis labels.\n",
" :param (list[list]|ndarray) annotation_text: Text strings for\n",
" annotations. Should have the same dimensions as the z matrix. If no\n",
" text is added, the values of the z matrix are annotated. Default =\n",
" z matrix values.\n",
" :param (list|str) colorscale: heatmap colorscale.\n",
" :param (list) font_colors: List of two color strings: [min_text_color,\n",
" max_text_color] where min_text_color is applied to annotations for\n",
" heatmap values < (max_value - min_value)/2. If font_colors is not\n",
" defined, the colors are defined logically as black or white\n",
" depending on the heatmap's colorscale.\n",
" :param (bool) showscale: Display colorscale. Default = False\n",
" :param kwargs: kwargs passed through plotly.graph_objs.Heatmap.\n",
" These kwargs describe other attributes about the annotated Heatmap\n",
" trace such as the colorscale. For more information on valid kwargs\n",
" call help(plotly.graph_objs.Heatmap)\n",
" \n",
" Example 1: Simple annotated heatmap with default configuration\n",
" ```\n",
" import plotly.plotly as py\n",
" import plotly.figure_factory as FF\n",
" \n",
" z = [[0.300000, 0.00000, 0.65, 0.300000],\n",
" [1, 0.100005, 0.45, 0.4300],\n",
" [0.300000, 0.00000, 0.65, 0.300000],\n",
" [1, 0.100005, 0.45, 0.00000]]\n",
" \n",
" figure = FF.create_annotated_heatmap(z)\n",
" py.iplot(figure)\n",
" ```\n",
"\n"
]
}
],
"source": [
"help(FF.create_annotated_heatmap)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'annotated_heatmap.ipynb', 'python/annotated_heatmap/', 'Python Annotated Heatmaps | plotly',\n",
" 'How to make Annotated Heatmaps in Python with Plotly.',\n",
" title = 'Python Annotated Heatmaps | plotly',\n",
" name = 'Annotated Heatmaps',\n",
" thumbnail='thumbnail/ann_heat.jpg', language='python',\n",
" has_thumbnail='true', display_as='scientific', order=4,\n",
" ipynb= '~notebook_demo/35') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}