#!/usr/bin/env python # coding: utf-8 # #### New to Plotly? # 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/). #
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). #
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 # Note: Distplots are available in version 1.11.0+
# Run `pip install plotly --upgrade` to update your Plotly version # In[1]: import plotly plotly.__version__ # #### Basic Distplot # In[2]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np x = np.random.randn(1000) hist_data = [x] group_labels = ['distplot'] fig = ff.create_distplot(hist_data, group_labels) py.iplot(fig, filename='Basic Distplot') # #### Plot Multiple Datasets # In[3]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np # Add histogram data x1 = np.random.randn(200)-2 x2 = np.random.randn(200) x3 = np.random.randn(200)+2 x4 = np.random.randn(200)+4 # Group data together hist_data = [x1, x2, x3, x4] group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4'] # Create distplot with custom bin_size fig = ff.create_distplot(hist_data, group_labels, bin_size=.2) # Plot! py.iplot(fig, filename='Distplot with Multiple Datasets') # #### Use Multiple Bin Sizes # In[4]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np # Add histogram data x1 = np.random.randn(200)-2 x2 = np.random.randn(200) x3 = np.random.randn(200)+2 x4 = np.random.randn(200)+4 # Group data together hist_data = [x1, x2, x3, x4] group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4'] # Create distplot with custom bin_size fig = ff.create_distplot(hist_data, group_labels, bin_size=[.1, .25, .5, 1]) # Plot! py.iplot(fig, filename='Distplot with Multiple Bin Sizes') # #### Customize Rug Text, Colors & Title # In[5]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np x1 = np.random.randn(26) x2 = np.random.randn(26) + .5 hist_data = [x1, x2] group_labels = ['2014', '2015'] rug_text_one = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] rug_text_two = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj', 'kk', 'll', 'mm', 'nn', 'oo', 'pp', 'qq', 'rr', 'ss', 'tt', 'uu', 'vv', 'ww', 'xx', 'yy', 'zz'] rug_text = [rug_text_one, rug_text_two] colors = ['rgb(0, 0, 100)', 'rgb(0, 200, 200)'] # Create distplot with custom bin_size fig = ff.create_distplot( hist_data, group_labels, bin_size=.2, rug_text=rug_text, colors=colors) fig['layout'].update(title='Customized Distplot') # Plot! py.iplot(fig, filename='Distplot Colors') # #### Plot Normal Curve # In[6]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np x1 = np.random.randn(200) x2 = np.random.randn(200) + 2 hist_data = [x1, x2] group_labels = ['Group 1', 'Group 2'] colors = ['#3A4750', '#F64E8B'] # Create distplot with curve_type set to 'normal' fig = ff.create_distplot(hist_data, group_labels, bin_size=.5, curve_type='normal', colors=colors) # Add title fig['layout'].update(title='Distplot with Normal Distribution') # Plot! py.iplot(fig, filename='Distplot with Normal Curve') # #### Plot Only Curve and Rug # In[7]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np x1 = np.random.randn(200) - 1 x2 = np.random.randn(200) x3 = np.random.randn(200) + 1 hist_data = [x1, x2, x3] group_labels = ['Group 1', 'Group 2', 'Group 3'] colors = ['#333F44', '#37AA9C', '#94F3E4'] # Create distplot with curve_type set to 'normal' fig = ff.create_distplot(hist_data, group_labels, show_hist=False, colors=colors) # Add title fig['layout'].update(title='Curve and Rug Plot') # Plot! py.iplot(fig, filename='Curve and Rug') # #### Plot Only Hist and Rug # In[8]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np x1 = np.random.randn(200) - 1 x2 = np.random.randn(200) x3 = np.random.randn(200) + 1 hist_data = [x1, x2, x3] group_labels = ['Group 1', 'Group 2', 'Group 3'] colors = ['#835AF1', '#7FA6EE', '#B8F7D4'] # Create distplot with curve_type set to 'normal' fig = ff.create_distplot(hist_data, group_labels, colors=colors, bin_size=.25, show_curve=False) # Add title fig['layout'].update(title='Hist and Rug Plot') # Plot! py.iplot(fig, filename='Hist and Rug') # #### Plot Hist and Rug with Different Bin Sizes # In[9]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np x1 = np.random.randn(200) - 2 x2 = np.random.randn(200) x3 = np.random.randn(200) + 2 hist_data = [x1, x2, x3] group_labels = ['Group 1', 'Group 2', 'Group 3'] colors = ['#393E46', '#2BCDC1', '#F66095'] # Create distplot with curve_type set to 'normal' fig = ff.create_distplot(hist_data, group_labels, colors=colors, bin_size=[0.3, 0.2, 0.1], show_curve=False) # Add title fig['layout'].update(title='Hist and Rug Plot') # Plot! py.iplot(fig, filename='Hist and Rug Different Bin Size') # #### Plot Only Hist and Curve # In[10]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np x1 = np.random.randn(200) - 2 x2 = np.random.randn(200) x3 = np.random.randn(200) + 2 hist_data = [x1, x2, x3] group_labels = ['Group 1', 'Group 2', 'Group 3'] colors = ['#A56CC1', '#A6ACEC', '#63F5EF'] # Create distplot with curve_type set to 'normal' fig = ff.create_distplot(hist_data, group_labels, colors=colors, bin_size=.2, show_rug=False) # Add title fig['layout'].update(title='Hist and Curve Plot') # Plot! py.iplot(fig, filename='Hist and Curve') # #### Distplot with Pandas # In[11]: import plotly.plotly as py import plotly.figure_factory as ff import numpy as np import pandas as pd df = pd.DataFrame({'2012': np.random.randn(200), '2013': np.random.randn(200)+1}) py.iplot(ff.create_distplot([df[c] for c in df.columns], df.columns, bin_size=.25), filename='distplot with pandas') # #### Reference # In[12]: help(ff.create_distplot) # In[13]: from IPython.display import display, HTML display(HTML('')) display(HTML('')) get_ipython().system('pip install git+https://github.com/plotly/publisher.git --upgrade') import publisher publisher.publish( 'distplots.ipynb', 'python/distplot/', 'Python Distplots | plotly', 'How to make interactive Distplots in Python with Plotly. ', title = 'Python Distplots | plotly', name = 'Distplots', has_thumbnail='true', thumbnail='thumbnail/distplot.jpg', language='python', page_type='example_index', display_as='statistical', order=5, ipynb= '~notebook_demo/23') # In[ ]: