Search code examples
pythonpandasplotplotlyplotly-python

Pandas: How to choose plot element colors when using plotly as a backend?


I'm experimenting with Plotly as a pandas plot backend. I successfuly configured it following these instructions, but I'm having trouble to configure my chart element colors.

Here is what I want to do. It works with matplotlib as a backend:

df.plot(kind='area', 
        color=['orange', 'lightslategray'],
        backend='matplotlib' )

correct chart

But when I try using ploty, it fails with a strange error:

df.plot(kind='area', 
        color=['orange', 'lightslategray'], 
        backend='plotly' )

The error:

ValueError: All arguments should have the same length. The length of argument `color` is 2, whereas the length of  previously-processed arguments ['Dia', 'Alfa', 'Beta'] is 12

The plot works if I don't try to set any color:

df.plot(kind='area', backend='plotly' )

plotly with wrong colors


Solution

  • It looks like some of the parameters don't have the same meaning depending on your plotting backend. color is one of them. When using plotly it tries to choose a dimension that will be used as color. The correct way is to use the color_discrete_map parameter:

    df.plot(kind='area', 
            color_discrete_map={
                'Alfa': 'orange', 
                'Beta': 'lightslategray'}, 
            backend='plotly' )
    

    Correctly plot

    Unfortunately it won't be possible to interchange the backend engine.