数组与PColorMesh不兼容

时间:2016-10-06 03:16:00

标签: python-3.x matplotlib physics data-analysis spectrogram

因此,我正在尝试使用pcolormesh来帮助开发一些我试图为我的研究小组提供的数据的频谱图。简而言之,它来自RBSPICEHOPE的3级数据(它会给你一些隐私错误,但只是仔细考虑一下)Van Allen探测卫星上的仪器。当您阅读项目的其他部分的子图时,会建议您暂时阅读它。代码如下:

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()

我很抱歉代码很长但是我会把目光拖到他们需要的地方。

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())

带有以下追溯:

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)

我想我的问题就在这个问题的某个方面。所以我认为第一个重要的事情是看到我告诉它绘制的东西的尺寸。这导致了以下内容:

print(FLUX_RBSPICE_A.shape)

print(Energies_RBSPICE_A.shape)

print(EPOCH_RBSPICE_A.shape)

print(sidx_RBSPICE_A)

print(lidx_RBSPICE_A)

结果:

(233503, 6, 14)
(6, 14)
(233503,)
0
233502

所以这证实了我的怀疑,并把我带到了一堵砖墙。 pcolormesh有什么方面我做了什么?#/ p>

0 个答案:

没有答案