• Active Topics 

Install, Configure and Test Plotly's Python Package

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

Plot.ly or Plotly is an online analytics and data visualization tool, headquartered in Montreal, Quebec. Plot.ly provides online graphing, analytics, and statistics tools for individuals and collaboration, as well as scientific graphing libraries for Python, R, MATLAB, Perl, Julia, Arduino, and REST.

Plotly Installation

To install Plotly's Python Package on Ubuntu Linux (it is straightforward to install on other operating systems), open the command line and follow the procedures below:
  1. $ pip install plotly
  2. or
  3. $ sudo pip install plotly
  4. #Plotly's Python package is updated frequently! To upgrade, run:
  5. $ pip install plotly --upgrade

Initialize Plotly for Online Plotting

Plotly provides a web-service for hosting graphs you generate! You can create a free account to be able to save your graphs inside your online Plotly account and decide how you control the privacy. Public hosting is free.

To initialize Plotly for online plotting and graph hosting, open the command line and follow the steps below:
  1. #Invoke Python/Ipython interpreter
  2. $python
  3. #Import plotly and set your credentials:
  4. import plotly.tools as tls
  5. tls.set_credentials_file(username='Your_Account_Name', api_key='API_Key') #Insert Your_Account_Name and API_Key, use our account name TSSFL and  API Key: VIrC8tjxOn2nwujbiwrk for demo

You'll need to replace 'Your_Account_Name' and 'API_Key' with your Plotly username (of your choice) and API key which you can find here.

The initialization step places a special .plotly/ credentials file in your home directory, which should look something like this:
  1. {
  2.     "username": "Your_Account_Name",
  3.     "stream_ids": ["ylosqsyet5", "h2ct8btk1s", "oxz4fm883b"],
  4.     "api_key": "lr1c37zw81"
  5. }

Plot a graph and automatically publish it to Plotly Account

We create a simple Python program to plot and automatically upload the graph to our public Plotly account: https://plot.ly/~TSSFL
  1. """Plotly Demo"""
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import plotly.plotly as py
  5. graph = plt.figure()
  6. data = np.loadtxt("gals-DR123b_t1.txt", skiprows=3)
  7. number_of_colums = len(data[:,0]) #196237
  8. number_of_rows = len(data[0,:]) #42
  9. C5 = data[:,0:5] #From first to fifth columns
  10. C15 = data[:,10:15] #From eleventh to fifteenth columns
  11. fb1 = C5[:,2]  #First frequency band in C5
  12. fb2 = C15[:,2]  #First frequency band in C15
  13. eccentricity = fb2/fb1
  14. #Plot histogram
  15. bins = 50
  16. eccentricity = eccentricity[eccentricity<1.0]
  17. plt.hist(eccentricity[~np.isnan(eccentricity)], bins, normed=False, facecolor='green', alpha=.5)  #Get rid of 'NAN', you can use bins = 'auto'
  18. plt.xlabel('Epsilon')
  19. plt.ylabel('Number of Galaxies')
  20. plt.title("Histogram")
  21. plot_url = py.plot_mpl(graph, filename='Test_Plotly.png')

This line

Code: Select all

plot_url = py.plot_mpl(graph, filename='Test_Plotly.png')
is what actually does the uploading to our Plotly public account. Click here to see the generated plot, which should look like this:

Image

Currently, plotly provides libraries for:

Scala
ggplot2
R
JS plotly.js
Python
Pandas
node.js
Matplotlib
MATLAB
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#3

The plotly.plotly module is deprecated, and has been replaced by the chart-studio package which offers the chart_studio.plotly module instead.

To install Chart Studio's Python package, use the package manager pip inside your terminal:

  1. $ pip install chart_studio


or

  1. $ sudo pip install chart_studio



Plotly's Python package is installed alongside the Chart Studio package and it is updated frequently. To upgrade, run:


  1. $ pip install plotly --upgrade



You can find out more here.


In my case the pip is upgraded using the command:

  1. /home/user/anaconda/bin/python3 -m pip install --upgrade pip


Setting credentials has been changed to:

  1. import chart_studio
  2. chart_studio.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81') #Replace the account name and the API Key with yours


Plotting online can then be achieved as demonstrated by this simple code:

  1. import chart_studio.plotly as py
  2. import plotly.graph_objects as go
  3.  
  4. trace0 = go.Scatter(
  5.     x=[1, 2, 3, 4],
  6.     y=[10, 15, 13, 17]
  7. )
  8. trace1 = go.Scatter(
  9.     x=[1, 2, 3, 4],
  10.     y=[16, 5, 11, 9]
  11. )
  12. data = [trace0, trace1]
  13.  
  14. py.plot(data, filename = 'basic-line', auto_open=True)


Finally, we can Generate the Plotly Graph Embeddable Code for our Forum

You can generate an iframe for embedding the plotly graph at TSSFL Open Discussion Forums. Suppose we want to embed the Plotly graph located by the url https://chart-studio.plotly.com/~TSSFL/24/#/, the code below can then be used to generate the iframe code which can be used to embed the graph:

  1. import chart_studio.tools as tls
  2. tls.get_embed('https://plot.ly/~TSSFL/24/') #change url appropriately


You can embed interactive visualizations hosted either on Plotly or GitHub repository/pages, see here.

See embedded demos.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “Plotly”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests