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