Manually define all 3 colors of a bullet chart background

Backgroud:
I create following bullet chart:

ff.create_bullet(
            orientation='h',
            ranges='range',
            range_colors=['#ff1a1a', '#7cfc00'],
            measures='data',
            measure_colors=['#ffffff', '#333333'],
            data=[dict(
                range=[.4, .5, 1],
                data=[0, 0.8],
            )],
        )

It looks like this:
Screenshot%20from%202018-11-02%2019-05-35

Problem:
I donā€™t like the middle colour (the brown), that is automatically defined as a mix of both colours defined in range_colors=['#ff1a1a', '#7cfc00'].
Thus, I would like to manually define it. I have tried range_colors=['#ff1a1a', '#0000ff', '#7cfc00'], but range_colors expects a list of length 2 only ā€” I get following error message: ā€œplotly.exceptions.PlotlyError: Both ā€˜range_colorsā€™ or ā€˜measure_colorsā€™ must be a list of two valid colors.ā€

Question:

How to manually define the middle colour of the bullet chart?

Hi @ebosi,

I took a look at the code, and itā€™s not possible to specify a custom list of colors in the figure factory right now, but I think this would make sense so feel free to open a feature request at https://github.com/plotly/plotly.py.

In the meantime, your best option is to modify the figure factory result before displaying the figure. Hereā€™s an example of coloring the middle bar blue in your example above

# Imports
import plotly.graph_objs as go
import plotly.figure_factory as ff

# Create initial bullet chart
fig = ff.create_bullet(
            orientation='h',
            ranges='range',
            range_colors=['#ff1a1a', '#7cfc00'],
            measures='data',
            measure_colors=['#ffffff', '#333333'],
            data=[dict(
                range=[.4, .5, 1],
                data=[0, 0.8],
            )],
        )

# Filter down to the 'ranges' bars
bar = [bar for bar in fig.data if bar.name == 'ranges']

# Set the color of the middle bar to blue
bar[1].marker.color = 'blue'

# Display figure
go.FigureWidget(fig)

Hope that helps,
-Jon

1 Like

Thank you, it indeed solved my issue.

Iā€™ve opened a feature request: https://github.com/plotly/plotly.py/issues/1261

Thanks for having created the MWE for me.

1 Like

PS: Iā€™m afraid I cannot ā€œacceptā€ your answer, as Iā€™ve bee able to do in other threads 'Error loading dependencies' trying to create a bullet chart ā€¦ Indeed, I donā€™t see the tickbox at the bottom of your post:

Do you know why?

Thanks for bringing that up @ebosi, the solution option wasnā€™t enabled for this category, but I just turned it on so you should be able to mark answers as solutions in the Python category now.
-Jon

1 Like