@@ -1476,6 +1476,161 @@ class FigureFactory(object):
1476
1476
more information and examples of a specific chart type.
1477
1477
"""
1478
1478
1479
+ @staticmethod
1480
+ def _make_linear_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
+ scale = 1. / (len (colors ) - 1 )
1488
+ return [[i * scale , color ] for i , color in enumerate (colors )]
1489
+
1490
+ @staticmethod
1491
+ def create_2D_density (x , y , colorscale = 'Earth' , ncontours = 20 ,
1492
+ hist_color = (0 , 0 , 0.5 ), point_color = (0 , 0 , 0.5 ),
1493
+ point_size = 2 , height = 600 , width = 600 ):
1494
+ """
1495
+ Returns figure for a 2D density plot
1496
+
1497
+ :param (list|array) x: x-axis data for plot generation
1498
+ :param (list|array) y: y-axis data for plot generation
1499
+ :param (str|tuple|list) colorscale: either a plotly scale name, an rgb
1500
+ or hex color, a color tuple or a list or tuple of colors. An rgb
1501
+ color is of the form 'rgb(x, y, z)' where x, y, z belong to the
1502
+ interval [0, 255] and a color tuple is a tuple of the form
1503
+ (a, b, c) where a, b and c belong to [0, 1]. If colormap is a
1504
+ list, it must contain the valid color types aforementioned as its
1505
+ members.
1506
+ :param (int) ncontours: the number of 2D contours to draw on the plot
1507
+ :param (str) hist_color: the color of the plotted histograms
1508
+ :param (str) point_color: the color of the scatter points
1509
+ :param (str) point_size: the color of the scatter points
1510
+ :param (float) height: the height of the chart
1511
+ :param (float) width: the width of the chart
1512
+
1513
+ Example 1: Simple 2D Density Plot
1514
+ ```
1515
+ import plotly.plotly as py
1516
+ from plotly.tools import FigureFactory as FF
1517
+
1518
+ # Make data points
1519
+ t = np.linspace(-1,1.2,2000)
1520
+ x = (t**3)+(0.3*np.random.randn(2000))
1521
+ y = (t**6)+(0.3*np.random.randn(2000))
1522
+
1523
+ # Create a figure
1524
+ fig = FF.create_2D_density(x, y)
1525
+
1526
+ # Plot the data
1527
+ py.iplot(fig, filename='simple-2d-density')
1528
+ ```
1529
+
1530
+ Example 2: Using Parameters
1531
+ ```
1532
+ import plotly.plotly as py
1533
+ from plotly.tools import FigureFactory as FF
1534
+
1535
+ # Make data points
1536
+ t = np.linspace(-1,1.2,2000)
1537
+ x = (t**3)+(0.3*np.random.randn(2000))
1538
+ y = (t**6)+(0.3*np.random.randn(2000))
1539
+
1540
+ # Create custom colorscale
1541
+ colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)',
1542
+ (1, 1, 0.2), (0.98,0.98,0.98)]
1543
+
1544
+ # Create a figure
1545
+ fig = FF.create_2D_density(
1546
+ x, y, colorscale=colorscale,
1547
+ hist_color='rgb(255, 237, 222)', point_size=3)
1548
+
1549
+ # Plot the data
1550
+ py.iplot(fig, filename='use-parameters')
1551
+ ```
1552
+ """
1553
+ from plotly .graph_objs import graph_objs
1554
+ from numbers import Number
1555
+
1556
+ # validate x and y are filled with numbers only
1557
+ for array in [x , y ]:
1558
+ if not all (isinstance (element , Number ) for element in array ):
1559
+ raise exceptions .PlotlyError (
1560
+ "All elements of your 'x' and 'y' lists must be numbers."
1561
+ )
1562
+
1563
+ # validate x and y are the same length
1564
+ if len (x ) != len (y ):
1565
+ raise exceptions .PlotlyError (
1566
+ "Both lists 'x' and 'y' must be the same length."
1567
+ )
1568
+
1569
+ colorscale = FigureFactory ._validate_colors (colorscale , 'rgb' )
1570
+ colorscale = FigureFactory ._make_linear_colorscale (colorscale )
1571
+
1572
+ # validate hist_color and point_color
1573
+ hist_color = FigureFactory ._validate_colors (hist_color , 'rgb' )
1574
+ point_color = FigureFactory ._validate_colors (point_color , 'rgb' )
1575
+
1576
+ trace1 = graph_objs .Scatter (
1577
+ x = x , y = y , mode = 'markers' , name = 'points' ,
1578
+ marker = dict (
1579
+ color = point_color [0 ],
1580
+ size = point_size ,
1581
+ opacity = 0.4
1582
+ )
1583
+ )
1584
+ trace2 = graph_objs .Histogram2dcontour (
1585
+ x = x , y = y , name = 'density' , ncontours = ncontours ,
1586
+ colorscale = colorscale , reversescale = True , showscale = False
1587
+ )
1588
+ trace3 = graph_objs .Histogram (
1589
+ x = x , name = 'x density' ,
1590
+ marker = dict (color = hist_color [0 ]), yaxis = 'y2'
1591
+ )
1592
+ trace4 = graph_objs .Histogram (
1593
+ y = y , name = 'y density' ,
1594
+ marker = dict (color = hist_color [0 ]), xaxis = 'x2'
1595
+ )
1596
+ data = [trace1 , trace2 , trace3 , trace4 ]
1597
+
1598
+ layout = graph_objs .Layout (
1599
+ showlegend = False ,
1600
+ autosize = False ,
1601
+ title = 'Love' ,
1602
+ height = height ,
1603
+ width = width ,
1604
+ xaxis = dict (
1605
+ domain = [0 , 0.85 ],
1606
+ showgrid = False ,
1607
+ zeroline = False
1608
+ ),
1609
+ yaxis = dict (
1610
+ domain = [0 , 0.85 ],
1611
+ showgrid = False ,
1612
+ zeroline = False
1613
+ ),
1614
+ margin = dict (
1615
+ t = 50
1616
+ ),
1617
+ hovermode = 'closest' ,
1618
+ bargap = 0 ,
1619
+ xaxis2 = dict (
1620
+ domain = [0.85 , 1 ],
1621
+ showgrid = False ,
1622
+ zeroline = False
1623
+ ),
1624
+ yaxis2 = dict (
1625
+ domain = [0.85 , 1 ],
1626
+ showgrid = False ,
1627
+ zeroline = False
1628
+ )
1629
+ )
1630
+
1631
+ fig = graph_objs .Figure (data = data , layout = layout )
1632
+ return fig
1633
+
1479
1634
@staticmethod
1480
1635
def _validate_gantt (df ):
1481
1636
"""
0 commit comments