{ "metadata": { "name": "", "signature": "sha256:10119054985f27eefc838b05b2059a6aadc1915af50f1d00c1950284d6878b64" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Retrieving data" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "from Plotly graphs in Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook comes in response to this twitter conversation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the following, we intend to help Plotly users create graphs and share datasets. \n", "\n", "Randal S. Olson's blog post (link here, embedded below) contains information about the datasets used in the original figure and in the figure generated in this notebook. \n", "\n", "Please note that Randal S. Olson's blog post contains other figures --- which conflict with the figures shown in this notebook --- that are not replicated here." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import HTML\n", "HTML('' )" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, check the version which version of the Python API installed on your machine:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly\n", "plotly.__version__" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "'1.1.2'" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If not the latest, upgrade using `pip`:\n", "\n", " $ pip install plotly --upgrade" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, if you have a plotly account as well as a credentials file set up on your machine, singing in to Plotly's servers is done automatically while importing `plotly.plotly`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly.plotly as py " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more info on how to sign up or sign in to Plotly, see Plotly's Python API User Guide\n", "\n", "If more convenient, you can manually sign in to Plotly by typing:\n", " >>> py.sign_in('your_username','your_api_key')\n", " \n", "Next, import the Plotly `tools` module:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly.tools as tls " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And embed the original plot in this notebook, for reference:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "tls.embed(\"https://plot.ly/~randal_olson/10/\")\n", "\n", "# OR\n", "#tls.embed('randal_olson',10) " ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Retrieve the data from Plotly's servers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, pull down the figure object associated with the above figure and assign it to a variable:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "randal_olson10 = py.get_figure(\"https://plot.ly/~randal_olson/10\")\n", "\n", "# OR\n", "#py.get_figure('randal_olson',10) " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Python, Plotly figure objects are simply dictionaries:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "randal_olson10" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "{'data': [{'mode': 'markers',\n", " 'name': 'Physics & Astronomy',\n", " 'type': 'scatter',\n", " 'x': [0.19755117],\n", " 'y': [133]},\n", " {'mode': 'markers',\n", " 'name': 'Philosophy',\n", " 'type': 'scatter',\n", " 'x': [0.303802758],\n", " 'y': [129]},\n", " {'mode': 'markers',\n", " 'name': 'Mathematical Sciences',\n", " 'type': 'scatter',\n", " 'x': [0.430899055],\n", " 'y': [130]},\n", " {'mode': 'markers',\n", " 'name': 'Materials Engineering',\n", " 'type': 'scatter',\n", " 'x': [0.266536965],\n", " 'y': [129]},\n", " {'mode': 'markers',\n", " 'name': 'Economics',\n", " 'type': 'scatter',\n", " 'x': [0.292412488],\n", " 'y': [128]},\n", " {'mode': 'markers',\n", " 'name': 'Chemical Engineering',\n", " 'type': 'scatter',\n", " 'x': [0.320681753],\n", " 'y': [128]},\n", " {'mode': 'markers',\n", " 'name': 'Mechanical Engineering',\n", " 'type': 'scatter',\n", " 'x': [0.120928874],\n", " 'y': [126]},\n", " {'mode': 'markers',\n", " 'name': 'Physical Sciences',\n", " 'type': 'scatter',\n", " 'x': [0.399915718],\n", " 'y': [125]},\n", " {'mode': 'markers',\n", " 'name': 'Engineering',\n", " 'type': 'scatter',\n", " 'x': [0.191111056],\n", " 'y': [126]},\n", " {'mode': 'markers',\n", " 'name': 'Electrical Engineering',\n", " 'type': 'scatter',\n", " 'x': [0.118980639],\n", " 'y': [126]},\n", " {'mode': 'markers',\n", " 'name': 'Chemistry',\n", " 'type': 'scatter',\n", " 'x': [0.484352059],\n", " 'y': [124]},\n", " {'mode': 'markers',\n", " 'name': 'Computer & Information Science',\n", " 'type': 'scatter',\n", " 'x': [0.151776603],\n", " 'y': [124]},\n", " {'mode': 'markers',\n", " 'name': 'Civil Engineering',\n", " 'type': 'scatter',\n", " 'x': [0.208496367],\n", " 'y': [124]},\n", " {'mode': 'markers',\n", " 'name': 'Religion & Theory',\n", " 'type': 'scatter',\n", " 'x': [0.45890411],\n", " 'y': [121]},\n", " {'mode': 'markers',\n", " 'name': 'Industrial Engineering',\n", " 'type': 'scatter',\n", " 'x': [0.299327682],\n", " 'y': [123]},\n", " {'mode': 'markers',\n", " 'name': 'Earth, Atmos & Mar. Science',\n", " 'type': 'scatter',\n", " 'x': [0.387461459],\n", " 'y': [121]},\n", " {'mode': 'markers',\n", " 'name': 'English Language & Literature',\n", " 'type': 'scatter',\n", " 'x': [0.693180767],\n", " 'y': [120]},\n", " {'mode': 'markers',\n", " 'name': 'Humanities & Arts',\n", " 'type': 'scatter',\n", " 'x': [0.672495143],\n", " 'y': [120]},\n", " {'mode': 'markers',\n", " 'name': 'Arts-History, Theory, Critical Theory',\n", " 'type': 'scatter',\n", " 'x': [0.880733945],\n", " 'y': [120]},\n", " {'mode': 'markers',\n", " 'name': 'Biological Sciences',\n", " 'type': 'scatter',\n", " 'x': [0.607156676],\n", " 'y': [121]},\n", " {'mode': 'markers',\n", " 'name': 'Political Science',\n", " 'type': 'scatter',\n", " 'x': [0.438320142],\n", " 'y': [120]},\n", " {'mode': 'markers',\n", " 'name': 'Foreign Languages & Literature',\n", " 'type': 'scatter',\n", " 'x': [0.695368498],\n", " 'y': [119]},\n", " {'mode': 'markers',\n", " 'name': 'Anthropology & Archeology',\n", " 'type': 'scatter',\n", " 'x': [0.712784428],\n", " 'y': [119]},\n", " {'mode': 'markers',\n", " 'name': 'History',\n", " 'type': 'scatter',\n", " 'x': [0.402921329],\n", " 'y': [119]},\n", " {'mode': 'markers',\n", " 'name': 'Library & Archival Sciences',\n", " 'type': 'scatter',\n", " 'x': [0.926315789],\n", " 'y': [117]},\n", " {'mode': 'markers',\n", " 'name': 'Architecture',\n", " 'type': 'scatter',\n", " 'x': [0.413907285],\n", " 'y': [118]},\n", " {'mode': 'markers',\n", " 'name': 'Secondary Education',\n", " 'type': 'scatter',\n", " 'x': [0.598188875],\n", " 'y': [116]},\n", " {'mode': 'markers',\n", " 'name': 'Social Sciences',\n", " 'type': 'scatter',\n", " 'x': [0.651926263],\n", " 'y': [115]},\n", " {'mode': 'markers',\n", " 'name': 'Agriculture',\n", " 'type': 'scatter',\n", " 'x': [0.500371819],\n", " 'y': [115]},\n", " {'mode': 'markers',\n", " 'name': 'Arts-Performance & Studio',\n", " 'type': 'scatter',\n", " 'x': [0.61211729],\n", " 'y': [114]},\n", " {'mode': 'markers',\n", " 'name': 'Sociology',\n", " 'type': 'scatter',\n", " 'x': [0.691523961],\n", " 'y': [114]},\n", " {'mode': 'markers',\n", " 'name': 'Business',\n", " 'type': 'scatter',\n", " 'x': [0.481804179],\n", " 'y': [114]},\n", " {'mode': 'markers',\n", " 'name': 'Psychology',\n", " 'type': 'scatter',\n", " 'x': [0.76688749],\n", " 'y': [113]},\n", " {'mode': 'markers',\n", " 'name': 'Communications',\n", " 'type': 'scatter',\n", " 'x': [0.622262034],\n", " 'y': [111]},\n", " {'mode': 'markers',\n", " 'name': 'Health & Medical Sciences',\n", " 'type': 'scatter',\n", " 'x': [0.847846304],\n", " 'y': [111]},\n", " {'mode': 'markers',\n", " 'name': 'Business Admin & Mgmt.',\n", " 'type': 'scatter',\n", " 'x': [0.485422216],\n", " 'y': [111]},\n", " {'mode': 'markers',\n", " 'name': 'Education',\n", " 'type': 'scatter',\n", " 'x': [0.794328118],\n", " 'y': [110]},\n", " {'mode': 'markers',\n", " 'name': 'Accounting',\n", " 'type': 'scatter',\n", " 'x': [0.519718537],\n", " 'y': [110]},\n", " {'mode': 'markers',\n", " 'name': 'Public Administration',\n", " 'type': 'scatter',\n", " 'x': [0.496778153],\n", " 'y': [109]},\n", " {'mode': 'markers',\n", " 'name': 'Elementary Education',\n", " 'type': 'scatter',\n", " 'x': [0.90574922],\n", " 'y': [108]},\n", " {'mode': 'markers',\n", " 'name': 'Home Economics',\n", " 'type': 'scatter',\n", " 'x': [0.977715877],\n", " 'y': [106]},\n", " {'mode': 'markers',\n", " 'name': 'Special Education',\n", " 'type': 'scatter',\n", " 'x': [0.887551867],\n", " 'y': [106]},\n", " {'mode': 'markers',\n", " 'name': 'Early Childhood Education',\n", " 'type': 'scatter',\n", " 'x': [0.96746988],\n", " 'y': [104]},\n", " {'mode': 'markers',\n", " 'name': 'Social Work',\n", " 'type': 'scatter',\n", " 'x': [0.886732364],\n", " 'y': [103]}],\n", " 'layout': {'autosize': True,\n", " 'bargap': 0.2,\n", " 'bargroupgap': 0,\n", " 'barmode': 'group',\n", " 'boxmode': 'overlay',\n", " 'dragmode': 'zoom',\n", " 'font': {'color': '#444',\n", " 'family': '\"Open sans\", verdana, arial, sans-serif',\n", " 'size': 12},\n", " 'height': 527,\n", " 'hidesources': False,\n", " 'hovermode': 'x',\n", " 'legend': {'bgcolor': '#fff',\n", " 'bordercolor': '#444',\n", " 'borderwidth': 0,\n", " 'font': {'color': '', 'family': '', 'size': 0},\n", " 'traceorder': 'normal',\n", " 'x': 1.02,\n", " 'xanchor': 'left',\n", " 'y': 1,\n", " 'yanchor': 'top'},\n", " 'margin': {'autoexpand': True,\n", " 'b': 80,\n", " 'l': 80,\n", " 'pad': 0,\n", " 'r': 80,\n", " 't': 100},\n", " 'paper_bgcolor': '#fff',\n", " 'plot_bgcolor': '#fff',\n", " 'separators': '.,',\n", " 'showlegend': False,\n", " 'title': 'U.S. college majors: Average IQ of students by gender ratio',\n", " 'titlefont': {'color': '', 'family': '', 'size': 0},\n", " 'width': 1296,\n", " 'xaxis': {'anchor': 'y',\n", " 'autorange': True,\n", " 'autotick': True,\n", " 'domain': [0, 1],\n", " 'dtick': 0.1,\n", " 'exponentformat': 'B',\n", " 'gridcolor': '#eee',\n", " 'gridwidth': 1,\n", " 'linecolor': '#444',\n", " 'linewidth': 1,\n", " 'mirror': False,\n", " 'nticks': 0,\n", " 'overlaying': False,\n", " 'position': 0,\n", " 'range': [0.06845686171507281, 1.0282396542849273],\n", " 'rangemode': 'normal',\n", " 'showexponent': 'all',\n", " 'showgrid': True,\n", " 'showline': False,\n", " 'showticklabels': True,\n", " 'tick0': 0,\n", " 'tickangle': 'auto',\n", " 'tickcolor': '#444',\n", " 'tickfont': {'color': '', 'family': '', 'size': 0},\n", " 'ticklen': 5,\n", " 'ticks': '',\n", " 'tickwidth': 1,\n", " 'title': '% Female Majors',\n", " 'titlefont': {'color': '', 'family': '', 'size': 0},\n", " 'type': 'linear',\n", " 'zeroline': True,\n", " 'zerolinecolor': '#444',\n", " 'zerolinewidth': 1},\n", " 'yaxis': {'anchor': 'x',\n", " 'autorange': True,\n", " 'autotick': True,\n", " 'domain': [0, 1],\n", " 'dtick': 5,\n", " 'exponentformat': 'B',\n", " 'gridcolor': '#eee',\n", " 'gridwidth': 1,\n", " 'linecolor': '#444',\n", " 'linewidth': 1,\n", " 'mirror': False,\n", " 'nticks': 0,\n", " 'overlaying': False,\n", " 'position': 0,\n", " 'range': [101.00685602350637, 134.99314397649363],\n", " 'rangemode': 'normal',\n", " 'showexponent': 'all',\n", " 'showgrid': True,\n", " 'showline': False,\n", " 'showticklabels': True,\n", " 'tick0': 0,\n", " 'tickangle': 'auto',\n", " 'tickcolor': '#444',\n", " 'tickfont': {'color': '', 'family': '', 'size': 0},\n", " 'ticklen': 5,\n", " 'ticks': '',\n", " 'tickwidth': 1,\n", " 'title': 'Average IQ',\n", " 'titlefont': {'color': '', 'family': '', 'size': 0},\n", " 'type': 'linear',\n", " 'zeroline': True,\n", " 'zerolinecolor': '#444',\n", " 'zerolinewidth': 1}}}" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make it easier to for Python users to build Plotly figure from the API, the figure object (or dictionary) is subdivided in *graph objects* (more later in this notebook). Consider," ] }, { "cell_type": "code", "collapsed": false, "input": [ "print randal_olson10.to_string()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Figure(\n", " data=Data([\n", " Scatter(\n", " x=[0.19755117],\n", " y=[133],\n", " mode='markers',\n", " name='Physics & Astronomy'\n", " ),\n", " Scatter(\n", " x=[0.303802758],\n", " y=[129],\n", " mode='markers',\n", " name='Philosophy'\n", " ),\n", " Scatter(\n", " x=[0.430899055],\n", " y=[130],\n", " mode='markers',\n", " name='Mathematical Sciences'\n", " ),\n", " Scatter(\n", " x=[0.266536965],\n", " y=[129],\n", " mode='markers',\n", " name='Materials Engineering'\n", " ),\n", " Scatter(\n", " x=[0.292412488],\n", " y=[128],\n", " mode='markers',\n", " name='Economics'\n", " ),\n", " Scatter(\n", " x=[0.320681753],\n", " y=[128],\n", " mode='markers',\n", " name='Chemical Engineering'\n", " ),\n", " Scatter(\n", " x=[0.120928874],\n", " y=[126],\n", " mode='markers',\n", " name='Mechanical Engineering'\n", " ),\n", " Scatter(\n", " x=[0.399915718],\n", " y=[125],\n", " mode='markers',\n", " name='Physical Sciences'\n", " ),\n", " Scatter(\n", " x=[0.191111056],\n", " y=[126],\n", " mode='markers',\n", " name='Engineering'\n", " ),\n", " Scatter(\n", " x=[0.118980639],\n", " y=[126],\n", " mode='markers',\n", " name='Electrical Engineering'\n", " ),\n", " Scatter(\n", " x=[0.484352059],\n", " y=[124],\n", " mode='markers',\n", " name='Chemistry'\n", " ),\n", " Scatter(\n", " x=[0.151776603],\n", " y=[124],\n", " mode='markers',\n", " name='Computer & Information Science'\n", " ),\n", " Scatter(\n", " x=[0.208496367],\n", " y=[124],\n", " mode='markers',\n", " name='Civil Engineering'\n", " ),\n", " Scatter(\n", " x=[0.45890411],\n", " y=[121],\n", " mode='markers',\n", " name='Religion & Theory'\n", " ),\n", " Scatter(\n", " x=[0.299327682],\n", " y=[123],\n", " mode='markers',\n", " name='Industrial Engineering'\n", " ),\n", " Scatter(\n", " x=[0.387461459],\n", " y=[121],\n", " mode='markers',\n", " name='Earth, Atmos & Mar. Science'\n", " ),\n", " Scatter(\n", " x=[0.693180767],\n", " y=[120],\n", " mode='markers',\n", " name='English Language & Literature'\n", " ),\n", " Scatter(\n", " x=[0.672495143],\n", " y=[120],\n", " mode='markers',\n", " name='Humanities & Arts'\n", " ),\n", " Scatter(\n", " x=[0.880733945],\n", " y=[120],\n", " mode='markers',\n", " name='Arts-History, Theory, Critical Theory'\n", " ),\n", " Scatter(\n", " x=[0.607156676],\n", " y=[121],\n", " mode='markers',\n", " name='Biological Sciences'\n", " ),\n", " Scatter(\n", " x=[0.438320142],\n", " y=[120],\n", " mode='markers',\n", " name='Political Science'\n", " ),\n", " Scatter(\n", " x=[0.695368498],\n", " y=[119],\n", " mode='markers',\n", " name='Foreign Languages & Literature'\n", " ),\n", " Scatter(\n", " x=[0.712784428],\n", " y=[119],\n", " mode='markers',\n", " name='Anthropology & Archeology'\n", " ),\n", " Scatter(\n", " x=[0.402921329],\n", " y=[119],\n", " mode='markers',\n", " name='History'\n", " ),\n", " Scatter(\n", " x=[0.926315789],\n", " y=[117],\n", " mode='markers',\n", " name='Library & Archival Sciences'\n", " ),\n", " Scatter(\n", " x=[0.413907285],\n", " y=[118],\n", " mode='markers',\n", " name='Architecture'\n", " ),\n", " Scatter(\n", " x=[0.598188875],\n", " y=[116],\n", " mode='markers',\n", " name='Secondary Education'\n", " ),\n", " Scatter(\n", " x=[0.651926263],\n", " y=[115],\n", " mode='markers',\n", " name='Social Sciences'\n", " ),\n", " Scatter(\n", " x=[0.500371819],\n", " y=[115],\n", " mode='markers',\n", " name='Agriculture'\n", " ),\n", " Scatter(\n", " x=[0.61211729],\n", " y=[114],\n", " mode='markers',\n", " name='Arts-Performance & Studio'\n", " ),\n", " Scatter(\n", " x=[0.691523961],\n", " y=[114],\n", " mode='markers',\n", " name='Sociology'\n", " ),\n", " Scatter(\n", " x=[0.481804179],\n", " y=[114],\n", " mode='markers',\n", " name='Business'\n", " ),\n", " Scatter(\n", " x=[0.76688749],\n", " y=[113],\n", " mode='markers',\n", " name='Psychology'\n", " ),\n", " Scatter(\n", " x=[0.622262034],\n", " y=[111],\n", " mode='markers',\n", " name='Communications'\n", " ),\n", " Scatter(\n", " x=[0.847846304],\n", " y=[111],\n", " mode='markers',\n", " name='Health & Medical Sciences'\n", " ),\n", " Scatter(\n", " x=[0.485422216],\n", " y=[111],\n", " mode='markers',\n", " name='Business Admin & Mgmt.'\n", " ),\n", " Scatter(\n", " x=[0.794328118],\n", " y=[110],\n", " mode='markers',\n", " name='Education'\n", " ),\n", " Scatter(\n", " x=[0.519718537],\n", " y=[110],\n", " mode='markers',\n", " name='Accounting'\n", " ),\n", " Scatter(\n", " x=[0.496778153],\n", " y=[109],\n", " mode='markers',\n", " name='Public Administration'\n", " ),\n", " Scatter(\n", " x=[0.90574922],\n", " y=[108],\n", " mode='markers',\n", " name='Elementary Education'\n", " ),\n", " Scatter(\n", " x=[0.977715877],\n", " y=[106],\n", " mode='markers',\n", " name='Home Economics'\n", " ),\n", " Scatter(\n", " x=[0.887551867],\n", " y=[106],\n", " mode='markers',\n", " name='Special Education'\n", " ),\n", " Scatter(\n", " x=[0.96746988],\n", " y=[104],\n", " mode='markers',\n", " name='Early Childhood Education'\n", " ),\n", " Scatter(\n", " x=[0.886732364],\n", " y=[103],\n", " mode='markers',\n", " name='Social Work'\n", " )\n", " ]),\n", " layout=Layout(\n", " title='U.S. college majors: Average IQ of students by gender ratio',\n", " titlefont={'color': '', 'family': '', 'size': 0},\n", " font=Font(\n", " family='\"Open sans\", verdana, arial, sans-serif',\n", " size=12,\n", " color='#444'\n", " ),\n", " showlegend=False,\n", " autosize=True,\n", " width=1296,\n", " height=527,\n", " xaxis=XAxis(\n", " title='% Female Majors',\n", " titlefont={'color': '', 'family': '', 'size': 0},\n", " range=[0.06845686171507281, 1.0282396542849273],\n", " domain=[0, 1],\n", " type='linear',\n", " rangemode='normal',\n", " showgrid=True,\n", " zeroline=True,\n", " showline=False,\n", " autotick=True,\n", " nticks=0,\n", " ticks='',\n", " showticklabels=True,\n", " tick0=0,\n", " dtick=0.1,\n", " ticklen=5,\n", " tickwidth=1,\n", " tickcolor='#444',\n", " tickangle='auto',\n", " tickfont=Font(\n", " family='',\n", " size=0,\n", " color=''\n", " ),\n", " exponentformat='B',\n", " showexponent='all',\n", " gridcolor='#eee',\n", " gridwidth=1,\n", " zerolinecolor='#444',\n", " zerolinewidth=1,\n", " linecolor='#444',\n", " linewidth=1,\n", " anchor='y',\n", " position=0,\n", " mirror=False,\n", " overlaying=False,\n", " autorange=True\n", " ),\n", " yaxis=YAxis(\n", " title='Average IQ',\n", " titlefont={'color': '', 'family': '', 'size': 0},\n", " range=[101.00685602350637, 134.99314397649363],\n", " domain=[0, 1],\n", " type='linear',\n", " rangemode='normal',\n", " showgrid=True,\n", " zeroline=True,\n", " showline=False,\n", " autotick=True,\n", " nticks=0,\n", " ticks='',\n", " showticklabels=True,\n", " tick0=0,\n", " dtick=5,\n", " ticklen=5,\n", " tickwidth=1,\n", " tickcolor='#444',\n", " tickangle='auto',\n", " tickfont=Font(\n", " family='',\n", " size=0,\n", " color=''\n", " ),\n", " exponentformat='B',\n", " showexponent='all',\n", " gridcolor='#eee',\n", " gridwidth=1,\n", " zerolinecolor='#444',\n", " zerolinewidth=1,\n", " linecolor='#444',\n", " linewidth=1,\n", " anchor='x',\n", " position=0,\n", " mirror=False,\n", " overlaying=False,\n", " autorange=True\n", " ),\n", " legend=Legend(\n", " x=1.02,\n", " y=1,\n", " traceorder='normal',\n", " font=Font(\n", " family='',\n", " size=0,\n", " color=''\n", " ),\n", " bgcolor='#fff',\n", " bordercolor='#444',\n", " borderwidth=0,\n", " xanchor='left',\n", " yanchor='top'\n", " ),\n", " margin=Margin(\n", " l=80,\n", " r=80,\n", " b=80,\n", " t=100,\n", " pad=0,\n", " autoexpand=True\n", " ),\n", " paper_bgcolor='#fff',\n", " plot_bgcolor='#fff',\n", " hovermode='x',\n", " dragmode='zoom',\n", " barmode='group',\n", " bargap=0.2,\n", " bargroupgap=0,\n", " boxmode='overlay',\n", " separators='.,',\n", " hidesources=False\n", " )\n", ")\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "where `Figure`, `Data`, `Scatter`, etc are individual graph objects. \n", "\n", "Furthermore, Plotly's Python API makes it easy to retrieve only the parts of the figure object associated with the data making up the figure:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "randal_olson10_data = randal_olson10.get_data()\n", "\n", "randal_olson10_data" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "[{'name': 'Physics & Astronomy', 'x': [0.19755117], 'y': [133]},\n", " {'name': 'Philosophy', 'x': [0.303802758], 'y': [129]},\n", " {'name': 'Mathematical Sciences', 'x': [0.430899055], 'y': [130]},\n", " {'name': 'Materials Engineering', 'x': [0.266536965], 'y': [129]},\n", " {'name': 'Economics', 'x': [0.292412488], 'y': [128]},\n", " {'name': 'Chemical Engineering', 'x': [0.320681753], 'y': [128]},\n", " {'name': 'Mechanical Engineering', 'x': [0.120928874], 'y': [126]},\n", " {'name': 'Physical Sciences', 'x': [0.399915718], 'y': [125]},\n", " {'name': 'Engineering', 'x': [0.191111056], 'y': [126]},\n", " {'name': 'Electrical Engineering', 'x': [0.118980639], 'y': [126]},\n", " {'name': 'Chemistry', 'x': [0.484352059], 'y': [124]},\n", " {'name': 'Computer & Information Science', 'x': [0.151776603], 'y': [124]},\n", " {'name': 'Civil Engineering', 'x': [0.208496367], 'y': [124]},\n", " {'name': 'Religion & Theory', 'x': [0.45890411], 'y': [121]},\n", " {'name': 'Industrial Engineering', 'x': [0.299327682], 'y': [123]},\n", " {'name': 'Earth, Atmos & Mar. Science', 'x': [0.387461459], 'y': [121]},\n", " {'name': 'English Language & Literature', 'x': [0.693180767], 'y': [120]},\n", " {'name': 'Humanities & Arts', 'x': [0.672495143], 'y': [120]},\n", " {'name': 'Arts-History, Theory, Critical Theory',\n", " 'x': [0.880733945],\n", " 'y': [120]},\n", " {'name': 'Biological Sciences', 'x': [0.607156676], 'y': [121]},\n", " {'name': 'Political Science', 'x': [0.438320142], 'y': [120]},\n", " {'name': 'Foreign Languages & Literature', 'x': [0.695368498], 'y': [119]},\n", " {'name': 'Anthropology & Archeology', 'x': [0.712784428], 'y': [119]},\n", " {'name': 'History', 'x': [0.402921329], 'y': [119]},\n", " {'name': 'Library & Archival Sciences', 'x': [0.926315789], 'y': [117]},\n", " {'name': 'Architecture', 'x': [0.413907285], 'y': [118]},\n", " {'name': 'Secondary Education', 'x': [0.598188875], 'y': [116]},\n", " {'name': 'Social Sciences', 'x': [0.651926263], 'y': [115]},\n", " {'name': 'Agriculture', 'x': [0.500371819], 'y': [115]},\n", " {'name': 'Arts-Performance & Studio', 'x': [0.61211729], 'y': [114]},\n", " {'name': 'Sociology', 'x': [0.691523961], 'y': [114]},\n", " {'name': 'Business', 'x': [0.481804179], 'y': [114]},\n", " {'name': 'Psychology', 'x': [0.76688749], 'y': [113]},\n", " {'name': 'Communications', 'x': [0.622262034], 'y': [111]},\n", " {'name': 'Health & Medical Sciences', 'x': [0.847846304], 'y': [111]},\n", " {'name': 'Business Admin & Mgmt.', 'x': [0.485422216], 'y': [111]},\n", " {'name': 'Education', 'x': [0.794328118], 'y': [110]},\n", " {'name': 'Accounting', 'x': [0.519718537], 'y': [110]},\n", " {'name': 'Public Administration', 'x': [0.496778153], 'y': [109]},\n", " {'name': 'Elementary Education', 'x': [0.90574922], 'y': [108]},\n", " {'name': 'Home Economics', 'x': [0.977715877], 'y': [106]},\n", " {'name': 'Special Education', 'x': [0.887551867], 'y': [106]},\n", " {'name': 'Early Childhood Education', 'x': [0.96746988], 'y': [104]},\n", " {'name': 'Social Work', 'x': [0.886732364], 'y': [103]}]" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "which is just a list of dictionaries, one for each trace.\n", "\n", "Next, let's combine data from all these traces into three lists: one for x-coordinates, y-coordinates and degree names using Python list comprehension:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "X = [trace['x'][0] for trace in randal_olson10_data]\n", "Y = [trace['y'][0] for trace in randal_olson10_data]\n", "NAME = [trace['name'] for trace in randal_olson10_data]\n", "\n", "zip(X,Y,NAME) # print to screen as tuple" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "[(0.19755117, 133, 'Physics & Astronomy'),\n", " (0.303802758, 129, 'Philosophy'),\n", " (0.430899055, 130, 'Mathematical Sciences'),\n", " (0.266536965, 129, 'Materials Engineering'),\n", " (0.292412488, 128, 'Economics'),\n", " (0.320681753, 128, 'Chemical Engineering'),\n", " (0.120928874, 126, 'Mechanical Engineering'),\n", " (0.399915718, 125, 'Physical Sciences'),\n", " (0.191111056, 126, 'Engineering'),\n", " (0.118980639, 126, 'Electrical Engineering'),\n", " (0.484352059, 124, 'Chemistry'),\n", " (0.151776603, 124, 'Computer & Information Science'),\n", " (0.208496367, 124, 'Civil Engineering'),\n", " (0.45890411, 121, 'Religion & Theory'),\n", " (0.299327682, 123, 'Industrial Engineering'),\n", " (0.387461459, 121, 'Earth, Atmos & Mar. Science'),\n", " (0.693180767, 120, 'English Language & Literature'),\n", " (0.672495143, 120, 'Humanities & Arts'),\n", " (0.880733945, 120, 'Arts-History, Theory, Critical Theory'),\n", " (0.607156676, 121, 'Biological Sciences'),\n", " (0.438320142, 120, 'Political Science'),\n", " (0.695368498, 119, 'Foreign Languages & Literature'),\n", " (0.712784428, 119, 'Anthropology & Archeology'),\n", " (0.402921329, 119, 'History'),\n", " (0.926315789, 117, 'Library & Archival Sciences'),\n", " (0.413907285, 118, 'Architecture'),\n", " (0.598188875, 116, 'Secondary Education'),\n", " (0.651926263, 115, 'Social Sciences'),\n", " (0.500371819, 115, 'Agriculture'),\n", " (0.61211729, 114, 'Arts-Performance & Studio'),\n", " (0.691523961, 114, 'Sociology'),\n", " (0.481804179, 114, 'Business'),\n", " (0.76688749, 113, 'Psychology'),\n", " (0.622262034, 111, 'Communications'),\n", " (0.847846304, 111, 'Health & Medical Sciences'),\n", " (0.485422216, 111, 'Business Admin & Mgmt.'),\n", " (0.794328118, 110, 'Education'),\n", " (0.519718537, 110, 'Accounting'),\n", " (0.496778153, 109, 'Public Administration'),\n", " (0.90574922, 108, 'Elementary Education'),\n", " (0.977715877, 106, 'Home Economics'),\n", " (0.887551867, 106, 'Special Education'),\n", " (0.96746988, 104, 'Early Childhood Education'),\n", " (0.886732364, 103, 'Social Work')]" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Plot the data back" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's remake the original plot. \n", "\n", "First, import the required graph objects to build the figure:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from plotly.graph_objs import Data, Layout, Figure\n", "from plotly.graph_objs import Scatter, Marker, Line\n", "from plotly.graph_objs import XAxis, YAxis" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "This time, put all the data into one `Scatter` object:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "scatter = Scatter(\n", " x=X, # x-coordinates\n", " y=Y, # y-coordinates \n", " mode='markers', # show just markers pts\n", " name='', # no name (which appear on the side of the cursor) \n", " text=NAME, # list degree names in a text block on hover\n", " marker= Marker( \n", " size=18, \n", " color='rgb(142, 124, 195)', \n", " opacity=0.7, # slightly transparent pts\n", " line=Line( \n", " color='white', # line around marker pts\n", " width=0.5\n", " )\n", " )\n", ")\n", "\n", "data = Data([scatter]) # package into Data object (accepts a list)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please note that \n", "\n", "> Values associated to the `'size'`, `'opacity'` and `'color'` keys can be lists or numpy array where the elements are mapped to the markers in the same order as the x,y coordinates. So, for instance, one does not need to create multiple traces to plot marker points of different colors." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On to the figure's layout specifications:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "layout = Layout(\n", " title=randal_olson10['layout']['title'], # original title\n", " xaxis= XAxis(\n", " title=randal_olson10['layout']['xaxis']['title'] # original x-axis title\n", " ),\n", " yaxis= YAxis(\n", " title=randal_olson10['layout']['yaxis']['title'] # original y-axis title\n", " ),\n", " showlegend=False, # remove legend\n", " hovermode='closest', # show closest pt on hover\n", " autosize=False, # custom size\n", " width=700, \n", " height=525\n", ")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Package data and layout object into a new figure object (or instance in Python lingo)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fig = Figure(data=data, layout=layout)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Send `fig` to Plotly and display result in this notebook:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "py.iplot(fig, filename='randal_olson10-remake')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#####The data grid of the original figure looks like:\n", "\n", "
\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "#####The data grid of the remake looks like:\n", "\n", "
\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "#####To make the above plot from the data grid\n", "\n", "* Select *Bubble charts* under *MAKE A PLOT*\n", "\n", "\n", "\n", "
\n", "\n", "* Click on *Text* in the left-hand panel and select the appropriate column." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "To learn more about Plotly's Python API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Refer to\n", "\n", "* our online documentation page or\n", "* our User Guide." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "
\n", "\n", "

Got Questions or Feedback?

\n", "\n", "About Plotly\n", "\n", "* email: feedback@plot.ly \n", "* tweet: \n", "@plotlygraphs\n", "\n", "

Notebook styling ideas

\n", "\n", "Big thanks to\n", "\n", "* Cam Davidson-Pilon\n", "* Lorena A. Barba\n", "\n", "
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import display, HTML\n", "import urllib2\n", "url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'\n", "display(HTML(urllib2.urlopen(url).read()))" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 16 } ], "metadata": {} } ] }