Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting with date times and matplotlib

So, I'm using a function from this website to (try) to make stick plots of some netCDF4 data. There is an excerpt of my code below. I got my data from here.

The stick_plot(time,u,v) function is EXACTLY as it appears in the website I linked which is why I did not show a copy of that function below.

When I run my code I get the following error. Any idea on how to get around this?

AttributeError: 'numpy.float64' object has no attribute 'toordinal'

The description of time from the netCDF4 file:

<type 'netCDF4._netCDF4.Variable'>
float64 time(time)
    long_name: time
    standard_name: time
    units: days since 1900-01-01 00:00:00Z
    axis: T
    ancillary_variables: time_quality_flag
    data_min: 2447443.375
    data_max: 2448005.16667
unlimited dimensions: 
current shape = (13484,)
filling off

Here is an excerpt of my code:

imports:

import matplotlib.pyplot as plot
import numpy as np
from netCDF4 import Dataset
import os
from matplotlib.dates import date2num
from datetime import datetime

trying to generate the plots:

path = '/Users/Kyle/Documents/Summer_Research/east_coast_currents/'
currents = [x for x in os.listdir('%s' %(path)) if '.DS' not in x]

for datum in currents:
    working_data = Dataset('%s' %(path+datum), 'r', format = 'NETCDF4')
    u = working_data.variables['u'][:][:100]
    v = working_data.variables['v'][:][:100]
    time = working_data.variables['time'][:][:100]
    q = stick_plot(time,u,v)
    ref = 1
    qk = plot.quiverkey(q, 0.1, 0.85, ref,
                      "%s N m$^{-2}$" % ref,
                      labelpos='N', coordinates='axes')

    _ = plot.xticks(rotation=70)    
like image 675
K. Shores Avatar asked Apr 26 '26 05:04

K. Shores


1 Answers

Joe Kington answered my question. The netCDF4 file read the times in as a datetime object. All I had to do was replace date2num(time) with time which fixed everything.

like image 153
K. Shores Avatar answered Apr 28 '26 05:04

K. Shores



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!