【问题标题】:Plotting a list of hours绘制小时列表
【发布时间】:2013-07-05 19:25:50
【问题描述】:

好吧,我搜索了很多关于它的内容,但我仍然没有很好地得到它...... 假设我有:

listHours = ['00:00:000-10:00:000', '10:00:000-20:00:000', '20:00:000-30:00:000',    '30:00:000-40:00:000', '40:00:000-50:00:000', '50:00:000-00:00:000']
listNumbers = ['0.01', '0.02', '0.03', '0.05', '0.05', '0.03'] 

我想要:

plt.plot(listHours, listNumbers)
plt.show()

当我使用刻度时,Matplotlib 不接受这个 listHours(“ValueError:float() 的无效文字:00:00:000-10:00:000”)我得到这个错误:“TypeError: float() argument must是一个字符串还是一个数字”我不知道是不是因为我用错了这个记号或者其他什么......有什么想法吗?我在这里有点沮丧。

已经谢谢了。

【问题讨论】:

  • 为什么你的时间格式是"hr:min:sec - hr:min:sec"?他们不应该只是"hr:min:sec"吗?对于单个小时 (hr:min:sec),您可以执行 matplotlib.dates.datestr2num( <hour string> ) 将其转换为 matplotlib 可以解释的内容。
  • 所以...我没有选择这个。数据就是这样传到我这里的,天知道为什么。

标签: python list matplotlib ticker hour


【解决方案1】:

您可以使用xticks 命令来执行此操作。看下面sn -p -

>>> listHours = ['00:00:000-10:00:000', '10:00:000-20:00:000', '20:00:000-30:00:000','30:00:000-40:00:000', '40:00:000-50:00:000', '50:00:000-00:00:000']
>>> listNumbers = ['0.01', '0.02', '0.03', '0.05', '0.05', '0.03']
>>> listNumbers = [float(elem) for elem in listNumbers]  # Convert to a list of floats.
>>> plt.plot(listNumbers)
>>> plt.xticks(range(len(listHours)), listHours, size='small')
>>> plt.show()

它给你一个像这样的数字

【讨论】:

  • 您是否将listNumbers 转换为数字列表以获得该图?
  • 我错过了,但它似乎很好地绘制了线条。我会将其转换为数字列表。
  • 耶!我实际上试图做类似的事情,但我相信我的 yAxis 是错误的。非常感谢!
猜你喜欢
  • 2020-02-04
  • 2022-11-15
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多