Black Lives Matter. Please consider donating to Black Girls Code today.

Unable to generate a line trace together with a scatter trace in the same figure

Hi, I am having great difficulty in getting a regression line to be drawn on the same figure as a scatter trace. I am just really puzzled because there is nothing complicated here, and I am following quite closely with the line and scatter examples.

Here is the code that I am trying to use to generate the plot:

datapts = go.Scatter(x = data["Population"], y=data["Profit"], mode = "markers",
                hoveron = "points")
reg_line = go.Scatter(x = pop_range, y = profit_predicted, mode = "lines",
                     hoveron="points")

plotly.offline.iplot({
    "data": [datapts,reg_line],
    "layout": go.Layout(title="Relationship between Population and Profit", xaxis = dict(title="Population of City in 10,000s"), 
                     yaxis = dict(title="Profit in $10,000s"))
})

The figure generated just looks like:

As you can see, the line, trace 1, just does not show up at all. To make it possible to help me, these are what the arrays look like:

data["Population"][0:5]  # 97 elements long
0    6.1101
1    5.5277
2    8.5186
3    7.0032
4    5.8598
Name: Population, dtype: float64

data["Profit"][0:5]  # 97 elements long
0    17.5920
1     9.1302
2    13.6620
3    11.8540
4     6.8233
Name: Profit, dtype: float64

pop_range[0:5]  # 100 elements long
array([ 5.0269    ,  5.20039596,  5.37389192,  5.54738788,  5.72088384])

profit_predicted[0:5]  # 100 elements long
matrix([[ 2.23289546],
        [ 2.43525461],
        [ 2.63761377],
        [ 2.83997293],
        [ 3.04233208]])

I’m at my wits end. I’ve tried casting the profit_predicted matrix to a 1 dimensional flattened numpy array, but the bug does not go away, so I really do not know what’s the cause here.

Thank you.

@kohaugustine It doesn’t plot anything because you are using np.matrix, and Plotly doesn’t recognize this type of data. Convert it to a flattened numpy array and the line(s) will show up.
Paste here, please, your code with converted profit_predicted to np.array to see why the line wasn’t plotted.

@empet the code that I used to flatten profit_predicted was: np.array(profit_predicted).T. The result from that still did not work when I passed it into the y parameter.

Your reply gave me further confidence that the root cause for the line not showing up has to do with the unsuitable array type, so I tried to find other more certain ways that I could flatten profit_predicted, and realized that there is an numpy.ndarray.flatten() method (pardon me, I’m still new to numpy) and found that right ways are np.asarray(profit_predicted).flatten() or np.array(profit_predicted).flatten(). Both of these ways give a resulting array of:

np.asarray(profit_predicted).flatten()[0:5]
array([ 2.23289546,  2.43525461,  2.63761377,  2.83997293,  3.04233208])

and when I looked back to my original code, I realized that there was a feature in the original output that I overlooked:

np.array(profit_predicted).T[0:5]
array([[  2.23289546,   2.43525461,   2.63761377,   2.83997293,
          3.04233208,   3.24469124,   3.44705039,   3.64940955,
          3.8517687 ,   4.05412786,   4.25648701,   4.45884617,
          4.66120532,   4.86356448,   5.06592363,   5.26828279,
          5.47064194,   5.6730011 ,   5.87536025,   6.07771941,
......
     17.61219126,  17.81455041,  18.01690957,  18.21926872,
     18.42162788,  18.62398703,  18.82634619,  19.02870534,
     19.2310645 ,  19.43342365,  19.63578281,  19.83814196,
     20.04050112,  20.24286027,  20.44521943,  20.64757858,
     20.84993774,  21.05229689,  21.25465605,  21.4570152 ,
     21.65937436,  21.86173351,  22.06409267,  22.26645183]])

now its totally clear to me why my “flattening” also did not work!

I’m now able to generate the line:

It was quite a silly mistake, so I really appreciate your help! Thank you very much!