【问题标题】:Matplotlib x-axis overlapping using time stringMatplotlib x 轴重叠使用时间字符串
【发布时间】:2018-06-11 19:54:42
【问题描述】:

我想从以下数据创建一个图:

timeArray= ['11:47:46.585', '11:47:46.695', '11:47:46.805', '11:47:46.915', '11:47:47.025', '11:47:47.135', '11:47:47.245', '11:47:47.355', '11:47:47.465', '11:47:47.575', '11:47:47.685', '11:47:47.795', '11:47:47.905', '11:47:48.015', '11:47:48.125', '11:47:48.235', '11:47:48.345', '11:47:48.455', '11:47:48.565', '11:47:48.675', '11:47:48.785', '11:47:48.895', '11:47:49.005', '11:47:49.115', '11:47:49.225', '11:47:49.335', '11:47:49.445', '11:47:49.555', '11:47:49.665', '11:47:49.775', '11:47:49.885', '11:47:49.995', '11:47:50.105', '11:47:50.215', '11:47:50.325', '11:47:50.435', '11:47:50.545', '11:47:50.655', '11:47:50.765', '11:47:50.875', '11:47:50.985', '11:47:51.095', '11:47:51.205', '11:47:51.315', '11:47:51.425', '11:47:51.535', '11:47:51.645', '11:47:51.755', '11:47:51.865', '11:47:51.975', '11:47:52.085', '11:47:52.195', '11:47:52.305', '11:47:52.415']

valueArray = [10382.0, 8372.0, 11117.0, 11804.0, 10164.0, 10221.0, 10488.0, 7910.0, 12911.0, 11422.0, 15361.0, 15424.0, 10629.0, 14993.0, 13827.0, 15164.0, 10514.0, 10356.0, 14638.0, 12272.0, 14980.0, 14391.0, 12984.0, 18967.0, 15792.0, 14753.0, 16205.0, 19187.0, 13922.0, 10787.0, 14500.0, 12918.0, 13985.0, 14695.0, 14014.0, 12087.0, 12163.0, 11424.0, 8598.0, 8573.0, 9986.0, 10315.0, 11449.0, 9146.0, 11160.0, 6861.0, 10211.0, 9097.0, 8443.0, 5446.0, 6354.0, 6829.0, 5786.0, 7860.0]

timeArray 将是 x 轴,valueArray 将是 y 轴。

情节线如下所示:

import matplotlib.pyplot as plt
plt.plot(timeArray,valueArray,'r', label='values over time')

我得到了这张图:

我用过:plt.gcf().autofmt_xdate(),但还是有一次比下一次。

我也试过了:

xaxis = np.linspace(min(timeArray),max(timeArray), 10)
plt.xticks(xaxis) 

但我有一个typeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

有没有一种简单的方法可以保持数据原样但不以微秒为单位显示每一次?

【问题讨论】:

  • 你可以试试这样的stackoverflow.com/a/50034357/6361531
  • 这是通常的“我已经在我的轴上绘制了字符串,现在它们看起来很糟糕。”问题。使用日期而不是字符串。

标签: python matplotlib plot time overlap


【解决方案1】:

我建议您将时间转换为 datetime 对象而不是字符串,然后使用具有适当日期格式的 matplotlib.mdates.DateFormatter()

import matplotlib.dates as mdates
import datetime

fmt = mdates.DateFormatter('%H:%M:%S')
timeArray = [datetime.datetime.strptime(i, '%H:%M:%S.%f') for i in timeArray]

fig, ax = plt.subplots()

plt.plot(timeArray,valueArray,'r', label='values over time')
ax.xaxis.set_major_formatter(fmt)

结果:

【讨论】:

    【解决方案2】:

    你可以这样做:

     import datetime
     import matplotlib.pyplot as plt
     import pandas as pd
    
     timeArray = pd.to_datetime(pd.Series(timeArray))
     plt.plot(timeArray,valueArray,'r', label='values over time')
     plt.show()
    

    输出:

    或者为刻度添加一些旋转:

    plt.xticks(rotation=45) # or 90 to be vertical
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2012-11-11
      相关资源
      最近更新 更多