m0_57965808 2021-12-12 15:13 采纳率: 91.6%
浏览 963
已结题

用matplotlib画图报错:ValueError: could not convert string to float: '2020-02-05'

代码如下:

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x_data, y_data, color='tab:blue')
ax.plot(fx_data, fy_data, color='tab:orange')
ax.plot(zx_data, zy_data, color='tab:red')
 
# create the events marking the x data points
xevents1 = EventCollection(x_data, color='tab:blue', linelength=0.05)
xevents2 = EventCollection(fx_data, color='tab:orange', linelength=0.05)
xevents3=EventCollection(zx_data,color='tab:red',linelength=0.05)
 
# create the events marking the y data points
yevents1 = EventCollection(y_data, color='tab:blue', linelength=0.05,
                           orientation='vertical')
yevents2 = EventCollection(fy_data, color='tab:orange', linelength=0.05,
                           orientation='vertical')
yevents3=EventCollection(zy_data,color='tab:red',linelength=0.05,orientation='vertical')
 
# add the events to the axis
ax.add_collection(xevents1)
ax.add_collection(xevents2)
ax.add_collection(xevents2)
ax.add_collection(yevents1)
ax.add_collection(yevents2)
ax.add_collection(xevents3)
ax.add_collection(yevents3)
# set the limits
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
 
ax.set_title('line plot with data points')
 
# display the plot
plt.show()
报错:ValueError: could not convert string to float: '2020-02-05'

问题:我没有设置要将数据变为float的代码呀?请问是哪里出错了呢?还是说用matplotlib画图用的数据都会自动转成float?

  • 写回答

3条回答 默认 最新

  • 陈年椰子 2021-12-12 15:30
    关注

    那三组数据 , 大概样子贴一下, 看提示,就是你有个日期字段(字符串类型), 你按数值来要求 plt 展示了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 陈年椰子 2021-12-12 16:36
    关注

    你这些 日期数据, 只能作为坐标轴, 不能作为数据的。

    img

    数据就是红框里那些。 那你的需求, 我理解是应该把日期作为X轴, 对数据进行排序,补齐空缺日期。 然后画3条折线,对吧?

    评论
  • 陈年椰子 2021-12-13 14:55
    关注
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月20日
  • 已采纳回答 12月12日
  • 创建了问题 12月12日

悬赏问题

  • ¥30 频率与占空比均可调的方波发生器
  • ¥15 VB6.0中PICTUREBOX加载本地图片无法显示
  • ¥100 关于游戏app session获取的问题
  • ¥15 MYSQL数据库建表
  • ¥15 爬虫程序爬取TTGChina网站文章代码
  • ¥35 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作。
  • ¥15 如何用下图方法在AMESim中搭建离心泵模型
  • ¥15 C#连接服务器,请求时报Ssl/Tsl未能建立安全通道
  • ¥15 xcode15build的c++ dylib在10.15上不兼容
  • ¥15 CPLD如何实现在线逻辑分析