Search code examples
pythonmatplotlibpython-iris

How to insert line function (x=) onto iris data cube graph without changing axis scale?


I have attempted to plot a line on my dataset at hr=2, however it changes my x axis scale by about 35 years for some reason. The dataset has 420 datapoints across 6 hours, hence the number in the middle two lines.

fig= plt.figure(figsize=(9,2.7))
iplt.plot(pressure_no1, color='black')

plt.plot(np.linspace(-2, 4, 20), pressure_no1.data)
plt.axvline(0)

plt.xlabel('Time / hour of day')
plt.ylabel('Atmospheric pressure / kPa')
iplt.show()

This code gives me this graph: skewed scale graph the far left blue line is the inserted function line, and far right is the actual data

And this is how the scale should actually be: original graph

Please help, i have no idea how to fix this. Thanks in advance :)


Solution

  • Since you are plotting your cube against time, you probably need to specify your vertical line with a datetime:

    import datetime
    
    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)
    
    qplt.plot(cube, color='k')
    plt.axvline(datetime.datetime(2020, 7, 1, 2, 0), color='b')
    
    plt.show()
    

    enter image description here