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

用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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog