将 xtick 标签与数据对齐 [英] Align xtick label to data

查看:51
本文介绍了将 xtick 标签与数据对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to plot some data and would like to have real timestamps to get printed on xticks instead of numbers (generated from time = np.linspace(0,(numOfSamples)/60,numOfSamples)).

这是我的数据文件示例:

Here is example of my data file:

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个时间戳(每秒)和数据点,每个时间戳对应于每个数据点.因此,第一列是我要在x轴上显示的内容,第二列和第三列是我绘制的数据点.

I have about 2754 timestamps (for each second) and data points in that file, with each timestamp corresponding to each data point. So the first column is what I want to be displayed on my xaxis, and second and third are the data points which I plot.

首先我试过这个:

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个刻度).

This however plots all 2754 timestamps, instead of 8-10 that correspond to correct location on the plot (no specifying xtick, there is about 8-10 ticks on a plot).

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

I havealso tried to use ax.set_xticklabels(time_label) however this only uses first few timestamps to replace existing 8-10 ticks on a plot.

这是原始滴答声(使用时间)

Here is original ticks (using time)

我从下面的答案中得到以下错误:

I am getting following error from trying answer below:

    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

我正在定义图形并动态添加子图(基于我传递给脚本的参数数量).例如:

I am define figure and adding subplots dynamically (based on the number of arguments that I pass to the script). For example:

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)

推荐答案

我想最简单的方法是将 x 值字符串转换为日期时间对象.

I guess the easiest way would be to convert the x-value strings to datetime objects.

这可以通过:

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 位置和格式:

To adjust the xticks position and format:

# 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 以满足您的需要.

You most likely have to adjust the x_tick_positions list and the xlabel_format to match your needs.

这篇关于将 xtick 标签与数据对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