Skip to content

Commit c39ba8f

Browse files
Merge pull request plotly#1391 from plotly/python-next-chart-studio
Python next chart studio
2 parents 460a68d + adf8e30 commit c39ba8f

18 files changed

+7244
-0
lines changed

_posts/python-next/chart-studio/data-api.ipynb

Lines changed: 620 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.1'
9+
jupytext_version: 1.1.7
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.5
24+
plotly:
25+
permalink: python/data-api/
26+
description: How to upload data to Plotly from Python with the Plotly Grid API.
27+
name: Upload Data to Plotly from Python
28+
has_thumbnail: true
29+
thumbnail: thumbnail/table.jpg
30+
layout: user-guide
31+
name: Plots from Grids
32+
language: python
33+
title: Plotly Data API
34+
display_as: chart_studio
35+
has_thumbnail: true
36+
page_type: u-guide
37+
order: 5
38+
v4upgrade: true
39+
---
40+
41+
#### Creating a Plotly Grid
42+
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:
43+
- `grid` (Grid Object): the actual grid object that you are uploading.
44+
- `filename` (str): name of the grid in your plotly account,
45+
- `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.
46+
- `auto_open` (bool): if determines if the grid is opened in the browser or not.
47+
48+
You can run `help(py.grid_ops.upload)` for a more detailed description of these and all the arguments.
49+
50+
```python
51+
import chart_studio
52+
import chart_studio.plotly as py
53+
import chart_studio.tools as tls
54+
import plotly.graph_objects as go
55+
from chart_studio.grid_objs import Column, Grid
56+
57+
from datetime import datetime as dt
58+
import numpy as np
59+
from IPython.display import IFrame
60+
61+
column_1 = Column(['a', 'b', 'c'], 'column 1')
62+
column_2 = Column([1, 2, 3], 'column 2') # Tabular data can be numbers, strings, or dates
63+
grid = Grid([column_1, column_2])
64+
url = py.grid_ops.upload(grid,
65+
filename='grid_ex_'+str(dt.now()),
66+
world_readable=True,
67+
auto_open=False)
68+
print(url)
69+
```
70+
71+
#### View and Share your Grid
72+
You can view your newly created grid at the `url`:
73+
74+
```python
75+
IFrame(src= url.rstrip('/') + ".embed", width="100%",height="200px", frameBorder="0")
76+
```
77+
78+
You are also able to view the grid in your list of files inside your [organize folder](https://plot.ly/organize).
79+
80+
81+
#### Upload Dataframes to Plotly
82+
Along with uploading a grid, you can upload a Dataframe as well as convert it to raw data as a grid:
83+
84+
```python
85+
import chart_studio.plotly as py
86+
import plotly.figure_factory as ff
87+
88+
import pandas as pd
89+
90+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv')
91+
df_head = df.head()
92+
table = ff.create_table(df_head)
93+
py.iplot(table, filename='dataframe_ex_preview')
94+
```
95+
96+
#### Making Graphs from Grids
97+
Plotly graphs are usually described with data embedded in them. For example, here we place `x` and `y` data directly into our `Histogram2dContour` object:
98+
99+
```python
100+
x = np.random.randn(1000)
101+
y = np.random.randn(1000) + 1
102+
103+
data = [
104+
go.Histogram2dContour(
105+
x=x,
106+
y=y
107+
)
108+
]
109+
110+
py.iplot(data, filename='Example 2D Histogram Contour')
111+
```
112+
113+
We can also create graphs based off of references to columns of grids. Here, we'll upload several `column`s to our Plotly account:
114+
115+
```python
116+
column_1 = Column(np.random.randn(1000), 'column 1')
117+
column_2 = Column(np.random.randn(1000)+1, 'column 2')
118+
column_3 = Column(np.random.randn(1000)+2, 'column 3')
119+
column_4 = Column(np.random.randn(1000)+3, 'column 4')
120+
121+
grid = Grid([column_1, column_2, column_3, column_4])
122+
url = py.grid_ops.upload(grid, filename='randn_int_offset_'+str(dt.now()))
123+
print(url)
124+
```
125+
126+
```python
127+
IFrame(src= url.rstrip('/') + ".embed", width="100%",height="200px", frameBorder="0")
128+
```
129+
130+
#### Make Graph from Raw Data
131+
Instead of placing data into `x` and `y`, we'll place our Grid columns into `xsrc` and `ysrc`:
132+
133+
```python
134+
data = [
135+
go.Histogram2dContour(
136+
xsrc=grid[0],
137+
ysrc=grid[1]
138+
)
139+
]
140+
141+
py.iplot(data, filename='2D Contour from Grid Data')
142+
```
143+
144+
So, when you view the data, you'll see your original grid, not just the columns that compose this graph:
145+
146+
147+
#### Attaching Meta Data to Grids
148+
In [Chart Studio Enterprise](https://plot.ly/product/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.
149+
150+
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.
151+
152+
```python
153+
meta = {
154+
"Month": "November",
155+
"Experiment ID": "d3kbd",
156+
"Operator": "James Murphy",
157+
"Initial Conditions": {
158+
"Voltage": 5.5
159+
}
160+
}
161+
162+
grid_url = py.grid_ops.upload(grid, filename='grid_with_metadata_'+str(dt.now()), meta=meta)
163+
print(url)
164+
```
165+
166+
#### Reference
167+
168+
```python
169+
help(py.grid_ops)
170+
```
171+
172+
```python
173+
174+
```

0 commit comments

Comments
 (0)