Blank 3D Plotly Scatter Plot

1.6k views Asked by At
import plotly.graph_objects as go
import numpy as np
import numpy

# Download data set from plotly repo
pts = np.loadtxt(np.DataSource().open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt'))
x, y, z = pts.T

US = [(1970, 4688.5), (1971, 4559.4), (1972, 4807.0), (1973, 4994.6), (1974, 4827.4), (1975, 4612.5), (1976, 4907.3),
      (1977, 5071.4), (1978, 5066.2), (1979, 5105.1), (1980, 4877.4), (1981, 4808.0), (1982, 4562.2), (1983, 4541.5),
      (1984, 4751.8), (1985, 4777.2), (1986, 4736.6), (1987, 4893.1), (1988, 5114.1), (1989, 5171.4), (1990, 5063.9),
      (1991, 5022.3), (1992, 5103.5), (1993, 5222.5), (1994, 5305.7), (1995, 5357.6), (1996, 5505.1), (1997, 5679.0),
      (1998, 5693.0), (1999, 5712.3), (2000, 5915.6), (2001, 5858.2), (2002, 5778.7), (2003, 5854.5), (2004, 5937.6),
      (2005, 5946.6), (2006, 5841.2), (2007, 5919.5), (2008, 5730.6), (2009, 5314.8), (2010, 5555.4), (2011, 5425.1),
      (2012, 5244.1), (2013, 5316.5), (2014, 5389.3), (2015, 5225.4), (2016, 5161.1), (2017, 5128.4), (2018, 5275.5)]

US_x = numpy.asarray([US[x][0] for x in range(0, len(US))])
US_y = numpy.asarray([US[x][1] for x in range(0, len(US))])
z = numpy.asarray([x for x in range(1,50)])

US_x = numpy.reshape(US_x, (7, 7))
US_y = numpy.reshape(US_y, (7,7))
z = numpy.reshape(z, (7, 7))

#fig = go.Figure(data=[go.Mesh3d(x=US_x, y=US_y, z=z, color='lightpink', opacity=0.50)])

fig = go.Figure(data=[go.Scatter3d(x=US_x, y=US_y, z=z,mode='markers')])

fig.update_layout(
    scene=dict(
        xaxis=dict(nticks=4, range=[1970, 2018], ),
        yaxis=dict(nticks=4, range=[4500, 5800], ),
        zaxis=dict(nticks=4, range=[1, 49], ), ),
    width=700,
    margin=dict(r=20, l=10, b=10, t=10))


fig.show()

When I try to plot my data, plotly just shows a 3D graph with no points. I'm not sure what the problem is. When I don't reshape, even though the arrays have the same length of 49, all the variables have different amount of columns. When I do a reshape it makes the array look like 7 lists instead of one big list.

1

There are 1 answers

1
Maximilian Peters On

Plotly's Scatter3d expects a flat, i.e one-dimensional array. You could flatten your arrays first and then pass them to Plotly.

US_x = numpy.reshape(US_x, (7, 7)).flatten()
US_y = numpy.reshape(US_y, (7,7)).flatten()
z = numpy.reshape(z, (7, 7)).flatten()

fig = go.Figure(data=[go.Scatter3d(x=US_x, y=US_y, z=z,mode='markers')])

fig.show() 

enter image description here