Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Automatically create traces in Plot.ly from dataframe

I can’t seem to find a good pointer of whether what I want to achieve with Plot.ly is possible. I have the following Pandas dataframe:

enter image description here

From this dataframe I want to create a stacked area chart, where I show the total_value (y axis) and the date (x_axis).

This should be grouped by the account and name. There are several accounts that have the same names (assets in a portfolio). Assets (name column) can be added later automatically and I don’t want to change the code for that.

In my opinion I have to be able to automatically create traces, based on the account and name column.

How would this be possible?

Thanks a lot! :slight_smile:

Hi @rStorms,

For making a stacked area chart, check out this quick example of the new scatter.stackgroup parameter introduced in version 3.3.0.

For creating the initial scatter traces, I’d recommend using pandas to group your DataFrame by your account and name columns. This thread might be helpful: Similar to seaborn’s hue function in plotly.

If you’d like more help, please include some sample data and, if possible, a screenshot of the kind of plot you want to end up with.

Hope that helps!
-Jon

@jmmease, would cufflinks be of benefits?

Thanks, this helped a lot! I am solving this with the following code now in the DASH app.py:

#transform the dataframe
depot['position'] = depot['account'] + " - " + depot['name']
depot = depot.pivot_table(index='date', columns='position', values='total_value')

traces = []
for position, values in depot.iteritems():
    traces.append(go.Scatter(x=values.index, y=values.values, name=position, stackgroup='A'))

Probably so. I’m just less familiar with the trace types cufflinks can produce :slightly_smiling_face:
-Jon