Show Sidebar Hide Sidebar

Plots from Grids in Python

How to upload data to Plotly from Python with the Plotly Grid API.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Creating a Plotly Grid

You can instantiate a grid with data by either uploading tabular data to Plotly or by creating a Plotly grid using the API. To upload the grid we will use plotly.plotly.grid_ops.upload(). It takes the following arguments:

  • grid (Grid Object): the actual grid object that you are uploading.
  • filename (str): name of the grid in your plotly account,
  • world_readable (bool): if True, the grid is public and can be viewed by anyone in your files. If False, it is private and can only be viewed by you.
  • auto_open (bool): if determines if the grid is opened in the browser or not.

You can run help(py.grid_ops.upload) for a more detailed description of these and all the arguments.

In [1]:
import plotly
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
from plotly.grid_objs import Column, Grid

import datetime
import numpy as np
from IPython.display import Image

column_1 = Column([1, 2, 3], 'column 1')
column_2 = Column(['a', 'b', datetime.datetime.now()], 'column 2') # Tabular data can be numbers, strings, or dates
grid = Grid([column_1, column_2])
url = py.grid_ops.upload(grid, filename='grid_ex_6', world_readable=True, auto_open=True)
print(url)
https://plot.ly/~PythonPlotBot/370/

View and Share your Grid

You can view your newly created grid at the url:

In [3]:
Image('view_grid_url.png')
Out[3]:

You are also able to view the grid in your list of files inside Organize and in your profile if the grid is public:

In [4]:
Image('view_in_organize.png')
Out[4]:

Upload Dataframes to Plotly

Along with uploading a grid, you can upload a Dataframe as well as convert it to raw data as a grid:

In [7]:
import plotly.plotly as py
import plotly.figure_factory

import pandas_datareader.data as web
from datetime import datetime

df = web.DataReader("aapl", 'yahoo', datetime(2007, 10, 1), datetime(2009, 4, 1))
df_head = df.head(6)
table = plotly.figure_factory.create_table(df_head)
py.iplot(table, filename='dataframe_ex_preview')
Out[7]:
In [8]:
grid = Grid([Column(df[column_name], column_name) for column_name in df.columns])
url = py.grid_ops.upload(grid, filename='dataframe_ex_2', world_readable=True, auto_open=True)
print(url)
https://plot.ly/~PythonPlotBot/373/
In [9]:
Image('dataframe_view.png')
Out[9]:

Making Graphs from Grids

Plotly graphs are usually described with data embedded in them. For example, here we place x and y data directly into our Histogram2dContour object:

In [10]:
x = np.random.randn(1000)
y = np.random.randn(1000) + 1

data = Data([
    Histogram2dContour(
        x=x,
        y=y
    )
])

py.iplot(data, filename='Example 2D Histogram Contour')
Out[10]:

We can also create graphs based off of references to columns of grids. Here, we'll upload several columns to our Plotly account:

In [11]:
column_1 = Column(np.random.randn(1000), 'column 1')
column_2 = Column(np.random.randn(1000)+1, 'column 2')
column_3 = Column(np.random.randn(1000)+2, 'column 3')
column_4 = Column(np.random.randn(1000)+3, 'column 4')

grid = Grid([column_1, column_2, column_3, column_4])
url = py.grid_ops.upload(grid, filename='randn_with_integer_offset_2')
print(url)
https://plot.ly/~PythonPlotBot/376/
In [12]:
Image('rand_int_histogram_view.png')
Out[12]:

Make Graph from Raw Data

Instead of placing data into x and y, we'll place our Grid columns into xsrc and ysrc:

In [13]:
data = Data([
    Histogram2dContour(
        xsrc=grid[0],
        ysrc=grid[1]
    )
])

py.iplot(data, filename='2D Contour from Grid Data')
Out[13]:

So, when you view the data, you'll see your original grid, not just the columns that compose this graph:

In [14]:
Image('graph_from_raw_data_view.png')
Out[14]:

Attaching Meta Data to Grids

In Plotly Enterprise, you can upload and assign free-form JSON metadata to any grid object. This means that you can keep all of your raw data in one place, under one grid.

