How to only plot data on the hour from a 6 hour dataset cube (iris)?

173 views Asked by At

So far my code to plot a a graph is like so:

iplt.plot(pressure_no1, color='black')
plt.xlabel('Time / hour of day')
plt.ylabel('Atmospheric pressure / kPa')
iplt.show()

it is a 6 hour cube (although is 2dimensional) data set, with 420 data points. I need just the data points hr=0, hr=1 hr=2, hr=3, hr=4, hr=5 to be plotted and none in between the hour.

could something like the following work?

pressure_no1_hrs = pressure_no1.coord('hour'==int).points
plt.plot(pressure_no1_hrs)

Image of graph with all data points and start of the time coordinates Image of last lot of time coordinates

2

There are 2 answers

1
RuthC On BEST ANSWER

For an Iris-specific solution, you could use a PartialDateTime:

import iris
import iris.quickplot as qplt
import numpy as np
import matplotlib.pyplot as plt

measures = np.random.normal(size=(360, ))
timepoints = np.arange(start=0, stop=360)

cube = iris.cube.Cube(measures, long_name='atmospheric_pressure', units='kPa')
time_coord = iris.coords.DimCoord(
    timepoints, units='minutes since 2020-07-01 00:00', standard_name='time')
cube.add_dim_coord(time_coord, 0)

subcube = cube.extract(iris.Constraint(time=iris.time.PartialDateTime(minute=0)))

qplt.plot(cube, label='All data')
qplt.plot(subcube, label='Hourly data')

plt.legend()

plt.show()

enter image description here

0
Molitoris On
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


measures = np.random.normal(size=(360, ))
timepoints = np.arange(start=0, stop=360)


df = pd.DataFrame({'measure': measures, 'timepoint': timepoints})

is_hour = df.loc[:, 'timepoint'] % 60 == 0  # <- Only select full hour

plt.plot(df.loc[is_hour, 'measure'])
plt.xlabel("Time [min]")
plt.ylabel("Atmospheric pressure / kPa")
plt.show()