Skip to content

Commit f051573

Browse files
committed
Added _make_colorscale method and 2D Density Plot
1 parent a1c0a9c commit f051573

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

plotly/tools.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,149 @@ class FigureFactory(object):
14761476
more information and examples of a specific chart type.
14771477
"""
14781478

1479+
@staticmethod
1480+
def _make_colorscale(colors):
1481+
"""
1482+
Makes a list of colors into a colorscale-acceptable form
1483+
1484+
For documentation regarding to the form of the output, see
1485+
https://plot.ly/python/reference/#mesh3d-colorscale
1486+
"""
1487+
colorscale = []
1488+
diff = 1./(len(colors) - 1)
1489+
1490+
for j in range(len(colors)):
1491+
colorscale.append([j * diff, colors[j]])
1492+
return colorscale
1493+
1494+
@staticmethod
1495+
def create_2D_density(x, y, colorscale='Earth', ncontours=20,
1496+
hist_color=(0, 0, 0.5), pt_color=(0, 0, 0.5),
1497+
pt_size=2, height=600, width=600):
1498+
"""
1499+
Returns figure for a 2D density plot
1500+
1501+
:param (list) x: x-axis data for plot generation
1502+
:param (list) y: y-axis data for plot generation
1503+
:param (str|tuple|list) colormap: either a plotly scale name, an rgb
1504+
or hex color, a color tuple or a list or tuple of colors. An rgb
1505+
color is of the form 'rgb(x, y, z)' where x, y, z belong to the
1506+
interval [0, 255] and a color tuple is a tuple of the form
1507+
(a, b, c) where a, b and c belong to [0, 1]. If colormap is a
1508+
list, it must contain the valid color types aforementioned as its
1509+
members.
1510+
:param (int) ncontours: the number of 2D contours to draw on the plot
1511+
:param (str) hist_color: the color of the plotted histograms
1512+
:param (str) pt_color: the color of the scatter points
1513+
:param (str) pt_size: the color of the scatter points
1514+
:param (float) height: the height of the chart
1515+
:param (float) width: the width of the chart
1516+
1517+
Example 1: Simple 2D Density Plot
1518+
```
1519+
import plotly.plotly as py
1520+
from plotly.tools import FigureFactory as FF
1521+
1522+
# Make data points
1523+
t = np.linspace(-1,1.2,2000)
1524+
x = (t**3)+(0.3*np.random.randn(2000))
1525+
y = (t**6)+(0.3*np.random.randn(2000))
1526+
1527+
# Create a figure
1528+
fig = FF.create_2D_density(x, y)
1529+
1530+
# Plot the data
1531+
py.iplot(fig, filename='simple-2d-density')
1532+
```
1533+
1534+
Example 2: Use Parameters
1535+
```
1536+
import plotly.plotly as py
1537+
from plotly.tools import FigureFactory as FF
1538+
1539+
# Make data points
1540+
t = np.linspace(-1,1.2,2000)
1541+
x = (t**3)+(0.3*np.random.randn(2000))
1542+
y = (t**6)+(0.3*np.random.randn(2000))
1543+
1544+
# Create custom colorscale
1545+
colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)',
1546+
(1, 1, 0.2), (0.98,0.98,0.98)]
1547+
1548+
# Create a figure
1549+
fig = FF.create_2D_density(
1550+
x, y, colorscale=colorscale, hist_color='rgb(255, 237, 222)', pt_size=3)
1551+
1552+
# Plot the data
1553+
py.iplot(fig, filename='use-parameters')
1554+
```
1555+
"""
1556+
from plotly.graph_objs import graph_objs
1557+
colorscale = FigureFactory._validate_colors(colorscale, 'rgb')
1558+
colorscale = FigureFactory._make_colorscale(colorscale)
1559+
1560+
# validate hist_color and pt_color
1561+
hist_color = FigureFactory._validate_colors(hist_color, 'rgb')
1562+
pt_color = FigureFactory._validate_colors(pt_color, 'rgb')
1563+
1564+
trace1 = graph_objs.Scatter(
1565+
x=x, y=y, mode='markers', name='points',
1566+
marker=dict(
1567+
color=pt_color[0],
1568+
size=pt_size,
1569+
opacity=0.4
1570+
)
1571+
)
1572+
trace2 = graph_objs.Histogram2dcontour(
1573+
x=x, y=y, name='density', ncontours=ncontours,
1574+
colorscale=colorscale, reversescale=True, showscale=False
1575+
)
1576+
trace3 = graph_objs.Histogram(
1577+
x=x, name='x density',
1578+
marker=dict(color=hist_color[0]), yaxis='y2'
1579+
)
1580+
trace4 = graph_objs.Histogram(
1581+
y=y, name='y density',
1582+
marker=dict(color=hist_color[0]), xaxis='x2'
1583+
)
1584+
data = [trace1, trace2, trace3, trace4]
1585+
1586+
layout = graph_objs.Layout(
1587+
showlegend=False,
1588+
autosize=False,
1589+
title='Love',
1590+
height=height,
1591+
width=width,
1592+
xaxis=dict(
1593+
___domain=[0, 0.85],
1594+
showgrid=False,
1595+
zeroline=False
1596+
),
1597+
yaxis=dict(
1598+
___domain=[0, 0.85],
1599+
showgrid=False,
1600+
zeroline=False
1601+
),
1602+
margin=dict(
1603+
t=50
1604+
),
1605+
hovermode='closest',
1606+
bargap=0,
1607+
xaxis2=dict(
1608+
___domain=[0.85, 1],
1609+
showgrid=False,
1610+
zeroline=False
1611+
),
1612+
yaxis2=dict(
1613+
___domain=[0.85, 1],
1614+
showgrid=False,
1615+
zeroline=False
1616+
)
1617+
)
1618+
1619+
fig = graph_objs.Figure(data=data, layout=layout)
1620+
return fig
1621+
14791622
@staticmethod
14801623
def _validate_gantt(df):
14811624
"""

0 commit comments

Comments
 (0)