Plot for every 10 minutes in datetime

datetick
plot datetime python
plot datetime matlab
matlab datetick more ticks
matlab plot time axis
pandas plot datetime
plot date vs value in python
matlab datetick interval

The 'df' I am using has multiple rows for each datetime. I want to plot a scatterplot of all coordinates with the same datetime for every 10 minutes. There is a data entry for every location, every 10 minutes in df_data

It works if I manually input the times into t_list = [datetime(2017, 12, 23, 06, 00, 00), datetime(2017, 12, 23, 06, 10, 00), datetime(2017, 12, 23, 06, 20, 00)]but I want to replace this with something that uses the dates from df so I can use it for multiple datasets.

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import numpy as np

df_data = pd.read_csv('C:\data.csv')
df_data['datetime'] = pd.to_datetime(df_data['TimeStamp'] )
df = df_data[(df_data['datetime']>= datetime(2017, 12, 23, 06,00, 00)) &
         (df_data['datetime']< datetime(2017, 12, 23, 07, 00, 00))]

##want a time array for all of the datetimes in the df
t_list = [datetime(2017, 12, 23, 06, 00, 00), datetime(2017, 12, 23, 06, 10, 00), 
datetime(2017, 12, 
23, 06, 20, 00)]

for t in t_list:
    t_end = t + timedelta(minutes = 10)
    t_text = t.strftime("%d-%b-%Y (%H:%M)")

    #boolean indexing with multiple conditions, you should wrap each single condition in brackets
    df_t = df[(df['datetime']>=t) & (df['datetime']<t_end)]

    #get data into variable
    ws = df_t['Sp_mean']
    lat = df_t['x']
    lon = df_t['y']
    col = 0.75

    #calc min/max for setting scale on images
    min_ws = df['Sp_mean'].min()
    max_ws = df['Sp_mean'].max()

    plt.figure(figsize=(15,10))
    plt.scatter(lon, lat, c=ws,s=300, vmin=min_ws, vmax=max_ws)  
    plt.title('event' + t_text,fontweight = 'bold',fontsize=18)
    plt.show()

I have tried a few ways of attempting to make a copy of datetime as an iterable list which haven't given me the results I am after, the most recent below:

date_arrray = np.arange(np.datetime64(df['datetime']))
df['timedelta'] = pd.to_timedelta(df['datetime'])

example dataset

Seams you are not familiar with pandas. You should check the resample function. Let df_data be your raw data:

# make a DatetimeIndex and resample it to 10-Min interval
df_data.index = pd.to_datetime(df_data['TimeStamp'])
resampled_data = df_data.resample('10Min')

# loop it:
min_ws = df['Sp_mean'].min()
max_ws = df['Sp_mean'].max()
col = 0.75
for start_time, sampled_df in resampled_data:
    ws = sampled_df['Sp_mean']
    lat = sampled_df['x']
    lon = sampled_df['y']
    plt.figure(figsize=(15,10))
    plt.scatter(lon, lat, c=ws,s=300, vmin=min_ws, vmax=max_ws)  
    plt.title('event' + start_time.strftime('%Y-%m-%d %H:%M:%S'),fontweight = 'bold',fontsize=18)
    plt.show()

How to set datetick at 10 minute intervals when data starts at random , I am plotting experimental data versus time via a function. I want to plot the data every 10 minutes but I want the intervals to be multiples of 10. For example, if my​  The 'df' I am using has multiple rows for each datetime.I want to plot a scatterplot of all coordinates with the same datetime for every 10 minutes.. It works if I manually input the times into t_list = [datetime(2017, 12, 23, 06, 00, 00), datetime(2017, 12, 23, 06, 10, 00), datetime(2017, 12, 23, 06, 20, 00)]but I want to replace this with something that uses the dates from df so I can use it

I tried this dataset:

           datetime  x  y
