在matplotlib中为列表/数组绘制散点图 [英] plotting a scatter plot for list/array in matplotlib
问题描述
通过浏览互联网上的示例,我浪费了很多时间.但是我只是想不出为什么我不能针对整数列表绘制一个datetime.date对象列表的简单图形
I have wasted so much time looking through the examples given on internet. But I just cant figure out why can't I plot a simple graph of a list of datetime.date objects against a list of integers
appleProd1 appleProd2 appleProd3 appleProd4 ..... 70 Products
apple.com 2010-09-12 2008-01-01 2009-03-02 .......................
我想绘制启动日期的散点图,其中x轴为日期,y轴为产品.而我这样做
I wanted to plot a scatter plot for the launch dates with x axis as dates and y axis as the products. And I do this
plt.subplot(1, 1, 1)
product_list = []
label_ticks = []
date_list = []
yval = 1
for prod, date in df.iteritems(): #date is a datetime.date object
date = pd.to_datetime(date)
product = prod
date_list.append(date)
prod_list.append(prod)
label_ticks.append(yval)
yval+=1
plt.plot_date(date_list, label_ticks)
最后一行plt.plot给我一个错误,提示TypeError: float() argument must be a string or a number
.我也尝试将两个列表都转换为numpy数组,并使用相同的plt.scatter.同样的错误.两个列表的长度相同.也参考了此网站
The last line plt.plot gives me an error saying TypeError: float() argument must be a string or a number
. I have also tried converting both the lists to numpy array and use same plt.scatter. Same error. Length of both lists is same. Referred to this site also
http://matplotlib.org/api/pyplot_api.html#matplotlib. pyplot.scatter
有些日期也是南.因此,使用date2num进行转换会在此处产生错误.
Some dates are Nan also. So converting using date2num is giving error there.
推荐答案
我弄清楚了问题所在.在date_list中确实存在一些字符串值,因此抱怨plt.plot. plt.plot可以很好地与datetime.date值列表一起使用.无需将datetime.date转换为任何其他格式.
I figured out what the problem was. There were indeed some string values in the date_list so the plt.plot complained. The plt.plot works just fine with a list of datetime.date values. There is no need to convert the datetime.date to any other format.
这篇关于在matplotlib中为列表/数组绘制散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!