简体   繁体   中英

Matplotlib milliseconds tick in x-axis

I'm trying to plot a time-series data from a csv file using Matplotlib. Below are the first few lines from the csv file.

YYYY-MO-DD HH-MI-SS_SSS,X,Y,Z
2019-12-15 11:01:35.000,-0.2937,0.8477,2.2274
2019-12-15 11:01:35.005,-0.2937,0.8477,2.2274
2019-12-15 11:01:35.010,-0.2937,0.8477,2.2274
2019-12-15 11:01:35.014,0.3231,-1.7574,-4.6244
2019-12-15 11:01:35.021,0.3231,-1.7574,-4.6244
2019-12-15 11:01:35.025,0.3231,-1.7574,-4.6244
2019-12-15 11:01:35.030,0.7319,-4.9294,-4.6236
2019-12-15 11:01:35.035,0.7319,-4.9294,-4.6236

The interval is 5 milliseconds (200 Hz) and the file contains one second worth of data (200 rows). My goal is to set the major x-ticks every 100 milliseconds, that is:

35.000, 35.100, 35.200, ...., 35.800, 35.900

I tried using the dateFormatter but cannot manage. How can I do this?

#import packages
from matplotlib import pyplot as plt
from matplotlib import dates
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import pandas as pd
import numpy as np
#import math
%matplotlib inline

df = pd.read_csv("one-second.csv",index_col="YYYY-MO-DD HH-MI-SS_SSS",parse_dates=True)

fig_1, axes = plt.subplots(3, 1, figsize=(12,12),constrained_layout=True)
axes[0].plot(df['X'],color='red')
axes[0].xaxis.set_major_formatter(dates.DateFormatter("%S.%f"))
axes[0].set_title("X Signal")
axes[0].set_xlabel('Time')
axes[0].set_ylabel('Amplitude')
axes[1].plot(df['Y'],color='green')
axes[1].set_title("Y Signal")
axes[1].set_xlabel('Time')
axes[1].set_ylabel('Amplitude')
axes[2].plot(df['Z'],color='blue')
axes[2].set_title("Z Signal")
axes[2].set_xlabel('Time')
axes[2].set_ylabel('Amplitude')

This is how I am managing so far.

到目前为止的情节

Thanks for your input, it helped me to dig further and find a (probably) better method as shown below.

ax2 = df['Y'].plot(figsize=(12,5),color='red')
ax2.set(xlabel='second.microseconds')
ax2.xaxis.set_major_locator(dates.MicrosecondLocator(interval=100000, tz=None))
ax2.xaxis.set_major_formatter(dates.DateFormatter('%S.%f'))

在此处输入图像描述

Took me some time to wrap my head around this one...

First create an array of datetimes using np.datetime64 :

custom_array=np.arange(np.datetime64('2019-12-15 11:01:35.0'), np.datetime64('2019-12-15 11:01:36.0'),dtype='datetime64[100ms]')

This will return:

array(['2019-12-15T11:01:35.000', '2019-12-15T11:01:35.100', '2019-12-15T11:01:35.200', '2019-12-15T11:01:35.300', '2019-12-15T11:01:35.400', '2019-12-15T11:01:35.500', '2019-12-15T11:01:35.600', '2019-12-15T11:01:35.700', '2019-12-15T11:01:35.800', '2019-12-15T11:01:35.900'], dtype='datetime64[100ms]')

Then use this custom array to set your ticks:

axes[0].xaxis.set_ticks(custom_array) axes[0].xaxis.set_major_formatter(dates.DateFormatter("%S.%f"))

This will create a tick mark every 100 milliseconds, starting at 11:01:35:

在此处输入图像描述

The easiest way is to set the DataFrame index directly to the millisecond values. Then you can specify the ticks you like as a simple range, eg:

import pandas as pd
import numpy as np
%matplotlib inline

df = pd.read_csv("one-second.csv", parse_dates=True) 
df.index = np.arange(start=0, stop=5*len(df), step=5)

df.plot(xticks=np.arange(start=0, stop=5*len(df), step=100))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM