Search code examples
pythonmatplotlibplotly

How to pass RGB tuples to `px.bar`?


You need so_sample.parquet to run the code

I need to replicate this bar plot in plotly, and most importantly be able to preselect the colors randomly in the same fashion.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.read_parquet('so_sample.parquet')
df = df.groupby(['col1', 'col2']).size().unstack().fillna(0)
df.plot(
    kind='bar',
    stacked=True,
    legend=False,
    xlabel='',
    rot=0,
    color=np.random.uniform(0, 1, (df.shape[1], 3)),
)

matplotlib

Here's what I did so far:

import plotly.express as px

df = pd.read_parquet('so_sample.parquet')
df = df.groupby(['col1', 'col2']).size().reset_index(name='count')
px.bar(
    df,
    x='col1',
    y='count',
    color='col2',
).show()

p2

I tried passing color_discrete_sequence=[color1, color2, ...] and it doesn't make any difference.


Solution

  • The data format used in matplotlib is wide format, but in plotly it is long format. plotly should be the same wide format and you can specify your favorite colormap. I chose Dark24, which has a large number of colors.

    import plotly.express as px
    import pandas as pd
    
    df = pd.read_parquet('./data/so_sample.parquet')
    #df = df.groupby(['col1', 'col2']).size().reset_index(name='count')
    df = df.groupby(['col1', 'col2']).size().unstack().fillna(0)
    
    px.bar(
        df,
        x=df.index,
        y=df.columns.tolist(),
        color_discrete_sequence=px.colors.qualitative.Dark24,
    ).show()
    

    enter image description here