If you update the original data source, in the workspace or with our API, all of the graphs that are sourced from it will be updated as well. You can make multiple graphs from a single Grid and you can make a graph from multiple grids. You can also add rows and columns to existing grids programatically.

In [15]:
meta = {
    "Month": "November",
    "Experiment ID": "d3kbd",
    "Operator": "James Murphy",
    "Initial Conditions": {
          "Voltage": 5.5
    }
}

grid_url = py.grid_ops.upload(grid, filename='grid_with_metadata_2', meta=meta)
print(url)
https://plot.ly/~PythonPlotBot/376/
In [16]:
Image('metadata_view.png')
Out[16]:

Reference

In [17]:
help(py.grid_ops)
Help on class grid_ops in module plotly.plotly.plotly:

class grid_ops
 |  Interface to Plotly's Grid API.
 |  Plotly Grids are Plotly's tabular data object, rendered
 |  in an online spreadsheet. Plotly graphs can be made from
 |  references of columns of Plotly grid objects. Free-form
 |  JSON Metadata can be saved with Plotly grids.
 |  
 |  To create a Plotly grid in your Plotly account from Python,
 |  see `grid_ops.upload`.
 |  
 |  To add rows or columns to an existing Plotly grid, see
 |  `grid_ops.append_rows` and `grid_ops.append_columns`
 |  respectively.
 |  
 |  To delete one of your grid objects, see `grid_ops.delete`.
 |  
 |  Class methods defined here:
 |  
 |  append_columns(cls, columns, grid=None, grid_url=None) from __builtin__.classobj
 |      Append columns to a Plotly grid.
 |      
 |      `columns` is an iterable of plotly.grid_objs.Column objects
 |      and only one of `grid` and `grid_url` needs to specified.
 |      
 |      `grid` is a ploty.grid_objs.Grid object that has already been
 |      uploaded to plotly with the grid_ops.upload method.
 |      
 |      `grid_url` is a unique URL of a `grid` in your plotly account.
 |      
 |      Usage example 1: Upload a grid to Plotly, and then append a column
 |      ```
 |      from plotly.grid_objs import Grid, Column
 |      import plotly.plotly as py
 |      column_1 = Column([1, 2, 3], 'time')
 |      grid = Grid([column_1])
 |      py.grid_ops.upload(grid, 'time vs voltage')
 |      
 |      # append a column to the grid
 |      column_2 = Column([4, 2, 5], 'voltage')
 |      py.grid_ops.append_columns([column_2], grid=grid)
 |      ```
 |      
 |      Usage example 2: Append a column to a grid that already exists on
 |                       Plotly
 |      ```
 |      from plotly.grid_objs import Grid, Column
 |      import plotly.plotly as py
 |      
 |      grid_url = 'https://plot.ly/~chris/3143'
 |      column_1 = Column([1, 2, 3], 'time')
 |      py.grid_ops.append_columns([column_1], grid_url=grid_url)
 |      ```
 |  
 |  append_rows(cls, rows, grid=None, grid_url=None) from __builtin__.classobj
 |      Append rows to a Plotly grid.
 |      
 |      `rows` is an iterable of rows, where each row is a
 |      list of numbers, strings, or dates. The number of items
 |      in each row must be equal to the number of columns
 |      in the grid. If appending rows to a grid with columns of
 |      unequal length, Plotly will fill the columns with shorter
 |      length with empty strings.
 |      
 |      Only one of `grid` and `grid_url` needs to specified.
 |      
 |      `grid` is a ploty.grid_objs.Grid object that has already been
 |      uploaded to plotly with the grid_ops.upload method.
 |      
 |      `grid_url` is a unique URL of a `grid` in your plotly account.
 |      
 |      Usage example 1: Upload a grid to Plotly, and then append rows
 |      ```
 |      from plotly.grid_objs import Grid, Column
 |      import plotly.plotly as py
 |      column_1 = Column([1, 2, 3], 'time')
 |      column_2 = Column([5, 2, 7], 'voltage')
 |      grid = Grid([column_1, column_2])
 |      py.grid_ops.upload(grid, 'time vs voltage')
 |      
 |      # append a row to the grid
 |      row = [1, 5]
 |      py.grid_ops.append_rows([row], grid=grid)
 |      ```
 |      
 |      Usage example 2: Append a row to a grid that already exists on Plotly
 |      ```
 |      from plotly.grid_objs import Grid
 |      import plotly.plotly as py
 |      
 |      grid_url = 'https://plot.ly/~chris/3143'
 |      
 |      row = [1, 5]
 |      py.grid_ops.append_rows([row], grid=grid_url)
 |      ```
 |  
 |  delete(cls, grid=None, grid_url=None) from __builtin__.classobj
 |      Delete a grid from your Plotly account.
 |      
 |      Only one of `grid` or `grid_url` needs to be specified.
 |      
 |      `grid` is a plotly.grid_objs.Grid object that has already
 |             been uploaded to Plotly.
 |      
 |      `grid_url` is the URL of the Plotly grid to delete
 |      
 |      Usage example 1: Upload a grid to plotly, then delete it
 |      ```
 |      from plotly.grid_objs import Grid, Column
 |      import plotly.plotly as py
 |      column_1 = Column([1, 2, 3], 'time')
 |      column_2 = Column([4, 2, 5], 'voltage')
 |      grid = Grid([column_1, column_2])
 |      py.grid_ops.upload(grid, 'time vs voltage')
 |      
 |      # now delete it, and free up that filename
 |      py.grid_ops.delete(grid)
 |      ```
 |      
 |      Usage example 2: Delete a plotly grid by url
 |      ```
 |      import plotly.plotly as py
 |      
 |      grid_url = 'https://plot.ly/~chris/3'
 |      py.grid_ops.delete(grid_url=grid_url)
 |      ```
 |  
 |  upload(cls, grid, filename, world_readable=True, auto_open=True, meta=None) from __builtin__.classobj
 |      Upload a grid to your Plotly account with the specified filename.
 |      
 |      Positional arguments:
 |          - grid: A plotly.grid_objs.Grid object,
 |                  call `help(plotly.grid_ops.Grid)` for more info.
 |          - filename: Name of the grid to be saved in your Plotly account.
 |                      To save a grid in a folder in your Plotly account,
 |                      separate specify a filename with folders and filename
 |                      separated by backslashes (`/`).
 |                      If a grid, plot, or folder already exists with the same
 |                      filename, a `plotly.exceptions.RequestError` will be
 |                      thrown with status_code 409
 |      
 |      Optional keyword arguments:
 |          - world_readable (default=True): make this grid publically (True)
 |                                           or privately (False) viewable.
 |          - auto_open (default=True): Automatically open this grid in
 |                                      the browser (True)
 |          - meta (default=None): Optional Metadata to associate with
 |                                 this grid.
 |                                 Metadata is any arbitrary
 |                                 JSON-encodable object, for example:
 |                                 `{"experiment name": "GaAs"}`
 |      
 |      Filenames must be unique. To overwrite a grid with the same filename,
 |      you'll first have to delete the grid with the blocking name. See
 |      `plotly.plotly.grid_ops.delete`.
 |      
 |      Usage example 1: Upload a plotly grid
 |      ```
 |      from plotly.grid_objs import Grid, Column
 |      import plotly.plotly as py
 |      column_1 = Column([1, 2, 3], 'time')
 |      column_2 = Column([4, 2, 5], 'voltage')
 |      grid = Grid([column_1, column_2])
 |      py.grid_ops.upload(grid, 'time vs voltage')
 |      ```
 |      
 |      Usage example 2: Make a graph based with data that is sourced
 |                       from a newly uploaded Plotly grid
 |      ```
 |      import plotly.plotly as py
 |      from plotly.grid_objs import Grid, Column
 |      from plotly.graph_objs import Scatter
 |      # Upload a grid
 |      column_1 = Column([1, 2, 3], 'time')
 |      column_2 = Column([4, 2, 5], 'voltage')
 |      grid = Grid([column_1, column_2])
 |      py.grid_ops.upload(grid, 'time vs voltage')
 |      
 |      # Build a Plotly graph object sourced from the
 |      # grid's columns
 |      trace = Scatter(xsrc=grid[0], ysrc=grid[1])
 |      py.plot([trace], filename='graph from grid')
 |      ```
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  ensure_uploaded(fid)

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.