From e5d8c1e3019b5d180b70fe29332843b41393fde4 Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Wed, 12 Jun 2019 22:25:18 +0200 Subject: [PATCH 1/3] horizontal-bar-charts notebook --- notebooks/horizontal-bar-charts.md | 144 +++++++++-------------------- 1 file changed, 45 insertions(+), 99 deletions(-) diff --git a/notebooks/horizontal-bar-charts.md b/notebooks/horizontal-bar-charts.md index 5e4414b9a..1b5388926 100644 --- a/notebooks/horizontal-bar-charts.md +++ b/notebooks/horizontal-bar-charts.md @@ -1,15 +1,26 @@ --- jupyter: jupytext: + notebook_metadata_filter: all text_representation: extension: .md format_name: markdown format_version: '1.1' jupytext_version: 1.1.1 kernelspec: - display_name: Python 2 + display_name: Python 3 language: python - name: python2 + name: python3 + language_info: + codemirror_mode: + name: ipython + version: 3 + file_extension: .py + mimetype: text/x-python + name: python + nbconvert_exporter: python + pygments_lexer: ipython3 + version: 3.6.7 plotly: description: How to make horizontal bar charts in Python with Plotly. display_as: basic @@ -25,77 +36,57 @@ jupyter: title: Horizontal Bar Charts | plotly --- -#### New to Plotly? -Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/). -
You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online). -
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started! -#### Version Check -Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version. +See more examples of bar charts (including vertical bar charts) and styling options [here](https://plot.ly/python/bar-charts/). -```python -import plotly -plotly.__version__ -``` #### Basic Horizontal Bar Chart ```python -import plotly.plotly as py import plotly.graph_objs as go data = [go.Bar( x=[20, 14, 23], y=['giraffes', 'orangutans', 'monkeys'], - orientation = 'h' + orientation='h' )] -py.iplot(data, filename='horizontal-bar') +fig = go.Figure(data=data) +fig.show() ``` ### Colored Horizontal Bar Chart ```python -import plotly.plotly as py import plotly.graph_objs as go -trace1 = go.Bar( +trace0 = go.Bar( y=['giraffes', 'orangutans', 'monkeys'], x=[20, 14, 23], name='SF Zoo', - orientation = 'h', - marker = dict( - color = 'rgba(246, 78, 139, 0.6)', - line = dict( - color = 'rgba(246, 78, 139, 1.0)', - width = 3) + orientation='h', + marker=dict( + color='rgba(246, 78, 139, 0.6)', + line=dict(color='rgba(246, 78, 139, 1.0)', width=3) ) ) -trace2 = go.Bar( +trace1 = go.Bar( y=['giraffes', 'orangutans', 'monkeys'], x=[12, 18, 29], name='LA Zoo', - orientation = 'h', - marker = dict( - color = 'rgba(58, 71, 80, 0.6)', - line = dict( - color = 'rgba(58, 71, 80, 1.0)', - width = 3) + orientation='h', + marker=dict( + color='rgba(58, 71, 80, 0.6)', + line=dict(color='rgba(58, 71, 80, 1.0)', width=3) ) ) -data = [trace1, trace2] -layout = go.Layout( - barmode='stack' -) - -fig = go.Figure(data=data, layout=layout) -py.iplot(fig, filename='marker-h-bar') +fig = go.Figure(data=[trace0, trace1]) +fig.update(layout_barmode='stack') ``` ### Color Palette for Bar Chart ```python -import plotly.plotly as py import plotly.graph_objs as go top_labels = ['Strongly
agree', 'Agree', 'Neutral', 'Disagree', @@ -122,14 +113,11 @@ traces = [] for i in range(0, len(x_data[0])): for xd, yd in zip(x_data, y_data): traces.append(go.Bar( - x=[xd[i]], - y=[yd], + x=[xd[i]], y=[yd], orientation='h', marker=dict( color=colors[i], - line=dict( - color='rgb(248, 248, 249)', - width=1) + line=dict(color='rgb(248, 248, 249)', width=1) ) )) @@ -150,12 +138,7 @@ layout = go.Layout( barmode='stack', paper_bgcolor='rgb(248, 248, 255)', plot_bgcolor='rgb(248, 248, 255)', - margin=dict( - l=120, - r=10, - t=140, - b=80 - ), + margin=dict(l=120, r=10, t=140, b=80), showlegend=False, ) @@ -204,18 +187,16 @@ for yd, xd in zip(y_data, x_data): showarrow=False)) space += xd[i] -layout['annotations'] = annotations - +layout.update(annotations=annotations) fig = go.Figure(data=traces, layout=layout) -py.iplot(fig, filename='bar-colorscale') +fig.show() ``` ### Bar Chart with Line Plot ```python -import plotly.plotly as py import plotly.graph_objs as go -from plotly import tools +from plotly.subplots import make_subplots import numpy as np @@ -243,11 +224,9 @@ trace0 = go.Bar( orientation='h', ) trace1 = go.Scatter( - x=y_net_worth, - y=x_net_worth, + x=y_net_worth, y=x_net_worth, mode='lines+markers', - line=dict( - color='rgb(128, 0, 128)'), + line_color='rgb(128, 0, 128)', name='Household net worth, Million USD/capita', ) layout = dict( @@ -282,19 +261,8 @@ layout = dict( side='top', dtick=25000, ), - legend=dict( - x=0.029, - y=1.038, - font=dict( - size=10, - ), - ), - margin=dict( - l=100, - r=20, - t=70, - b=70, - ), + legend=dict(x=0.029, y=1.038, font_size=10), + margin=dict(l=100, r=20, t=70, b=70), paper_bgcolor='rgb(248, 248, 255)', plot_bgcolor='rgb(248, 248, 255)', ) @@ -327,44 +295,22 @@ annotations.append(dict(xref='paper', yref='paper', '(2015), Household savings (indicator), ' + 'Household net worth (indicator). doi: ' + '10.1787/cfc6f499-en (Accessed on 05 June 2015)', - font=dict(family='Arial', size=10, - color='rgb(150,150,150)'), + font=dict(family='Arial', size=10, color='rgb(150,150,150)'), showarrow=False)) -layout['annotations'] = annotations +layout.update(annotations=annotations) # Creating two subplots -fig = tools.make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True, - shared_yaxes=False, vertical_spacing=0.001) +fig = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True, + shared_yaxes=False, vertical_spacing=0.001) fig.append_trace(trace0, 1, 1) fig.append_trace(trace1, 1, 2) -fig['layout'].update(layout) -py.iplot(fig, filename='oecd-networth-saving-bar-line') +fig.update(layout=layout) +fig.show() ``` ### Reference See more examples of bar charts and styling options [here](https://plot.ly/python/bar-charts/).
See https://plot.ly/python/reference/#bar for more information and chart attribute options! -```python -from IPython.display import display, HTML - -display(HTML('')) -display(HTML('')) - -#! pip install git+https://github.com/plotly/publisher.git --upgrade -import publisher -publisher.publish( - 'horizontal-bars.ipynb', 'python/horizontal-bar-charts/', 'Horizontal Bar Charts | plotly', - 'How to make horizontal bar charts in Python with Plotly.', - title = 'Horizontal Bar Charts | plotly', - name = 'Horizontal Bar Charts', - thumbnail='thumbnail/horizontal-bar.jpg', language='python', - has_thumbnail='true', display_as='basic', order=5, - ipynb= '~notebook_demo/5') -``` - -```python - -``` \ No newline at end of file From 72c04e05f6e81f761822f436fc8d8ca19b602e5d Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Thu, 13 Jun 2019 08:17:59 +0200 Subject: [PATCH 2/3] addressed comments, added px example --- notebooks/horizontal-bar-charts.md | 41 +++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/notebooks/horizontal-bar-charts.md b/notebooks/horizontal-bar-charts.md index 1b5388926..b881f405f 100644 --- a/notebooks/horizontal-bar-charts.md +++ b/notebooks/horizontal-bar-charts.md @@ -34,23 +34,56 @@ jupyter: permalink: python/horizontal-bar-charts/ thumbnail: thumbnail/horizontal-bar.jpg title: Horizontal Bar Charts | plotly + v4upgrade: true --- See more examples of bar charts (including vertical bar charts) and styling options [here](https://plot.ly/python/bar-charts/). +### Horizontal Bar Chart with plotly express + +Plotly express functions take as argument a tidy [pandas DataFrame](https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html). For a horizontal bar char, use the `px.bar` function with `orientation='h'`. + + +#### Basic Horizontal Bar Chart with plotly express + +```python +import plotly_express as px +tips = px.data.tips() +fig = px.bar(tips, x="total_bill", y="day", orientation='h') +fig.show() +``` + +#### Configure horizontal bar chart + +In this example a column is used to color the bars, and we add the information from other columns to the hover data. + +```python +import plotly_express as px +tips = px.data.tips() +fig = px.bar(tips, x="total_bill", y="sex", color='day', orientation='h', + hover_data=["tip", "size"], + height=400, + title='Restaurant bills') +fig.show() +``` + +### Horizontal Bar Chart with go.Bar + +When data are not available as a tidy dataframe, you can use the more generic function `go.Bar` from `plotly.graph_objs`. All the options of `go.Bar` are documented in the reference https://plot.ly/python/reference/#bar + + #### Basic Horizontal Bar Chart ```python import plotly.graph_objs as go -data = [go.Bar( +trace = go.Bar( x=[20, 14, 23], y=['giraffes', 'orangutans', 'monkeys'], - orientation='h' -)] + orientation='h') -fig = go.Figure(data=data) +fig = go.Figure(data=[trace]) fig.show() ``` From 915b90722ce26b9657db0339aed7445b3b98f402 Mon Sep 17 00:00:00 2001 From: Emmanuelle Gouillart Date: Fri, 21 Jun 2019 09:57:21 +0200 Subject: [PATCH 3/3] plotly.express, graph_objects, update_layout --- notebooks/horizontal-bar-charts.md | 84 ++++++++++++++---------------- 1 file changed, 40 insertions(+), 44 deletions(-) diff --git a/notebooks/horizontal-bar-charts.md b/notebooks/horizontal-bar-charts.md index b881f405f..07837c083 100644 --- a/notebooks/horizontal-bar-charts.md +++ b/notebooks/horizontal-bar-charts.md @@ -48,7 +48,7 @@ Plotly express functions take as argument a tidy [pandas DataFrame](https://pand #### Basic Horizontal Bar Chart with plotly express ```python -import plotly_express as px +import plotly.express as px tips = px.data.tips() fig = px.bar(tips, x="total_bill", y="day", orientation='h') fig.show() @@ -59,7 +59,7 @@ fig.show() In this example a column is used to color the bars, and we add the information from other columns to the hover data. ```python -import plotly_express as px +import plotly.express as px tips = px.data.tips() fig = px.bar(tips, x="total_bill", y="sex", color='day', orientation='h', hover_data=["tip", "size"], @@ -70,29 +70,29 @@ fig.show() ### Horizontal Bar Chart with go.Bar -When data are not available as a tidy dataframe, you can use the more generic function `go.Bar` from `plotly.graph_objs`. All the options of `go.Bar` are documented in the reference https://plot.ly/python/reference/#bar +When data are not available as a tidy dataframe, you can use the more generic function `go.Bar` from `plotly.graph_objects`. All the options of `go.Bar` are documented in the reference https://plot.ly/python/reference/#bar #### Basic Horizontal Bar Chart ```python -import plotly.graph_objs as go +import plotly.graph_objects as go -trace = go.Bar( +fig = go.Figure(go.Bar( x=[20, 14, 23], y=['giraffes', 'orangutans', 'monkeys'], - orientation='h') + orientation='h')) -fig = go.Figure(data=[trace]) fig.show() ``` ### Colored Horizontal Bar Chart ```python -import plotly.graph_objs as go +import plotly.graph_objects as go -trace0 = go.Bar( +fig = go.Figure() +fig.add_trace(go.Bar( y=['giraffes', 'orangutans', 'monkeys'], x=[20, 14, 23], name='SF Zoo', @@ -101,8 +101,8 @@ trace0 = go.Bar( color='rgba(246, 78, 139, 0.6)', line=dict(color='rgba(246, 78, 139, 1.0)', width=3) ) -) -trace1 = go.Bar( +)) +fig.add_trace(go.Bar( y=['giraffes', 'orangutans', 'monkeys'], x=[12, 18, 29], name='LA Zoo', @@ -111,16 +111,16 @@ trace1 = go.Bar( color='rgba(58, 71, 80, 0.6)', line=dict(color='rgba(58, 71, 80, 1.0)', width=3) ) -) +)) -fig = go.Figure(data=[trace0, trace1]) -fig.update(layout_barmode='stack') +fig.update_layout(barmode='stack') +fig.show() ``` ### Color Palette for Bar Chart ```python -import plotly.graph_objs as go +import plotly.graph_objects as go top_labels = ['Strongly
agree', 'Agree', 'Neutral', 'Disagree', 'Strongly
disagree'] @@ -140,12 +140,11 @@ y_data = ['The course was effectively
organized', 'my
ability to think critically about
the subject', 'I would recommend this
course to a friend'] - -traces = [] +fig = go.Figure() for i in range(0, len(x_data[0])): for xd, yd in zip(x_data, y_data): - traces.append(go.Bar( + fig.add_trace(go.Bar( x=[xd[i]], y=[yd], orientation='h', marker=dict( @@ -154,7 +153,7 @@ for i in range(0, len(x_data[0])): ) )) -layout = go.Layout( +fig.update_layout( xaxis=dict( showgrid=False, showline=False, @@ -220,15 +219,15 @@ for yd, xd in zip(y_data, x_data): showarrow=False)) space += xd[i] -layout.update(annotations=annotations) -fig = go.Figure(data=traces, layout=layout) +fig.update_layout(annotations=annotations) + fig.show() ``` ### Bar Chart with Line Plot ```python -import plotly.graph_objs as go +import plotly.graph_objects as go from plotly.subplots import make_subplots import numpy as np @@ -239,14 +238,17 @@ y_saving = [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996, y_net_worth = [93453.919999999998, 81666.570000000007, 69889.619999999995, 78381.529999999999, 141395.29999999999, 92969.020000000004, 66090.179999999993, 122379.3] -x_saving = ['Japan', 'United Kingdom', 'Canada', 'Netherlands', - 'United States', 'Belgium', 'Sweden', 'Switzerland'] -x_net_worth = ['Japan', 'United Kingdom', 'Canada', 'Netherlands', - 'United States', 'Belgium', 'Sweden', 'Switzerland' - ] -trace0 = go.Bar( +x = ['Japan', 'United Kingdom', 'Canada', 'Netherlands', + 'United States', 'Belgium', 'Sweden', 'Switzerland'] + + +# Creating two subplots +fig = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True, + shared_yaxes=False, vertical_spacing=0.001) + +fig.append_trace(go.Bar( x=y_saving, - y=x_saving, + y=x, marker=dict( color='rgba(50, 171, 96, 0.6)', line=dict( @@ -255,14 +257,16 @@ trace0 = go.Bar( ), name='Household savings, percentage of household disposable income', orientation='h', -) -trace1 = go.Scatter( - x=y_net_worth, y=x_net_worth, +), 1, 1) + +fig.append_trace(go.Scatter( + x=y_net_worth, y=x, mode='lines+markers', line_color='rgb(128, 0, 128)', name='Household net worth, Million USD/capita', -) -layout = dict( +), 1, 2) + +fig.update_layout( title='Household savings & net worth for eight OECD countries', yaxis=dict( showgrid=False, @@ -306,7 +310,7 @@ y_s = np.round(y_saving, decimals=2) y_nw = np.rint(y_net_worth) # Adding labels -for ydn, yd, xd in zip(y_nw, y_s, x_saving): +for ydn, yd, xd in zip(y_nw, y_s, x): # labeling the scatter savings annotations.append(dict(xref='x2', yref='y2', y=xd, x=ydn - 20000, @@ -331,16 +335,8 @@ annotations.append(dict(xref='paper', yref='paper', font=dict(family='Arial', size=10, color='rgb(150,150,150)'), showarrow=False)) -layout.update(annotations=annotations) - -# Creating two subplots -fig = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True, - shared_yaxes=False, vertical_spacing=0.001) - -fig.append_trace(trace0, 1, 1) -fig.append_trace(trace1, 1, 2) +fig.update_layout(annotations=annotations) -fig.update(layout=layout) fig.show() ```