4

I am giving myself an intro to plotting data and have come across some trouble. I am working on a line chart that I plan on making animated as soon as I figure out this problem. I want a graph that looks like this:

However this code I have now:

`x=df_pre_2003['year']
 y=df_pre_2003['nAllNeonic']
 trace=go.Scatter(
     x=x,
     y=y
)
data=[trace]
ply.plot(data, filename='test.html')`

is giving me this: enter image description here So I added y=df_pre_2003['nAllNeonic'].sum() but, now it says ValueError: Invalid value of type 'builtins.float' received for the 'y' property of scatter Received value: 1133180.4000000006

The 'y' property is an array that may be specified as a tuple,
list, numpy array, or pandas Series

Which I tried and it still did not work. The data types for year is int64 and nAllNeonic is float64.

Michael Jones
  • 77
  • 1
  • 8

2 Answers2

1

It looks like you have to sort the values first based on the date. Now it's connecting a value in the year 1997 with a value in 1994.

df_pre_2003.sort_values(by = ['year'])
Michieldo
  • 169
  • 1
  • 2
  • 15
1

This is not to answer this question, but to share my similar case for any future research needs: In my case the error message was coming when I tried to export the django models objects to use it in the plotly scatter chart, and the error was as follows:

The 'x' property is an array that may be specified as a tuple, list, numpy array, or pandas Series

The solution for this in my case was to export the django model info into pandas data frame then use the pandas data frame columns instead of the model fields name.

HassanSh__3571619
  • 1,859
  • 1
  • 19
  • 18
  • did you ever find a cleaner way to do this? – Josh Jun 28 '21 at 03:37
  • I found this article just a few mins after posting, https://community.plotly.com/t/valueerror-received-for-the-x-property-of-scatter-zeromq-live-chart/11692/4 maybe it will help others. it did correct my issue and I did not have to go the DF route. – Josh Jun 28 '21 at 03:40