Skip to content

Commit 2acc83b

Browse files
committed
new ff readme
1 parent 728ce95 commit 2acc83b

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

Diff for: plotly/add_figure_factory_to_plotly_library.md

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Add A Figure Factory to the Plotly [Python Library](https://plot.ly/python/)
2+
3+
## What is a Figure Factory?
4+
In the Python Plotly Library, we have very basic plot types that are created using the `plotly.graph_objs` module. These plot types include Scatter, Box and Bar types. They are the basis of the plots and charts instatiated by Plotly.
5+
6+
To create a basic chart like this, first we create the `data` using the tools in `plotly.graph_objs` and then we plot it. For example:
7+
8+
```
9+
import plotly.plotly as py
10+
import plotly.graph_objs as go
11+
12+
trace = go.Scatter(
13+
x = [1, 2, 3],
14+
y = [4, 5, 6],
15+
mode = 'markers',
16+
)
17+
18+
data = [trace]
19+
20+
py.iplot(data, filename='new-scatter-plot')
21+
```
22+
23+
There is another type of chart which uses these basic plot types to make other types of graphs, and this is the figure factories. These are wrappers that utilize the code from `plotly.graph_objs` to build charts that can use their structures. A good example of a figure factory is the [Scatterplot Matrix](https://plot.ly/python/scatterplot-matrix/) as it utilizes `go.Scatter`, `go.Box` and `go.Histogram`.
24+
25+
So if you have ever wanted to contribute to the Plotly Python Library by adding a new chart type we don't have, now you can! This README will help you get started by cloning the plotly.py repo, forking a new branch, creating a new figure factory, and creatng a new Pull Request to get feedback for merging. Just follow all these steps and you'll be ready to go.
26+
27+
## Getting Started:
28+
1. In the Terminal, clone the `plotly.py` repo locally and then check out the master branch.
29+
30+
```
31+
$ git clone [email protected]:plotly/plotly.py.git
32+
$ git fetch origin
33+
$ git checkout master
34+
```
35+
36+
2. Create a new branch off the master branch and give it an appropriate name.
37+
38+
```
39+
$ git checkout -b "add-ff-type"
40+
```
41+
42+
## Create a figure_factory File
43+
1. Creating python file
44+
45+
Move to the `plotly/figure_factory` directory in the `plotly.py` repo. To do this, open up the Terminal and excute the command:
46+
47+
```
48+
cd plotly/figure_factory
49+
```
50+
51+
By running `ls` in the Terminal, you will get a list of all files in your current directory. In the `plotly/figure_factory` directory there is an `__init__.py` file as well as a bunch of `_ff_type.py` files. Each figure factory chart gets its own python file, and the name of each of these python files are found in the `__init__.py` file.
52+
53+
If you are making a chart called `foo`, then you must create `_foo.py` in this directory.
54+
55+
56+
2. Updating `__init__.py`
57+
58+
The inside of the `__init__.py` looks like:
59+
60+
```
61+
from __future__ import absolute_import
62+
63+
# Require that numpy exists for figure_factory
64+
import numpy
65+
66+
from plotly.figure_factory._2d_density import create_2d_density
67+
from plotly.figure_factory._annotated_heatmap import create_annotated_heatmap
68+
from plotly.figure_factory._candlestick import create_candlestick
69+
...
70+
```
71+
72+
Now add the following line to the end of `__init__.py`:
73+
74+
```
75+
from plotly.figure_factory._foo import create_foo
76+
```
77+
78+
3. Imports
79+
80+
In `_foo.py` write
81+
82+
```
83+
from __future__ import absolute_import
84+
```
85+
86+
at line 1. You can add other imports later if you will need them.
87+
88+
4. The main function
89+
90+
It's now time to write the main function `create_foo` that will be called directly by the user. It has the form:
91+
92+
```
93+
def create_foo(attribute1, attribute2=value, ...):
94+
"""
95+
Returns figure for a foo plot.
96+
97+
:param (type) attribute1: description of 'attribute1'.
98+
:param (type) attribute2: description of what 'attribute2' is.
99+
Default = value
100+
# ...
101+
102+
Example 1:
103+
'''
104+
105+
'''
106+
107+
Example 2:
108+
'''
109+
110+
'''
111+
"""
112+
# code goes here
113+
return fig
114+
```
115+
116+
You _must_ include a documentation string in your function. A doc string is a block string that contains useful information about what the function does, the arguments of the function and their descriptions, and examples of this function in use. The doc string is displayed when the help method is run by a user: `help(create_foo)` or `create_foo?` in python.
117+
118+
The parameters are listed in the doc string with the format
119+
```
120+
:param (param_type) param_name: description.
121+
```
122+
Afterwards, you must include examples which demonstrate the different capabilities and features of the function. For more information on proper doc string syntax see [PEP-257 page](https://www.python.org/dev/peps/pep-0257/).
123+
124+
After the doc string, you will add the main code of your function which should result in returning the fig, i.e. `return fig`.
125+
126+
```
127+
# create figure
128+
fig = create_foo(...)
129+
130+
# plot figure
131+
py.iplot(fig, filename='my_figure')
132+
```
133+
134+
The figure `fig` must be a Plotly Figure, meaning it must have the form `fig = graph_objs.Figure(data=data, layout=layout)`.
135+
136+
5. Useful Tips
137+
138+
It is often not a good idea to put all your code into your `create_foo()` function. It is best practice to not repeat yourself and this requires taking repeated blocks of code and puting them into a seperate function.
139+
140+
It is best to make all other functions besides `create_foo()` secret so a user cannot access them. This is done by placing a `_` before the name of the function, so `_aux_func()` for example.
141+
142+
143+
## Create a Pull Request
144+
145+
Now add changes to your current local branch
146+
147+
```
148+
$ git add -a
149+
```
150+
151+
and commit these changes and write a commit message.
152+
153+
```
154+
$ git commit -m "this is the work that I did"
155+
```
156+
157+
After you have added and commited all of your changes to the local branch, it is time to create your PR for the Plotly team to review.
158+
159+
```
160+
$ git push origin add-ff-type
161+
```
162+
163+
## Be Part of the Discussion
164+
165+
Go check out your newly pushed branch at https://github.com/plotly/plotly.py. If you have any other questions, check out the [Plotly Contributing Page](https://github.com/plotly/plotly.py/blob/master/contributing.md). Thanks for contributing to Plotly's Graphing Library!

0 commit comments

Comments
 (0)