Skip to content

Commit a3a267a

Browse files
committed
Merge branch 'master' into trisurf_colorbar
2 parents a328f86 + 0af4ef6 commit a3a267a

File tree

8 files changed

+515
-129
lines changed

8 files changed

+515
-129
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
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+
## [1.12.4] - 2016-07-14
6+
### Added
7+
- The FigureFactory can now create 2D-density charts with `.create_2D_density`. Check it out with:
8+
```
9+
import plotly.tools as tls
10+
help(tls.FigureFactory.create_2D_density)
11+
```
12+
13+
## [1.12.3] - 2016-06-30
14+
### Updated
15+
- Updated `plotly.min.js` from 1.13.0 to 1.14.1
16+
- Numerous additions and changes where made to the mapbox layout layers attributes
17+
- Attribute line.color in scatter3d traces now support color scales
18+
- Layout shapes can now be moved and resized (except for 'path' shapes) in editable contexts
19+
- See [the plotly.js CHANGELOG](https://github.com/plotly/plotly.js/blob/master/CHANGELOG.md#1141----2016-06-28) for additional information regarding the updates
20+
- Updated `default-schema`
21+
22+
### Added
23+
- Added `update_plotlyjs_for_offline` in makefile in order to automate updating `plotly.min.js` for offline mode
24+
525
## [1.12.2] - 2016-06-20
626
### Updated
727
- Updated plotly.min.js so the offline mode is using plotly.js v1.13.0

makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,16 @@ pull_chunked : submodules/chunked_requests
5757
@echo ""
5858
@echo "Pulling down updates from chunked_requests"
5959
cd submodules/chunked_requests; git pull origin master
60+
61+
update_plotlyjs_for_offline :
62+
@echo "Updating plotly.js for Offline Mode"
63+
@echo "------------------"
64+
python -c "import urllib2;\
65+
cdn_url = 'https://cdn.plot.ly/plotly-latest.min.js';\
66+
response = urllib2.urlopen(cdn_url);\
67+
html = response.read();\
68+
f = open('./plotly/offline/plotly.min.js', 'w');\
69+
f.write(html);\
70+
f.close()"
71+
@echo "---------------------------------"
72+
@echo "Remember to update the CHANGELOG!"

optional-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ pandas
2222

2323
## scipy deps for some FigureFactory functions ##
2424
scipy
25+

plotly/graph_reference/default-schema.json

Lines changed: 196 additions & 110 deletions
Large diffs are not rendered by default.

plotly/offline/plotly.min.js

Lines changed: 19 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plotly/tests/test_core/test_tools/test_figure_factory.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,111 @@ def test_gantt_all_args(self):
13651365
self.assertEqual(test_gantt_chart['layout'],
13661366
exp_gantt_chart['layout'])
13671367

1368+
1369+
class Test2D_Density(TestCase):
1370+
1371+
def test_validate_2D_density(self):
1372+
1373+
# validate that x and y contain only numbers
1374+
x = [1, 2]
1375+
y = ['a', 2]
1376+
1377+
pattern = ("All elements of your 'x' and 'y' lists must be numbers.")
1378+
1379+
self.assertRaisesRegexp(PlotlyError, pattern,
1380+
tls.FigureFactory.create_2D_density, x, y)
1381+
1382+
# validate that x and y are the same length
1383+
x2 = [1]
1384+
y2 = [1, 2]
1385+
1386+
pattern2 = ("Both lists 'x' and 'y' must be the same length.")
1387+
1388+
self.assertRaisesRegexp(PlotlyError, pattern2,
1389+
tls.FigureFactory.create_2D_density, x2, y2)
1390+
1391+
def test_2D_density_all_args(self):
1392+
1393+
# check if 2D_density data matches with expected output
1394+
x = [1, 2]
1395+
y = [2, 4]
1396+
1397+
colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)',
1398+
(1, 1, 0.2), (0.98, 0.98, 0.98)]
1399+
1400+
test_2D_density_chart = tls.FigureFactory.create_2D_density(
1401+
x, y, colorscale=colorscale, hist_color='rgb(255, 237, 222)',
1402+
point_size=3, height=800, width=800)
1403+
1404+
exp_2D_density_chart = {
1405+
'data': [{'marker': {'color': 'rgb(0.0, 0.0, 127.5)',
1406+
'opacity': 0.4,
1407+
'size': 3},
1408+
'mode': 'markers',
1409+
'name': 'points',
1410+
'type': 'scatter',
1411+
'x': [1, 2],
1412+
'y': [2, 4]},
1413+
{'colorscale': [[0.0, 'rgb(122.0, 69.0, 121.0)'],
1414+
[0.25, 'rgb(213.0, 96.0, 115.0)'],
1415+
[0.5, 'rgb(236.0, 158.0, 105.0)'],
1416+
[0.75, 'rgb(255.0, 255.0, 51.0)'],
1417+
[1.0, 'rgb(249.9, 249.9, 249.9)']],
1418+
'name': 'density',
1419+
'ncontours': 20,
1420+
'reversescale': True,
1421+
'showscale': False,
1422+
'type': 'histogram2dcontour',
1423+
'x': [1, 2],
1424+
'y': [2, 4]},
1425+
{'marker': {'color': 'rgb(255.0, 237.0, 222.0)'},
1426+
'name': 'x density',
1427+
'type': 'histogram',
1428+
'x': [1, 2],
1429+
'yaxis': 'y2'},
1430+
{'marker': {'color': 'rgb(255.0, 237.0, 222.0)'},
1431+
'name': 'y density',
1432+
'type': 'histogram',
1433+
'xaxis': 'x2',
1434+
'y': [2, 4]}],
1435+
'layout': {'autosize': False,
1436+
'bargap': 0,
1437+
'height': 800,
1438+
'hovermode': 'closest',
1439+
'margin': {'t': 50},
1440+
'showlegend': False,
1441+
'title': '2D Density Plot',
1442+
'width': 800,
1443+
'xaxis': {'domain': [0, 0.85],
1444+
'showgrid': False,
1445+
'zeroline': False},
1446+
'xaxis2': {'domain': [0.85, 1],
1447+
'showgrid': False,
1448+
'zeroline': False},
1449+
'yaxis': {'domain': [0, 0.85],
1450+
'showgrid': False,
1451+
'zeroline': False},
1452+
'yaxis2': {'domain': [0.85, 1],
1453+
'showgrid': False,
1454+
'zeroline': False}}
1455+
}
1456+
1457+
self.assertEqual(test_2D_density_chart['data'][0],
1458+
exp_2D_density_chart['data'][0])
1459+
1460+
self.assertEqual(test_2D_density_chart['data'][1],
1461+
exp_2D_density_chart['data'][1])
1462+
1463+
self.assertEqual(test_2D_density_chart['data'][2],
1464+
exp_2D_density_chart['data'][2])
1465+
1466+
self.assertEqual(test_2D_density_chart['data'][3],
1467+
exp_2D_density_chart['data'][3])
1468+
1469+
self.assertEqual(test_2D_density_chart['layout'],
1470+
exp_2D_density_chart['layout'])
1471+
1472+
13681473
# class TestDistplot(TestCase):
13691474