0  31/10/2017 23:50  1  9
1  31/10/2017 23:50  1  9
2  31/10/2017 23:50  1  9
3  31/10/2017 23:40  1  9
4  31/10/2017 23:40  1  9
5  31/10/2017 23:40  1  9
6  31/10/2017 23:30  1  9
7  31/10/2017 23:30  1  9
8  31/10/2017 23:20  1  9

And this code:

a = [["31/10/2017 23:50", 1,9],["31/10/2017 23:50", 1,9],["31/10/2017 23:50", 1,9],["31/10/2017 23:40", 1,9],["31/10/2017 23:40", 1,9],["31/10/2017 23:40", 1,9],["31/10/2017 23:30", 1,9],["31/10/2017 23:30", 1,9],["31/10/2017 23:20", 1,9]]
df = pd.DataFrame(a,columns=["TimeStamp","x","y"])
df["datetime"] = pd.to_datetime(df["TimeStamp"])
t_list = df.groupby("datetime").all().index
print(t_list)
# DatetimeIndex(['2017-10-31 23:20:00', '2017-10-31 23:30:00',
# ...                '2017-10-31 23:40:00', '2017-10-31 23:50:00'],
# ...               dtype='datetime64[ns]', name='datetime', freq=None)

Plot Dates and Durations - MATLAB & Simulink, This example shows how to create line and scatter plots of datetime and duration Create t as seven linearly spaced duration values between 0 and 3 minutes. Plot the vectors using the plot function. t = datetime (2014,6,28) + calweeks (0:9); y = rand (1,10); plot (t,y); By default, plot chooses tick mark locations based on the range of data. When you zoom in and out of a plot, the tick labels automatically adjust to the new axis limits. Change the x -axis limits.

hope it helps

new_df = df.groupby('datetime')

for hour_group in new_df.groups:
    min_ws = new_df.get_group(i)['Sp_mean'].min()
    max_ws = new_df.get_group(i)['Sp_mean'].max()

    lat = new_df.get_group(i)['x']
    lon = new_df.get_group(i)['y']
    ws = new_df.get_group(i)['Sp_mean']

    plt.figure(figsize=(15,10))
    plt.scatter(lon, lat, c=ws,s=300, vmin=min_ws, vmax=max_ws)  
    plt.title('event' + hour_group.strftime('%Y-%m-%d %H:%M:%S'),
              fontweight ='bold',fontsize=18)

    plt.show()

Work With Datetime Format in Python, After completing this chapter, you will be able to: Use the datetime object to create easier-to-read time series plots and work with data 10)) # Add x-axis and y-axis ax.plot(boulder_july_2018['date'], Both min and mean are affected by these negative, no data values boulder_july_2018.describe()  Plot time. You can plot time using a timestamp: import matplotlib. import matplotlib.pyplot as plt. import numpy as np. import datetime. # create data. y = [ 2,4,6,8,10,12,14,16,18,20 ] x = [datetime.datetime.now () + datetime.timedelta (hours=i) for i in range (len (y))]

If I understand well you want to make sub-sets of your data grouped by 10 minutes. If your dataset is already sampled, you can just group data by minutes and iterate over the resulting dataframes.

minutes_dfs = df.groupby(df.datetime.map(lambda t: t.minute))

If is not already sampled you can make groups of 10 minutes

minutes_dfs = a.groupby(pd.Grouper(freq='10Min'))

Full Code:

# Example Data Frame
data = {'TimeStamp':['31/10/2017 23:50:00', '31/10/2017 23:50:00', '31/10/2017 23:50:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00'], 
        'datetime':['31/10/2017 23:50:00', '31/10/2017 23:50:00', '31/10/2017 23:50:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00', '31/10/2017 23:40:00'], 
        'x':[1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
        'y':[9, 8, 7, 9, 8, 7, 6, 5, 4, 3, 2, 1], 
        'Sp_mean':[6.49, 5.63, 7.07, 7.86, 7.27, 6.59, 6.78, 8.35, 6.3, 5.82, 8.74, 8.94]}
df = pd.DataFrame(data)
df['TimeStamp'] = pd.to_datetime(df['TimeStamp'])
df['datetime'] = pd.to_datetime(df['datetime'])
df = df.set_index('datetime')
df['datetime'] = df.index
print(df)

#If data is already sampled
#minutes_dfs = df.groupby(df.datetime.map(lambda t: t.minute))

#Not sampled data
minutes_dfs = a.groupby(pd.Grouper(freq='10Min'))

for min, minutes in minutes_dfs:
  t_text = str(min)
  #get data into variable
  ws = minutes['Sp_mean']
  lat = minutes['x']
  lon = minutes['y']
  col = 0.75

  #calc min/max for setting scale on images
  min_ws = df['Sp_mean'].min()
  max_ws = df['Sp_mean'].max()

  plt.figure(figsize=(15,10))
  plt.scatter(lon, lat, c=ws,s=300, vmin=min_ws, vmax=max_ws)  
  plt.title('event' + t_text,fontweight = 'bold',fontsize=18)
  plt.show()

Customize Dates on Time Series Plots in Python Using Matplotlib , When you plot time series data using the matplotlib package in Python, you 10: MODIS Data · Chapter 11: Calculate Vegetation Indices in Python So far in this chapter, using the datetime index has worked well for of the ticks (e.g. a value of 2 to add a tick mark for every other week). ylim=[min, max]. For 2-D line plots, it is more convenient to plot datetime values using the plot function. You can then format the tick labels using the xtickformat and ytickformat functions. datetick is useful when plotting numeric values that are serial date numbers.

Working with Time Series, Date and time data comes in a few flavors, which we will discuss here: Here is a minute-based datetime: In [7]: We can visualize this using the plot() method, after the normal Matplotlib setup boilerplate (see Chapter 4):. In [27]:. %matplotlib​  t2 = 1x3 datetime 08-Mar-2014 00:00:00 09-Mar-2014 00:00:00 10-Mar-2014 01:00:00 Because a daylight saving time shift occurred on March 9, 2014, the third datetime in t2 does not occur at midnight. Verify that the difference between each pair of datetime values in t2 is 24 hours.

matplotlib.dates, Matplotlib provides sophisticated date plotting capabilities, standing on the date2num, Convert datetime objects to Matplotlib dates. self.intervald = { YEARLY : [1, 2, 4, 5, 10, 20, 40, 50, 100, 200, 400, 500, 1000, 2000, For instance, every 7 days is sensible for daily ticks, but for minutes/seconds, 15 or 30 make sense. You could get that if you were trying to do a datetime plot on top of an existing numeric axes. If you are trying to draw on top of an existing axes, then you might have "hold on" in effect, and that might have affected the xtick values that the axes thought should be used.

Fixing common date annoyances, Matplotlib allows you to natively plots python datetime instances, and for 8, 23), , datetime.date(2008, 10, 10), datetime.date(2008, 10, 13),  Plotting timeseries with dates on x-axis and times on y-axis can be a bit tricky in ggplot2. However, with a little trick this problem can be easily overcome. Let’s assume that I wanted to plot when the sun rises in London in 2010. sunriset function in maptools package calculates the sunrise times using algorithms provided […]

Comments
  • Could you post a sample of your dataset or at least the date format you are using?
  • Are you trying to do a "group by"? -- pandas.pydata.org/pandas-docs/stable/reference/api/…
  • @gustavovelascoh - question edited to include a snippet of the dataset
  • @squar_o Do I understand you correctly that you want to have one scatter plot per each 10 minute interval?
  • t_list = df['TimeStamp'].unique(). Than sort it and iterate on the values to get the right slice.
  • I should have made it clear that my data is already sampled every 10 minutes - edited to reflect this.
  • @squar_o That doesn't change anything. You can still use resample to do the grouping and statistics.