【问题标题】:How to only plot data on the hour from a 6 hour dataset cube (iris)?如何仅从 6 小时数据集立方体(虹膜)中绘制小时数据?
【发布时间】:2021-06-28 09:10:01
【问题描述】:

到目前为止,我绘制图形的代码是这样的:

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

这是一个 6 小时的立方体(虽然是二维的)数据集,有 420 个数据点。我只需要绘制数据点 hr=0, hr=1 hr=2, hr=3, hr=4, hr=5 并且在一小时之间没有。

可以像下面这样工作吗?

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

【问题讨论】:

  • 欢迎来到stackoverflow。 pressure_no1 长什么样子?您可能希望使用模运算符来过滤掉与整小时相对应的时间点。
  • 也许在你提出这个要求之前先检查pressure_no1.coord('hour'==int).points是否有效。如果您在询问之前先测试一些想法会更快。
  • @Molitoris 感谢您的建议。我有点菜鸟,所以我不确定这意味着什么,我该怎么做? pressure_no1 的时间限制为 6 小时,由大气压和时间坐标组成
  • @furas 这没有用,但我想知道我是否会走上正轨。我对 python/iris 编码了解不多
  • 您可以附加数据集的样本吗?

标签: python matplotlib iris-dataset python-iris


【解决方案1】:

对于特定于 Iris 的解决方案,您可以使用 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()

【讨论】:

  • 感谢您的帮助!图表看起来很棒:)
【解决方案2】:
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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2021-04-02
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 2012-12-12
    相关资源
    最近更新 更多