将xtick标签与数据对齐

时间:2015-01-16 13:00:15

标签: python matplotlib plot

我正在尝试绘制一些数据,并希望有实时时间戳打印在xticks上而不是数字上(从time = np.linspace(0,(numOfSamples)/60,numOfSamples)生成)。

以下是我的数据文件示例:

01/01/2015 10:44:10.438,65,8.1
01/01/2015 10:44:11.438,65,8.1
01/01/2015 10:44:12.438,65,7.3
01/01/2015 10:44:13.438,65,7.3
01/01/2015 10:44:14.438,70,6.6
01/01/2015 10:44:15.438,73,6.6
01/01/2015 10:44:16.438,74,6.9
01/01/2015 10:44:17.438,73,6.9
01/01/2015 10:44:18.438,68,7.2

我有大约2754个时间戳(每秒)和该文件中的数据点,每个时间戳对应于每个数据点。所以第一列是我希望在我的xaxis上显示的内容,第二列和第三列是我绘制的数据点。

首先我试过这个:

time_label = np.array(data_header[:,0])
time = np.linspace(0,(numOfSamples)/60,numOfSamples)
plt.xticks(time,time_label)

然而,这会绘制所有2754个时间戳,而不是8-10个时间戳对应于绘图上的正确位置(没有指定xtick,绘图上有大约8-10个刻度)。

plt.xticks

我也尝试使用ax.set_xticklabels(time_label)但是这只使用前几个时间戳来替换图上现有的8-10个刻度。

set_xticklabels

这是原始刻度(使用时间)

enter image description here

我在尝试以下答案时遇到以下错误:

    draw(artist, renderer, *args, **kwargs)
  File "C:\Users\aad0002\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\axis.py", line 1091, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "C:\Users\aad0002\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\axis.py", line 945, in _update_ticks
    tick_tups = [t for t in self.iter_ticks()]
  File "C:\Users\aad0002\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\axis.py", line 889, in iter_ticks
    majorLocs = self.major.locator()
  File "C:\Users\aad0002\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\dates.py", line 802, in __call__
    self.refresh()
  File "C:\Users\aad0002\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\dates.py", line 819, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "C:\Users\aad0002\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\dates.py", line 564, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "C:\Users\aad0002\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\dates.py", line 311, in num2date
    return _from_ordinalf(x, tz)
  File "C:\Users\aad0002\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\dates.py", line 214, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1

我定义了数字并动态添加子图(基于我传递给脚本的参数数量)。例如:

fig = plt.figure(figsize=(12, 10))
fig.suptitle(plot_title + ' -- ' + time_title, fontsize=18, **font)
fig.subplots_adjust(bottom=-1.6)

ax1 = fig.add_subplot(611)
ax2 = fig.add_subplot(612)
ax3 = fig.add_subplot(613)
ax4 = fig.add_subplot(614)
ax5 = fig.add_subplot(615)
ax6 = fig.add_subplot(616)

1 个答案:

答案 0 :(得分:1)

我想最简单的方法是将x值字符串转换为datetime对象。

这可以通过以下方式完成:

from datetime import datetime
from matplotlib import pyplot as plt
from matplotlib.dates import DateFormatter

format = '%d/%m/%Y %H:%M:%S.%f'
time_list = ['01/01/2015 10:44:10.438',
                    '01/01/2015 11:15:15.438',
                    '01/01/2015 13:44:50.438']
t_as_datetimes = [datetime.strptime(t, format) for t in time_list]
plt.plot(t_as_datetimes, [1, 3, 2])

调整xticks位置和格式:

# Set position of xticks individually
x_tick_positions = [datetime(2015, 1, 1, 11, 15, 10), datetime(2015, 1, 1, 13, 30, 30)]
plt.xticks(x_tick_positions)

# Adjust format of xticks
xlabel_format = DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(xlabel_format)
plt.show()

您很可能需要调整 x_tick_positions 列表和 xlabel_format 以符合您的需求。