Skip to content

Commit 86f61bc

Browse files
committed
Added dictionary compatibility to colors/fixed doc strings
1 parent 66382e2 commit 86f61bc

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed

plotly/tests/test_core/test_tools/test_figure_factory.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,17 @@ def test_gantt_validate_colors(self):
12451245
tls.FigureFactory.create_gantt, df,
12461246
index_col='Complete', colors=colors_dict)
12471247

1248+
# check: index is set if colors is a dictionary
1249+
colors_dict_good = {50: 'rgb(1, 2, 3)', 75: 'rgb(5, 10, 15)'}
1250+
1251+
pattern5 = ("Error. You have set colors to a dictionary but have not "
1252+
"picked an index. An index is required if you are "
1253+
"assigning colors to particular values in a dictioanry.")
1254+
1255+
self.assertRaisesRegexp(PlotlyError, pattern5,
1256+
tls.FigureFactory.create_gantt, df,
1257+
colors=colors_dict_good)
1258+
12481259
def test_gantt_all_args(self):
12491260

12501261
# check if gantt chart matches with expected output

plotly/tools.py

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,10 +1897,19 @@ def create_gantt(df, colors=None, index_col=None, show_colorbar=False,
18971897
used for indexing. If a list, its elements must be dictionaries
18981898
with the same required column headers: 'Task', 'Start' and
18991899
'Finish'.
1900-
:param (str|list|dict) colors: a list of 'rgb(a, b, c)' colors where a, b and c
1901-
are between 0 and 255. Can also be a Plotly colorscale but this is
1902-
will result in only a 2-color cycle. If number of colors is less
1903-
than the total number of tasks, colors will cycle
1900+
:param (str|list|dict) colors: either a plotly scale name, an rgb
1901+
or hex color, a color tuple or a list of colors. An rgb color is
1902+
of the form 'rgb(x, y, z)' where x, y, z belong to the interval
1903+
[0, 255] and a color tuple is a tuple of the form (a, b, c) where
1904+
a, b and c belong to [0, 1]. If colors is a list, it must
1905+
contain the valid color types aforementioned as its members.
1906+
If a dictionary, all values of the indexing column must be keys in
1907+
colors.
1908+
:param (str|float) index_col: the column header (if df is a data
1909+
frame) that will function as the indexing column. If df is a list,
1910+
index_col must be one of the keys in all the items of df.
1911+
:param (bool) show_colorbar: determines if colorbar will be visible.
1912+
Only applies if values in the index column are numeric.
19041913
:param (bool) reverse_colors: reverses the order of selected colors
19051914
:param (str) title: the title of the chart
19061915
:param (float) bar_width: the width of the horizontal bars in the plot
@@ -1923,10 +1932,10 @@ def create_gantt(df, colors=None, index_col=None, show_colorbar=False,
19231932
fig = FF.create_gantt(df)
19241933
19251934
# Plot the data
1926-
py.iplot(fig, filename='Gantt Chart', world_readable=True)
1935+
py.iplot(fig, filename='Simple Gantt Chart', world_readable=True)
19271936
```
19281937
1929-
Example 2: Colormap by 'Complete' Variable
1938+
Example 2: Index by Column with Numerical Entries
19301939
```
19311940
import plotly.plotly as py
19321941
from plotly.tools import FigureFactory as FF
@@ -1940,11 +1949,61 @@ def create_gantt(df, colors=None, index_col=None, show_colorbar=False,
19401949
Finish='2009-05-30', Complete=95)]
19411950
19421951
# Create a figure with Plotly colorscale
1943-
fig = FF.create_gantt(df, colors='Blues',
1944-
bar_width=0.5, showgrid_x=True, showgrid_y=True)
1952+
fig = FF.create_gantt(df, colors='Blues', index_col='Complete',
1953+
show_colorbar=True, bar_width=0.5,
1954+
showgrid_x=True, showgrid_y=True)
19451955
19461956
# Plot the data
1947-
py.iplot(fig, filename='Colormap Gantt Chart', world_readable=True)
1957+
py.iplot(fig, filename='Numerical Entries', world_readable=True)
1958+
```
1959+
1960+
Example 3: Index by Column with String Entries
1961+
```
1962+
import plotly.plotly as py
1963+
from plotly.tools import FigureFactory as FF
1964+
1965+
# Make data for chart
1966+
df = [dict(Task="Job A", Start='2009-01-01',
1967+
Finish='2009-02-30', Resource='Apple'),
1968+
dict(Task="Job B", Start='2009-03-05',
1969+
Finish='2009-04-15', Resource='Grape'),
1970+
dict(Task="Job C", Start='2009-02-20',
1971+
Finish='2009-05-30', Resource='Banana')]
1972+
1973+
# Create a figure with Plotly colorscale
1974+
fig = FF.create_gantt(df, colors=['rgb(200, 50, 25)',
1975+
(1, 0, 1),
1976+
'#6c4774'],
1977+
index_col='Resource',
1978+
reverse_colors=True)
1979+
1980+
# Plot the data
1981+
py.iplot(fig, filename='String Entries', world_readable=True)
1982+
```
1983+
1984+
Example 4: Use a dictionary for colors
1985+
```
1986+
import plotly.plotly as py
1987+
from plotly.tools import FigureFactory as FF
1988+
1989+
# Make data for chart
1990+
df = [dict(Task="Job A", Start='2009-01-01',
1991+
Finish='2009-02-30', Resource='Apple'),
1992+
dict(Task="Job B", Start='2009-03-05',
1993+
Finish='2009-04-15', Resource='Grape'),
1994+
dict(Task="Job C", Start='2009-02-20',
1995+
Finish='2009-05-30', Resource='Banana')]
1996+
1997+
# Make a dictionary of colors
1998+
colors = {'Apple': 'rgb(255, 0, 0)',
1999+
'Grape': 'rgb(170, 14, 200)',
2000+
'Banana': (1, 1, 0.2)}
2001+
2002+
# Create a figure with Plotly colorscale
2003+
fig = FF.create_gantt(df, colors=colors, index_col='Resource')
2004+
2005+
# Plot the data
2006+
py.iplot(fig, filename='dictioanry colors', world_readable=True)
19482007
```
19492008
"""
19502009
plotly_scales = {'Greys': ['rgb(0,0,0)', 'rgb(255,255,255)'],
@@ -2115,10 +2174,17 @@ def create_gantt(df, colors=None, index_col=None, show_colorbar=False,
21152174
colors.reverse()
21162175

21172176
if not index_col:
2118-
fig = FigureFactory._gantt(chart, colors, title,
2119-
bar_width, showgrid_x, showgrid_y,
2120-
height, width, tasks=None,
2121-
task_names=None, data=None)
2177+
if isinstance(colors, dict):
2178+
raise exceptions.PlotlyError("Error. You have set colors to "
2179+
"a dictionary but have not "
2180+
"picked an index. An index is "
2181+
"required if you are assigning "
2182+
"colors to particular values "
2183+
"in a dictioanry.")
2184+
2185+
fig = FigureFactory._gantt(chart, colors, title, bar_width,
2186+
showgrid_x, showgrid_y, height, width,
2187+
tasks=None, task_names=None, data=None)
21222188
return fig
21232189

21242190
else:

0 commit comments

Comments
 (0)