Why do I get the AttributeError attribute when I use the scatter () method, but not when I use plot ()

I want to plot using Matplotlib / pylab and show the date and time on x-axis . For this, I use the datetime module.

Here is the working code that does exactly what it takes -

 import datetime from pylab import * figure() t2=[] t2.append(datetime.datetime(1970,1,1)) t2.append(datetime.datetime(2000,1,1)) xend= datetime.datetime.now() yy=['0', '1'] plot(t2, yy) print "lim is", xend xlim(datetime.datetime(1980,1,1), xend) 

However, when I use the scatter(t2,yy) command instead of plot (t2,yy) , it gives an error:

AttributeError: object 'numpy.string_' does not have attribute 'toordinal'

Why is this happening and how can I show the scatter along with the plot?

A similar question was asked earlier, AttributeError: the object "time.struct_time" does not have the attribute 'toordinal' but the solutions do not help.

+6
source share
2 answers

If you use the int or float for yy , you will not get this error with scatter() :

 yy = [0, 1] 
0
source

Here is an extended example of how I will do this:

 import datetime import matplotlib.pyplot as plt fig, ax = plt.subplots() t2=[ datetime.datetime(1970,1,1), datetime.datetime(2000,1,1) ] xend = datetime.datetime.now() yy= [0, 1] ax.plot(t2, yy, linestyle='none', marker='s', markerfacecolor='cornflowerblue', markeredgecolor='black', markersize=7, label='my scatter plot') print("lim is {0}".format(xend)) ax.set_xlim(left=datetime.datetime(1960,1,1), right=xend) ax.set_ylim(bottom=-1, top=2) ax.set_xlabel('Date') ax.set_ylabel('Value') ax.legend(loc='upper left') 

enter image description here

+2
source

Source: https://habr.com/ru/post/971079/


All Articles