Scatter figure with multiple traces from a csv file

How do I put multiple traces onto a scatter figure is my data is coming from a csv file with ever-changing values?

I have a hierarchy for my data. It is state, city, month, and sales. I am designing a 2 by 2 dashboard, with 2 charts on the top row and two on the bottom row. I want to tell two different stories:

  1. On the top row, compare year-to-date sales by state in one chart and then to its right, compare year-to-date sales by cities within a selected state. With the help of contributors to this forum, I was able to get the state chart to work and interactively select cities for the city chart.

  2. On the bottom row are 2 charts showing year-to-date sales by month for all states and having a trace per state. (For now I am not working on the 4th chart which is month by city.) Currently, I have 4 states in my csv file, so I need to show 4 traces. I will be adding more as time goes on, and I would like the chart to dynamically add or remove traces based on what is in the csv file.

What I think I need to do is build a dataframe of just the unique states, then use this in a for loop to build a trace per state. The values would be x=month and y = sales for each trace. I am not sure how to do this though.

Can anyone suggest code or a reference url that shows how to build this scatter figure?

Thanks,

Let me know, you want your graph like on this picture.

Here is what I am looking for with the Sales by Month

Scatter%20with%20Multiple%20Traces

(Please ignore the months not being in correct order. I will fix that.)

I did this visualization manually. What I am hoping to do is this:

  1. as data for a new month is added to the csv file, the chart will automatically add the new month and data points

  2. as a new state is added, a new trace will be created on the chart.

I think I am close. I have create a state month dataframe that looks like this

month CA FL GA NY
0 Apr 1673.8 1764.9 533.4 1924.3
1 Feb 1979.9 1531.4 687.0 1968.1
2 Jan 1243.6 1674.8 583.2 1546.6
3 Jun 1623.6 1706.7 670.2 2034.6
4 Mar 1420.1 1935.4 447.9 1946.3
5 May 1495.3 1899.5 590.7 1770.1

Using this, I am able to create manually traces that look like this:

                'data': [
                    {
                        'x': ptstatemo_df.month,
                        'y': ptstatemo_df.iloc[:,1],
                        'name': Headers1[1],
                        'mode': 'lines',
                        'marker': {'size': 12}
                    },
                    {
                        'x': ptstatemo_df.month,
                        'y': ptstatemo_df.iloc[:,2],
                        'name': Headers1[2],
                        'mode': 'lines',
                        'marker': {'size': 12}
                    },

So I think I have the data frame set up correctly, and I understand generally how to use the data frame to build the traces.

I am able to run the follwoig count and get printouts of the correct columns.

for i in range(st_count):
print(ptstatemo_df.month)
print(ptstatemo_df.iloc[:,i+1])

I get the six months for the x values, and I get a different column of 6 sales values that match the sales of CA, FL, GA, and NY. So I am processing the dataframe and get my x and y values.

Is the following code what I need and where do I place it?

for i in range(st_count):
trace[i]=go.Scatter(x=ptstatemo_df[Headers[1]], y = ptstatemo_df[Headers[i+1]], mode = ‘lines’, name = Headers[i+1])

state_line = tools.make_subplots(rows = st_count, cols = 1, shared_xaxes = True)

for i in range(st_count):
state_line.append_trace(trace[i],i+1,1)

But I cannot get this to work.

three reasons why you should buy plotly pro: support open source, get great support, host your plots and dashboards online