Skip to content

Commit 7da657e

Browse files
committed
Move FF from tools.py to /figure_factory pkg.
The old location is now deprecated.
1 parent 8fdd412 commit 7da657e

17 files changed

+6217
-6109
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [Unreleased]
6+
7+
### Deprecated
8+
- `plotly.tools.FigureFactory`. Use `plotly.figure_factory.*`.
9+
510
## [1.13.0]
611
### Added
712
- Python 3.5 has been added as a tested environment for this package.

plotly/figure_factory/_2d_density.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
from __future__ import absolute_import
2+
3+
from plotly import exceptions
4+
from plotly.figure_factory import utils
5+
6+
7+
def make_linear_colorscale(colors):
8+
"""
9+
Makes a list of colors into a colorscale-acceptable form
10+
11+
For documentation regarding to the form of the output, see
12+
https://plot.ly/python/reference/#mesh3d-colorscale
13+
"""
14+
scale = 1. / (len(colors) - 1)
15+
return [[i * scale, color] for i, color in enumerate(colors)]
16+
17+
18+
def create_2d_density(x, y, colorscale='Earth', ncontours=20,
19+
hist_color=(0, 0, 0.5), point_color=(0, 0, 0.5),
20+
point_size=2, title='2D Density Plot',
21+
height=600, width=600):
22+
"""
23+
Returns figure for a 2D density plot
24+
25+
:param (list|array) x: x-axis data for plot generation
26+
:param (list|array) y: y-axis data for plot generation
27+
:param (str|tuple|list) colorscale: either a plotly scale name, an rgb
28+
or hex color, a color tuple or a list or tuple of colors. An rgb
29+
color is of the form 'rgb(x, y, z)' where x, y, z belong to the
30+
interval [0, 255] and a color tuple is a tuple of the form
31+
(a, b, c) where a, b and c belong to [0, 1]. If colormap is a
32+
list, it must contain the valid color types aforementioned as its
33+
members.
34+
:param (int) ncontours: the number of 2D contours to draw on the plot
35+
:param (str) hist_color: the color of the plotted histograms
36+
:param (str) point_color: the color of the scatter points
37+
:param (str) point_size: the color of the scatter points
38+
:param (str) title: set the title for the plot
39+
:param (float) height: the height of the chart
40+
:param (float) width: the width of the chart
41+
42+
Example 1: Simple 2D Density Plot
43+
```
44+
import plotly.plotly as py
45+
from plotly.tools import FigureFactory as FF
46+
47+
import numpy as np
48+
49+
# Make data points
50+
t = np.linspace(-1,1.2,2000)
51+
x = (t**3)+(0.3*np.random.randn(2000))
52+
y = (t**6)+(0.3*np.random.randn(2000))
53+
54+
# Create a figure
55+
fig = FF.create_2D_density(x, y)
56+
57+
# Plot the data
58+
py.iplot(fig, filename='simple-2d-density')
59+
```
60+
61+
Example 2: Using Parameters
62+
```
63+
import plotly.plotly as py
64+
from plotly.tools import FigureFactory as FF
65+
66+
import numpy as np
67+
68+
# Make data points
69+
t = np.linspace(-1,1.2,2000)
70+
x = (t**3)+(0.3*np.random.randn(2000))
71+
y = (t**6)+(0.3*np.random.randn(2000))
72+
73+
# Create custom colorscale
74+
colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)',
75+
(1, 1, 0.2), (0.98,0.98,0.98)]
76+
77+
# Create a figure
78+
fig = FF.create_2D_density(
79+
x, y, colorscale=colorscale,
80+
hist_color='rgb(255, 237, 222)', point_size=3)
81+
82+
# Plot the data
83+
py.iplot(fig, filename='use-parameters')
84+
```
85+
"""
86+
from plotly.graph_objs import graph_objs
87+
from numbers import Number
88+
89+
# validate x and y are filled with numbers only
90+
for array in [x, y]:
91+
if not all(isinstance(element, Number) for element in array):
92+
raise exceptions.PlotlyError(
93+
"All elements of your 'x' and 'y' lists must be numbers."
94+
)
95+
96+
# validate x and y are the same length
97+
if len(x) != len(y):
98+
raise exceptions.PlotlyError(
99+
"Both lists 'x' and 'y' must be the same length."
100+
)
101+
102+
colorscale = utils.validate_colors(colorscale, 'rgb')
103+
colorscale = make_linear_colorscale(colorscale)
104+
105+
# validate hist_color and point_color
106+
hist_color = utils.validate_colors(hist_color, 'rgb')
107+
point_color = utils.validate_colors(point_color, 'rgb')
108+
109+
trace1 = graph_objs.Scatter(
110+
x=x, y=y, mode='markers', name='points',
111+
marker=dict(
112+
color=point_color[0],
113+
size=point_size,
114+
opacity=0.4
115+
)
116+
)
117+
trace2 = graph_objs.Histogram2dcontour(
118+
x=x, y=y, name='density', ncontours=ncontours,
119+
colorscale=colorscale, reversescale=True, showscale=False
120+
)
121+
trace3 = graph_objs.Histogram(
122+
x=x, name='x density',
123+
marker=dict(color=hist_color[0]), yaxis='y2'
124+
)
125+
trace4 = graph_objs.Histogram(
126+
y=y, name='y density',
127+
marker=dict(color=hist_color[0]), xaxis='x2'
128+
)
129+
data = [trace1, trace2, trace3, trace4]
130+
131+
layout = graph_objs.Layout(
132+
showlegend=False,
133+
autosize=False,
134+
title=title,
135+
height=height,
136+
width=width,
137+
xaxis=dict(
138+
domain=[0, 0.85],
139+
showgrid=False,
140+
zeroline=False
141+
),
142+
yaxis=dict(
143+
domain=[0, 0.85],
144+
showgrid=False,
145+
zeroline=False
146+
),
147+
margin=dict(
148+
t=50
149+
),
150+
hovermode='closest',
151+
bargap=0,
152+
xaxis2=dict(
153+
domain=[0.85, 1],
154+
showgrid=False,
155+
zeroline=False
156+
),
157+
yaxis2=dict(
158+
domain=[0.85, 1],
159+
showgrid=False,
160+
zeroline=False
161+
)
162+
)
163+
164+
fig = graph_objs.Figure(data=data, layout=layout)
165+
return fig

plotly/figure_factory/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from __future__ import absolute_import
2+
3+
from plotly.figure_factory._annotated_heatmap import create_annotated_heatmap
4+
from plotly.figure_factory._candlestick import create_candlestick
5+
from plotly.figure_factory._dendrogram import create_dendrogram
6+
from plotly.figure_factory._distplot import create_distplot
7+
from plotly.figure_factory._gantt import create_gantt
8+
from plotly.figure_factory._ohlc import create_ohlc
9+
from plotly.figure_factory._quiver import create_quiver
10+
from plotly.figure_factory._scatterplot import create_scatterplotmatrix
11+
from plotly.figure_factory._streamline import create_streamline
12+
from plotly.figure_factory._table import create_table
13+
from plotly.figure_factory._trisurf import create_trisurf
14+
from plotly.figure_factory._violin import create_violin

0 commit comments

Comments
 (0)