{ "cells": [ { "cell_type": "markdown", "metadata": {}, "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!" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#### Formatting and Positioning Images as Logos" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "data = [\n", " go.Bar(\n", " x=['-35.3', '-15.9', '-15.8', '-15.6', '-11.1',\n", " '-9.6', '-9.2', '-3.5', '-1.9', '-0.9',\n", " '1.0', '1.4', '1.7', '2.0', '2.8', '6.2',\n", " '8.1', '8.5', '8.5', '8.6', '11.4', '12.5',\n", " '13.3', '13.7', '14.4', '17.5', '17.7',\n", " '18.9', '25.1', '28.9', '41.4'],\n", " y=['Designers, musicians, artists, etc.',\n", " 'Secretaries and administrative assistants',\n", " 'Waiters and servers', 'Archivists, curators, and librarians',\n", " 'Sales and related', 'Childcare workers, home car workers, etc.',\n", " 'Food preparation occupations', 'Janitors, maids, etc.',\n", " 'Healthcare technicians, assistants. and aides',\n", " 'Counselors, social and religious workers',\n", " 'Physical, life and social scientists', 'Construction',\n", " 'Factory assembly workers', 'Machinists, repairmen, etc.',\n", " 'Media and communications workers', 'Teachers',\n", " 'Mechanics, repairmen, etc.', 'Financial analysts and advisers',\n", " 'Farming, fishing and forestry workers',\n", " 'Truck drivers, heavy equipment operator, etc.','Accountants and auditors',\n", " 'Human resources, management analysts, etc.', 'Managers',\n", " 'Lawyers and judges', 'Engineers, architects and surveyors',\n", " 'Nurses', 'Legal support workers',\n", " 'Computer programmers and system admin.', 'Police officers and firefighters',\n", " 'Chief executives', 'Doctors, dentists and surgeons'],\n", " marker=dict(\n", " color='rgb(253, 240, 54)',\n", " line=dict(color='rgb(0, 0, 0)',\n", " width=2)\n", " ),\n", " orientation='h',\n", " )\n", "]\n", "\n", "layout = go.Layout(\n", " images=[dict(\n", " source=\"https://raw.githubusercontent.com/cldougl/plot_images/add_r_img/vox.png\",\n", " xref=\"paper\", yref=\"paper\",\n", " x=1, y=1.05,\n", " sizex=0.2, sizey=0.2,\n", " xanchor=\"right\", yanchor=\"bottom\"\n", " )],\n", " autosize=False, height=800, width=700,\n", " bargap=0.15, bargroupgap=0.1,\n", " barmode='stack', hovermode='x',\n", " margin=dict(r=20, l=300,\n", " b=75, t=125),\n", " title='Moving Up, Moving Down
Percentile change in income between childhood and adulthood',\n", " xaxis=dict(\n", " dtick=10, nticks=0,\n", " gridcolor='rgba(102, 102, 102, 0.4)',\n", " linecolor='#000', linewidth=1,\n", " mirror=True,\n", " showticklabels=True, tick0=0, tickwidth=1,\n", " title='Change in percentile',\n", " ),\n", " yaxis=dict(\n", " anchor='x',\n", " gridcolor='rgba(102, 102, 102, 0.4)', gridwidth=1,\n", " linecolor='#000', linewidth=1,\n", " mirror=True, showgrid=False,\n", " showline=True, zeroline=False,\n", " showticklabels=True, tick0=0,\n", " type='category',\n", " )\n", ")\n", "fig = go.Figure(data=data, layout=layout)\n", "py.iplot(fig)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "fig = py.get_figure('https://plotly.com/~Dreamshot/8152/', raw=True)\n", "fig['layout']['yaxis']['tickangle'] = 0\n", "fig = go.Figure(fig)\n", "\n", "fig.layout.images = [dict(\n", " source=\"https://raw.githubusercontent.com/cldougl/plot_images/add_r_img/accuweather.jpeg\",\n", " xref=\"paper\", yref=\"paper\",\n", " x=0.1, y=1.05,\n", " sizex=0.4, sizey=0.4,\n", " xanchor=\"center\", yanchor=\"bottom\"\n", " )]\n", "\n", "py.iplot(fig, fileopt='overwrite', filename='Logos/Florida_Rainfall_AccuWeather')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "\n", "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "fig = py.get_figure('https://plotly.com/~Dreamshot/8160/', raw=True)\n", "for j in range(len(fig['data'])):\n", " del fig['data'][j]['autobinx']\n", " del fig['data'][j]['autobiny']\n", "fig = go.Figure(fig)\n", "\n", "fig.layout.images = [dict(\n", " source=\"https://raw.githubusercontent.com/cldougl/plot_images/add_r_img/bleacherreport.png\",\n", " xref=\"paper\", yref=\"paper\",\n", " x=0.5, y=-0.35,\n", " sizex=0.3, sizey=0.3,\n", " xanchor=\"center\", yanchor=\"top\"\n", " )]\n", "\n", "py.iplot(fig, fileopt='overwrite', filename='Logos/Top_Earners_BleacherReport')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "\n", "fig = py.get_figure('https://plotly.com/~Dreamshot/8158/')\n", "\n", "fig.layout.images = [dict(\n", " source=\"https://raw.githubusercontent.com/cldougl/plot_images/add_r_img/theverge.png\",\n", " xref=\"paper\", yref=\"paper\",\n", " x=0.1, y=1.0,\n", " sizex=0.2, sizey=0.3,\n", " xanchor=\"center\", yanchor=\"bottom\"\n", " )]\n", "\n", "fig.layout.legend.orientation = 'h'\n", "\n", "py.iplot(fig, fileopt='overwrite', filename='Logos/Apple_Labor_Violations_TheVerge')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "\n", "fig = py.get_figure('https://plotly.com/~Dreamshot/8155/')\n", "\n", "fig.layout.images = [dict(\n", " source=\"https://raw.githubusercontent.com/cldougl/plot_images/add_r_img/politico.png\",\n", " xref=\"paper\", yref=\"paper\",\n", " x=0.1, y=-0.2,\n", " sizex=0.4, sizey=0.4,\n", " xanchor=\"center\", yanchor=\"bottom\"\n", " )]\n", "\n", "py.iplot(fig, fileopt='overwrite', filename='Logos/Foreign_Policy_Politico')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#### Reference\n", "See https://plotly.com/python/images/ for more examples of adding images
\n", "and https://plotly.com/python/reference/#layout-images for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-req-build-lNdjqb\n", "Building wheels for collected packages: publisher\n", " Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-ephem-wheel-cache-xCj_NF/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", "Successfully installed publisher-0.11\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n", "\n", "The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", "\n", "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n", "\n", "Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", "\n" ] } ], "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", " 'logos.ipynb', 'python/logos/', 'Add Logos to Charts',\n", " 'How to add images as logos to Plotly charts.',\n", " title = 'Add Logos to Charts | plotly',\n", " name = 'Logos',\n", " has_thumbnail='false', thumbnail='thumbnail/your-tutorial-chart.jpg', \n", " language='python', page_type='example_index',\n", " display_as='style_opt', order=6,\n", " ipynb= '~notebook_demo/92') " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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": 1 }