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

用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 展示了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计