So I am trying to use pcolormesh to help develop a spectrogram for some data I'm trying to present for my research group. In short, it's level 3 data from the RBSPICE and HOPE (it'll give you some privacy error but just mull over that) instruments on board the Van Allen Probe Satellite. When you read the subplots thing that is for some other part of my project and would advise that you just read over it for now. The code is as follows:
import math
import numpy as np
import numpy.ma as ma
import scipy.constants as c
from scipy.interpolate import spline
import spacepy as sp
from spacepy import pycdf
from pylab import *
from spacepy.toolbox import windowMean, normalize
import spacepy.plot.utils
import pylab
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.cbook as cbook
import matplotlib.ticker as ticker
from matplotlib.colors import LogNorm
from matplotlib.ticker import LogLocator
from matplotlib.dates import DateFormatter
from matplotlib.dates import DayLocator, HourLocator, MinuteLocator
from matplotlib import rc, rcParams
import matplotlib.dates as mdates
import datetime as dt
import bisect as bi
import seaborn as sea
import sys
import os
import multilabel as ml
import pandas as pd
sea.set_context('poster')
sea.set_style('ticks',{'axes.facecolor':'silver'})
sea.set_palette('muted',color_codes=True)
rc('text', usetex=True)
rc('font', family='Mono')
rcParams['text.latex.preamble']=[r'\usepackage{amsmath}']
RBSPICE_A = pycdf.CDF(r"C:\Users\Schmidt\Desktop\Project\Data\VAP\RBSPICE\A\rbsp-a-rbspice_lev-3_TOFxEH_20151202_v1.1.7-01.cdf")
EPOCH_RBSPICE_A = RBSPICE_A['Epoch'][...]
FLUX_RBSPICE_A = RBSPICE_A['FPDU'][...]
Energies_RBSPICE_A = RBSPICE_A['FPDU_Energy'][...]*10**3
L_RBSPICE_A = RBSPICE_A['L'][...]
MLT_RBSPICE_A = RBSPICE_A['MLT'][...]
SMx_RBSPICE_A = RBSPICE_A['Position_SM'][:, 0]
SMy_RBSPICE_A = RBSPICE_A['Position_SM'][:, 1]
SMz_RBSPICE_A = RBSPICE_A['Position_SM'][:, 2]
RBSPICE_A_Start_time = dt.datetime(2015, 12, 2, 0, 0, 0, 655000)
RBSPICE_A_Finish_time = dt.datetime(2015, 12, 2, 23, 59, 59, 883000)
plt.close('all')
sidx_RBSPICE_A = bi.bisect_left(EPOCH_RBSPICE_A, RBSPICE_A_Start_time)
sidx_RBSPICE_A = int(sidx_RBSPICE_A-(sidx_RBSPICE_A/100))
lidx_RBSPICE_A = bi.bisect_left(EPOCH_RBSPICE_A, RBSPICE_A_Finish_time)
lidx_RBSPICE_A = int (lidx_RBSPICE_A+((len(EPOCH_RBSPICE_A)-lidx_RBSPICE_A)/100))
fig_VAP, axs_VAP = plt.subplots(2)
cmap = plt.get_cmap(cm.jet)
cmap.set_bad('black')
if RBSPICE_A_Start_time.date() == RBSPICE_A_Finish_time.date():
stopfmt = '%H:%M'
else:
stopfmt = '%-m/%-d/%y %H:%M'
title = RBSPICE_A_Start_time.strftime('%m/%d/%y %H:%M')+' - '+RBSPICE_A_Finish_time.strftime(stopfmt)
# if VAP_dt.seconds !=0:
# title = title + ' with '+str(VAP_dt.seconds)+' second time averaging'
for j, ax in enumerate(axs_VAP.T.flatten()):
flix = np.array(FLUX_RBSPICE_A[sidx_RBSPICE_A:lidx_RBSPICE_A, :,j].T)
if VAP_dt==dt.timedelta(0):
fluxwin = flix
timewin = EPOCH_RBSPICE_A[sidx_RBSPICE_A:lidx_RBSPICE_A]
else:
fluxwin=[[0 for y in range(len(flix))] for x in range(len(flix))]
for i, flox in enumerate(flix):
fluxwin[i], timewin = windowMean(flox, EPOCH_RBSPICE_A[sidx_RBSPICE_A:lidx_RBSPICE_A], winsize=VAP_dt, overlap=dt.timedelta(0))
fluxwin[i] = np.array(fluxwin[i])
for x in np.where(np.diff(EPOCH_RBSPICE_A[sidx_RBSPICE_A:lidx_RBSPICE_A])
>dt.timedelta(hours=1))[0]+sidx_RBSPICE_A:
fluxwin[i][bi.bisect_right(timewin, EPOCH_RBSPICE_A[x]):bi.bisect_right(timewin, EPOCH_RBSPICE_A[x+1])]=0
fluxwin = np.array(fluxwin)
fluxwin[np.where(fluxwin<=0)] = 0
x = mdates.date2num(timewin)
#Plot spectrogram, vmin should exclude only 0 values, norm=LogNorm sets the color values to log scale
pax = ax.pcolormesh(x, Energies_RBSPICE_A, fluxwin, shading='turkey',
cmap=cmap, vmin=1, vmax=np.nanmax(FLUX_RBSPICE_A[sidx_RBSPICE_A:lidx_RBSPICE_A,:,:]), norm=LogNorm())
sax = ax.twinx()
plt.setp(sax.get_yticklabels(), visible=False)
sax.tick_params(axis='y', right='off')
ax.set_xlim(RBSPICE_A_Start_time, RBSPICE_A_Finish_time)
ax.set_yscale('log')
ax.set_yticks([100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400])
ax.set_ylim((Energies_RBSPICE_A[0], Energies_RBSPICE_A[-1]))
#Allows non-log formatted values to be used for ticks
ax.yaxis.set_major_formatter(plt.ScalarFormatter())
axs_VAP[0].set_ylabel('Energy (keV)')
axs_VAP[0].set_title('RBSPICE A')
plt.show()
I'm really sorry that the code is very long but I'll drag your eyes to where they need to be.
pax = ax.pcolormesh(x, Energies_RBSPICE_A, fluxwin, shading='turkey',
cmap=cmap, vmin=1, vmax=np.nanmax(FLUX_RBSPICE_A[sidx_RBSPICE_A:lidx_RBSPICE_A,:,:]), norm=LogNorm())
with the following traceback:
ipython-input-37-5721a5ffbf21> in <module>()
44 #Plot spectrogram, vmin should exclude only 0 values, norm=LogNorm sets the color values to log scale
45 pax = ax.pcolormesh(x, Energies_RBSPICE_A, fluxwin, shading='turkey',
---> 46 cmap=cmap, vmin=1, vmax=np.nanmax(FLUX_RBSPICE_A[sidx_RBSPICE_A:lidx_RBSPICE_A,:,:]), norm=LogNorm())
47 sax = ax.twinx()
48 plt.setp(sax.get_yticklabels(), visible=False)
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
1809 warnings.warn(msg % (label_namer, func.__name__),
1810 RuntimeWarning, stacklevel=2)
-> 1811 return func(ax, *args, **kwargs)
1812 pre_doc = inner.__doc__
1813 if pre_doc is None:
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in pcolormesh(self, *args, **kwargs)
5393 allmatch = (shading == 'gouraud')
5394
-> 5395 X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
5396 Ny, Nx = X.shape
5397
C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in _pcolorargs(funcname, *args, **kw)
5009 raise TypeError(
5010 'Incompatible X, Y inputs to %s; see help(%s)' % (
-> 5011 funcname, funcname))
5012 if allmatch:
5013 if not (Nx == numCols and Ny == numRows):
TypeError: Incompatible X, Y inputs to pcolormesh; see help(pcolormesh)
I imagine that my problem was somewhere in this very line. So I figured that the first important thing to to see the dimensions of the stuff I told it to plot. which led to the following:
print(FLUX_RBSPICE_A.shape)
print(Energies_RBSPICE_A.shape)
print(EPOCH_RBSPICE_A.shape)
print(sidx_RBSPICE_A)
print(lidx_RBSPICE_A)
Resulting with:
(233503, 6, 14)
(6, 14)
(233503,)
0
233502
So that de-confirmed my suspicions and has led me to a brick wall. What is the aspect of pcolormesh I done gone goofed up?