13701475
# def test_scipy_import_error(self):

plotly/tools.py

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,166 @@ def _convert_colorscale_to_rgb(colorscale):
15151515
)
15161516
return colorscale
15171517

1518+
def _make_linear_colorscale(colors):
1519+
"""
1520+
Makes a list of colors into a colorscale-acceptable form
1521+
1522+
For documentation regarding to the form of the output, see
1523+
https://plot.ly/python/reference/#mesh3d-colorscale
1524+
"""
1525+
scale = 1./(len(colors) - 1)
1526+
return[[i * scale, color] for i, color in enumerate(colors)]
1527+
1528+
@staticmethod
1529+
def create_2D_density(x, y, colorscale='Earth', ncontours=20,
1530+
hist_color=(0, 0, 0.5), point_color=(0, 0, 0.5),
1531+
point_size=2, title='2D Density Plot',
1532+
height=600, width=600):
1533+
"""
1534+
Returns figure for a 2D density plot
1535+
1536+
:param (list|array) x: x-axis data for plot generation
1537+
:param (list|array) y: y-axis data for plot generation
1538+
:param (str|tuple|list) colorscale: either a plotly scale name, an rgb
1539+
or hex color, a color tuple or a list or tuple of colors. An rgb
1540+
color is of the form 'rgb(x, y, z)' where x, y, z belong to the
1541+
interval [0, 255] and a color tuple is a tuple of the form
1542+
(a, b, c) where a, b and c belong to [0, 1]. If colormap is a
1543+
list, it must contain the valid color types aforementioned as its
1544+
members.
1545+
:param (int) ncontours: the number of 2D contours to draw on the plot
1546+
:param (str) hist_color: the color of the plotted histograms
1547+
:param (str) point_color: the color of the scatter points
1548+
:param (str) point_size: the color of the scatter points
1549+
:param (str) title: set the title for the plot
1550+
:param (float) height: the height of the chart
1551+
:param (float) width: the width of the chart
1552+
1553+
Example 1: Simple 2D Density Plot
1554+
```
1555+
import plotly.plotly as py
1556+
from plotly.tools import FigureFactory as FF
1557+
1558+
import numpy as np
1559+
1560+
# Make data points
1561+
t = np.linspace(-1,1.2,2000)
1562+
x = (t**3)+(0.3*np.random.randn(2000))
1563+
y = (t**6)+(0.3*np.random.randn(2000))
1564+
1565+
# Create a figure
1566+
fig = FF.create_2D_density(x, y)
1567+
1568+
# Plot the data
1569+
py.iplot(fig, filename='simple-2d-density')
1570+
```
1571+
1572+
Example 2: Using Parameters
1573+
```
1574+
import plotly.plotly as py
1575+
from plotly.tools import FigureFactory as FF
1576+
1577+
import numpy as np
1578+
1579+
# Make data points
1580+
t = np.linspace(-1,1.2,2000)
1581+
x = (t**3)+(0.3*np.random.randn(2000))
1582+
y = (t**6)+(0.3*np.random.randn(2000))
1583+
1584+
# Create custom colorscale
1585+
colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)',
1586+
(1, 1, 0.2), (0.98,0.98,0.98)]
1587+
1588+
# Create a figure
1589+
fig = FF.create_2D_density(
1590+
x, y, colorscale=colorscale,
1591+
hist_color='rgb(255, 237, 222)', point_size=3)
1592+
1593+
# Plot the data
1594+
py.iplot(fig, filename='use-parameters')
1595+
```
1596+
"""
1597+
from plotly.graph_objs import graph_objs
1598+
from numbers import Number
1599+
1600+
# validate x and y are filled with numbers only
1601+
for array in [x, y]:
1602+
if not all(isinstance(element, Number) for element in array):
1603+
raise exceptions.PlotlyError(
1604+
"All elements of your 'x' and 'y' lists must be numbers."
1605+
)
1606+
1607+
# validate x and y are the same length
1608+
if len(x) != len(y):
1609+
raise exceptions.PlotlyError(
1610+
"Both lists 'x' and 'y' must be the same length."
1611+
)
1612+
1613+
colorscale = FigureFactory._validate_colors(colorscale, 'rgb')
1614+
colorscale = FigureFactory._make_linear_colorscale(colorscale)
1615+
1616+
# validate hist_color and point_color
1617+
hist_color = FigureFactory._validate_colors(hist_color, 'rgb')
1618+
point_color = FigureFactory._validate_colors(point_color, 'rgb')
1619+
1620+
trace1 = graph_objs.Scatter(
1621+
x=x, y=y, mode='markers', name='points',
1622+
marker=dict(
1623+
color=point_color[0],
1624+
size=point_size,
1625+
opacity=0.4
1626+
)
1627+
)
1628+
trace2 = graph_objs.Histogram2dcontour(
1629+
x=x, y=y, name='density', ncontours=ncontours,
1630+
colorscale=colorscale, reversescale=True, showscale=False
1631+
)
1632+
trace3 = graph_objs.Histogram(
1633+
x=x, name='x density',
1634+
marker=dict(color=hist_color[0]), yaxis='y2'
1635+
)
1636+
trace4 = graph_objs.Histogram(
1637+
y=y, name='y density',
1638+
marker=dict(color=hist_color[0]), xaxis='x2'
1639+
)
1640+
data = [trace1, trace2, trace3, trace4]
1641+
1642+
layout = graph_objs.Layout(
1643+
showlegend=False,
1644+
autosize=False,
1645+
title=title,
1646+
height=height,
1647+
width=width,
1648+
xaxis=dict(
1649+
domain=[0, 0.85],
1650+
showgrid=False,
1651+
zeroline=False
1652+
),
1653+
yaxis=dict(
1654+
domain=[0, 0.85],
1655+
showgrid=False,
1656+
zeroline=False
1657+
),
1658+
margin=dict(
1659+
t=50
1660+
),
1661+
hovermode='closest',
1662+
bargap=0,
1663+
xaxis2=dict(
1664+
domain=[0.85, 1],
1665+
showgrid=False,
1666+
zeroline=False
1667+
),
1668+
yaxis2=dict(
1669+
domain=[0.85, 1],
1670+
showgrid=False,
1671+
zeroline=False
1672+
)
1673+
)
1674+
1675+
fig = graph_objs.Figure(data=data, layout=layout)
1676+
return fig
1677+
15181678
@staticmethod
15191679
def _validate_gantt(df):
15201680
"""

plotly/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.12.2'
1+
__version__ = '1.12.4'

0 commit comments

Comments
 (0)