【问题标题】:Time in x axis matplotlib overlapx轴matplotlib重叠的时间
【发布时间】:2017-01-23 17:56:39
【问题描述】:

我使用以下函数及时绘制比特率

#!/usr/bin/python3
import matplotlib.pyplot as plt
import datetime

def plotBitrate(time,bitrate,filename):
    time = [datetime.datetime.strptime(elem, '%H:%M:%S.%f') for elem in time]

    plt.plot(time,bitrate)
    plt.xlabel('Time')
    plt.ylabel('Bitrate kbits/s')

    plt.autofmt_xdate()
    plt.savefig(filename, dpi = 300)
    plt.close()

数据看起来像这样

Time        bitrate
00:00:00.95 33528.2
00:00:01.47 30013.2
00:00:01.99 26238.5

但是这种时间格式会导致绘图中的记号重叠

我尝试使用 plt.autofmt_xdate() 格式化 x 轴,但这不起作用,我得到 AttributeError: module 'matplotlib.pyplot' has no attribute 'autofmt_xdate'。

是否可以格式化 x 轴以使刻度不重叠?

最好的问候

【问题讨论】:

  • 有几种方法可以使刻度不重叠:(a) 使用较小的字体大小,(b) 使用较少的刻度(即最多显示 N 个刻度),(c) 更改刻度标签' 方向(例如垂直)。你想往哪个方向去?
  • 当我将方向更改为 ex plt.xticks(time,rotation=45) 时,时间格式会丢失并且显示不正确。也许更少的滴答声或最大的滴答声是更好的解决方案?
  • 你试过plt.gcf().autofmt_xdate()吗?
  • plt.gcf().autofmt_xdate() 以 45⁰ 角倾斜刻度。是否可以格式化时间数据,以便 matplotlib 会根据数据自动重新格式化?例如,使用 gnuplot 我可以使用 set xdata time 并且 gnuplot 将自动正确格式化数据。 matplotlib 中是否有等价物?
  • 重新格式化是什么意思?数据格式不正确?能给我举个例子吗? this example 能帮上忙吗?

标签: python-3.x matplotlib


【解决方案1】:

autofmt_xdate() 确实是防止标签重叠的好方法。但是pyplot没有这个方法,而是figure的方法。

因此

plt.gcf().autofmt_xdate()

会做的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2020-10-14
    • 2018-08-20
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    相关资源
    最近更新 更多