'yerr' shape matches 'y' shape but throws value error - python

I am using the Spyder IDE(5.3.3) with python(3.9.13 64bit) on Ubuntu 20.04LTS. I am trying to plot errorbar by calculating the standard deviation between '5' sets of data. My x-coordinate is named 'RC_AVG', y-coordinate is named 'PMF_AVG' and, the standard deviation is named 'PMF_STD'. After storing data in these lists, I've reshaped all of them to shape (175,1) and then I am using the ax.errorbar command to plot the errorbars but python throws 'Value error': 'yerr' (shape: (175, 1)) must be a scalar or a 1D or (2, n) array-like whose shape matches 'y' (shape: (175, 1)). I am unable to understand the cause of this error and need help in understanding it. However, when I remove the reshape(175,1) from the x,y and, the error coordinates the code works fine and I get the graph. I am attaching the code below:
typeimport numpy as np
import matplotlib.pyplot as plt
fig, (ax1) = plt.subplots(1,1,figsize=(5,5))
file_name = "bsResult-THETA83-IRUN"
result = []
for i in range(1,6):
a = np.array(np.loadtxt(file_name+str(i)+".xvg", dtype = float,skiprows=18,max_rows=175))
result.append(a)
result = np.array(result)
result1 = result.copy()
RC_AVG = np.mean(result1[:,:,0],axis=0).reshape(175,1) ###### x-coordinate
PMF_AVG = np.mean(result1[:,:,1],axis=0).reshape(175,1) ##### y-coordinate
PMF_STD = np.std(result1[:,:,2],axis=0).reshape(175,1) ###### error-coordinate
ax1.set_xlim(0.1,1.70)
ax1.set_xlabel("\u03B6 $(nm)$",fontweight = 'bold',fontsize=12)
ax1.set_ylabel("G $(k_{B}T)$",fontweight = 'bold',fontsize=12)
ax1.errorbar(RC_AVG,PMF_AVG,yerr=PMF_STD,label = 'Nitrogen',color='#D32D41',linewidth=1.0,elinewidth=1.0,
capsize=1.1,ecolor='black',errorevery=(8))
#####################################################################
Traceback (most recent call last):
File "/home/sps/software/yes/lib/python3.9/site-packages/spyder_kernels/py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "/media/sps/hdd/PMF/REFERENCE/pmf-2G6X6-epswdr-wspce-k400/pmfReference.py", line 31, in <module>
ax1.errorbar(RC_AVG,PMF_AVG,yerr=PMF_STD,label = 'Nitrogen',color='#D32D41',linewidth=1.0,elinewidth=1.0,
File "/home/sps/software/yes/lib/python3.9/site-packages/matplotlib/__init__.py", line 1423, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/home/sps/software/yes/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 3588, in errorbar
raise ValueError(
ValueError: 'yerr' (shape: (175, 1)) must be a scalar or a 1D or (2, n) array-like whose shape matches 'y' (shape: (175, 1)) here
I am able to get the errorbars in the plot if I remove the reshape(175,1) from the x,y and, the error coordinates as shown below:
import matplotlib.pyplot as plt
fig, (ax1) = plt.subplots(1,1,figsize=(5,5))
file_name = "bsResult-THETA83-IRUN"
result = []
for i in range(1,6):
a = np.array(np.loadtxt(file_name+str(i)+".xvg", dtype = float,skiprows=18,max_rows=175))
result.append(a)
result = np.array(result)
result1 = result.copy()
RC_AVG = np.mean(result1[:,:,0],axis=0)#.reshape(175,1) ----commented reshape
PMF_AVG = np.mean(result1[:,:,1],axis=0)#.reshape(175,1) ---commented reshape
PMF_STD = np.std(result1[:,:,2],axis=0)#.reshape(175,1) ----commented reshape
ax1.set_xlim(0.1,1.70)
ax1.set_xlabel("\u03B6 $(nm)$",fontweight = 'bold',fontsize=12)
ax1.set_ylabel("G $(k_{B}T)$",fontweight = 'bold',fontsize=12)
ax1.errorbar(RC_AVG,PMF_AVG,yerr=PMF_STD,label = 'Nitrogen',color='#D32D41',linewidth=1.0,elinewidth=1.0,
capsize=1.1,ecolor='black',errorevery=(8))
type here
[enter image description here](https://www.stackoverflow.com/)

Related

Librosa failing to plot mfcc generated

I'm was being able to generate MFCC from system captured audio and plot it, but after some refactor and configuring Tensorflow with CUDA. I used Librosa to generated the mfcc, matplotlib.pyplot with librosa.display to plot the MFCC and sounddevice capturing sound from Stereo mix from windows. The current configuration can create and plot MFCC from sample .wav files but when using system captured sounds it's not able to plot it since its generating a 3D array instead of a 2D when running MFCC. Here is the code that generates and plots
N_MFCC = 40
N_MELS = 40
N_FFT = 512
HOP_LENGTH = 160
MIN_FREQ = 0
MAX_FREQ = None
def create_mfcc(record, sample_rate):
features = librosa.feature.mfcc(record, sample_rate, n_fft=N_FFT,n_mfcc=N_MFCC,
n_mels=N_MELS,hop_length=HOP_LENGTH,fmin=MIN_FREQ, fmax=MAX_FREQ, htk=False)
return features
def plot_and_save_mfcc(mfcc_data, file_name, sample_rate):
plt.figure(figsize=(10, 8))
plt.title('Current audio MFCC', fontsize=18)
plt.xlabel('Time [s]', fontsize=18)
librosa_display.specshow(mfcc_data, sr=sample_rate)
plt.savefig(file_name)
plt.cla()
This generates this stack trace
Traceback (most recent call last):
File "main.py", line 68, in <module>
main()
File "main.py", line 63, in main
start_listening_and_creating_mfcc()
File "main.py", line 48, in start_listening_and_creating_mfcc
plot_and_save_mfcc(mfcc_data, conf.DEFAULT_MFCC_IMAGE_NAME.format(image_count), conf.SAMPLE_RATE)
File "main.py", line 38, in plot_and_save_mfcc
librosa_display.specshow(mfcc_data, sr=sample_rate)
File anaconda3\lib\site-packages\librosa\util\decorators.py", line 88, in inner_f
return f(*args, **kwargs)
File anaconda3\lib\site-packages\librosa\display.py", line 879, in specshow
out = axes.pcolormesh(x_coords, y_coords, data, **kwargs)
File anaconda3\lib\site-packages\matplotlib\__init__.py", line 1361, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 6183, in pcolormesh
X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
File anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 5671, in _pcolorargs
nrows, ncols = C.shape
ValueError: too many values to unpack (expected 2)
I did try debug it and change mfcc configuration, but no success. Also did try to reconfigure my environment but this didn't help either.
EDIT: Here is the mfcc.Shapes for the System audio
(48000, 40, 1)
And for the .wav sample files
(40, 122)
As mentioned I left a function out of the question but here is it and the function the is used to load and create mfcc for the .wav files
def create_mfcc_from_file(file_path):
(signal, sample_rate) = librosa.load(file_path)
librosa_features = create_mfcc(signal, sample_rate)
plot_and_save_mfcc(librosa_features, 'mfcc-librosa', sample_rate)
def start_listening_and_creating_mfcc():
image_count = 0
while True:
my_recording = record_window()
mfcc_data = create_mfcc(my_recording, conf.SAMPLE_RATE)
plot_and_save_mfcc(mfcc_data, conf.DEFAULT_MFCC_IMAGE_NAME.format(image_count), conf.SAMPLE_RATE)
wav.write(conf.DEFAULT_MFCC_IMAGE_NAME.format(image_count) + '.wav', conf.SAMPLE_RATE, my_recording)
image_count += 1
def delta(feat, N):
"""Compute delta features from a feature vector sequence.
:param feat: A numpy array of size (NUMFRAMES by number of features) containing features. Each row holds 1 feature vector.
:param N: For each frame, calculate delta features based on preceding and following N frames
:returns: A numpy array of size (NUMFRAMES by number of features) containing delta features. Each row holds 1 delta feature vector.
"""
if N < 0:
raise ValueError('N must be an integer >0')
NUMFRAMES = len(feat)
denominator = 2 * sum([i**2 for i in range(1, N+1)])
delta_feat = numpy.empty_like(feat)
padded = numpy.pad(feat, ((N, N), (0, 0)), mode='edge') # padded version of feat
for t in range(NUMFRAMES):
delta_feat[t] = numpy.dot(numpy.arange(-N, N+1), padded[t : t+2*N+1]) / denominator
plt.plot(signal, c='c')# [t : t+2*N+1] == [(N+t)-N : (N+t)+N+1]
return delta_feat

matplotlib hist(): weights should have the same shape as x while shape is the same

I'm trying to plot a histogram of a column in a pandas series ('df_plot'). Since I want the y-axis to be a percentage (and not the count), I use the weights option achieve this. As you find in the stacktrace below, the weights array and data series are of the same shape. How come that I still get the error telling me w and x are not the same shape?
Code:
w = 100*(np.zeros_like(df_plot[var]) + 1. / len(df_plot[var]))
print w.shape
print df_plot[var].shape
df_plot[var].hist(bins=100, cumulative=True, weights=w)
Stacktrace:
(9066,)
(9066,)

Traceback (most recent call last):
File "<ipython-input-59-5612307b159e>", line 4, in <module>
df_plot[var].hist(bins=100, cumulative=True, weights=w)
File "C:\Anaconda\lib\site-packages\pandas\tools\plotting.py", line 2819, in hist_series
ax.hist(values, bins=bins, **kwds)
File "C:\Anaconda\lib\site-packages\matplotlib\axes\_axes.py", line 5649, in hist
'weights should have the same shape as x')
ValueError: weights should have the same shape as x
you have nulls in your data set.
s = df_plot[var].dropna()
w = 100*(np.zeros_like(s) + 1. / len(s))
s.hist(bins=100, cumulative=True, weights=w)

Python: float() argument must be a string or a number, not 'interp2d'

this code returns the error "float() argument must be a string or a number, not 'interp2d'". I'm attempting to learn how to interpolate values to fill an array given a few of the values in the array (sorry, bad phrasing). Am I messing up the syntax for the interp2d function or what?
import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import scipy as sp
GCM_file = '/Users/Robert/Documents/Python Scripts/GCMfiles/ATM_echc0003_1979_2008.nc'
fh = Dataset(GCM_file, mode = 'r')
pressure = fh.variables['lev'][:]
lats = fh.variables['lat'][:]
temp = np.mean(fh.variables['t'][0,:,:,:,:], axis = (0, 3))
potential_temp = np.zeros((np.size(temp,axis=0), np.size(temp,axis=1)))
P0 = pressure[0]
#plt.figure(0)
for j in range(0, 96):
potential_temp[:,j] = temp[:, j] * (P0/ pressure[:]) ** .238
potential_temp_view = potential_temp.view()
temp_view = temp.view()
combo_t_and_pt = np.dstack((potential_temp_view,temp_view))
combo_view = combo_t_and_pt.view()
pt_and_t_flat=np.reshape(combo_view, (26*96,2))
t_flat = temp.flatten()
pt_flat = potential_temp.flatten()
temp_grid = np.zeros((2496,96))
for j in range(0, 2496):
if j <= 95:
temp_grid[j,j] = t_flat[j]
else:
temp_grid[j, j % 96] = t_flat[j]
'''Now you have the un-interpolated grid of all your values of t as a function of potential temp and latitude, so you have to interpolate the rest somehow....?'''
xlist = lats
ylist = pt_flat
X,Y = np.meshgrid(xlist,ylist)
temp_cubic = sp.interpolate.interp2d(xlist,ylist, temp_grid, kind = 'cubic')
#temp_linear= griddata(temp_grid, (X,Y), method = 'linear')
#temp_quintic = griddata(temp_grid, (X,Y), method = 'cubic')
plt.figure(0)
plt.contourf(X,Y, temp_cubic, 20)
EDIT: The error with this was pointed out to me. I changed the code from the interpolating line down into this, and I'm still getting an error, which reads "ValueError: Invalid input data". Here's the traceback:
runfile('C:/Users/Robert/Documents/Python Scripts/attempt at defining potential temperature.py', wdir='C:/Users/Robert/Documents/Python Scripts')
Traceback (most recent call last):
File "<ipython-input-27-1ffd3fcc3aa1>", line 1, in <module>
runfile('C:/Users/Robert/Documents/Python Scripts/attempt at defining potential temperature.py', wdir='C:/Users/Robert/Documents/Python Scripts')
File "C:\Users\Robert\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Users\Robert\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/Robert/Documents/Python Scripts/attempt at defining potential temperature.py", line 62, in <module>
Z = temp_cubic(xlist,ylist)
File "C:\Users\Robert\Anaconda3\lib\site-packages\scipy\interpolate\interpolate.py", line 292, in __call__
z = fitpack.bisplev(x, y, self.tck, dx, dy)
File "C:\Users\Robert\Anaconda3\lib\site-packages\scipy\interpolate\fitpack.py", line 1048, in bisplev
raise ValueError("Invalid input data")":
temp_cubic = sp.interpolate.interp2d(xlist, ylist, temp_grid, kind = 'cubic')
ylist = np.linspace(np.min(pt_flat), np.max(pt_flat), .01)
X,Y = np.meshgrid(xlist,ylist)
Z = temp_cubic(xlist,ylist)
plt.contourf(X,Y, Z, 20)
The problem is in the following line. interp2d returns an interpolation function. However, you used it in place of the Z argument to countourf, which is supposed to be a float matrix. See the contourf doc for details.
In particular:
contour(X,Y,Z,N)
make a contour plot of an array Z.
X, Y specify the (x, y) coordinates of the surface
X and Y must both be 2-D with the same shape as Z,
or they must both be 1-D such that
len(X) is the number of columns in Z and
len(Y) is the number of rows in Z.
contour up to N automatically-chosen levels.
In short, I believe that you want to apply the function to X and Y to generate the array you pass in as the third argument.
Credit to both the matplotlib documentation and kindall for showing the conceptual error of my other possibilities.

Python Matplotlib Hist2d with 2d array

I want to make a 2d histogramme by putting two 2D array as argument, Tx and alt_array, same size (56000,40)
def histo_2D(alt, Tx):
u,v = 56000,40
Tx = np.zeros((u,v))
alt_array = np.zeros((u,v))
alt,tx = np.zeros((v)), np.zeros((v))
for i in range(0,v):
alt[i] = i
tx[i] = i
alt_array[:][:] = alt
Tx[:][:] = tx
alt_array[:][:] = alt
print np.shape(Tx), np.shape(alt_array)
plt.hist2d(Tx , alt_array)
But when i try to execute my program, i get this error message :
Traceback (most recent call last):
File "goccp.py", line 516, in <module>
histo_2D(alt,Tx)
File "goccp.py", line 376, in histo_2D
plt.hist2d(Tx , alt_array)
File "/Code/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2847, in hist2d
weights=weights, cmin=cmin, cmax=cmax, **kwargs)
File "/Code/anaconda/lib/python2.7/site-packages/matplotlib/axes.py", line 8628, in hist2d
normed=normed, weights=weights)
File "/Code/anaconda/lib/python2.7/site-packages/numpy/lib/twodim_base.py", line 650, in histogram2d
hist, edges = histogramdd([x, y], bins, range, normed, weights)
File "/Code/anaconda/lib/python2.7/site-packages/numpy/lib/function_base.py", line 288, in histogramdd
N, D = sample.shape
ValueError: too many values to unpack
I've tried to use flattened array, but the result is not really good...
The documentation for hist2d states:
matplotlib.pyplot.hist2d(x, y, bins=10, range=None, normed=False, weights=None, cmin=None, cmax=None, hold=None, **kwargs)
Parameters: x, y: array_like, shape (n, ) :
Thus x and y need to be one dimensional; your values are two dimensional.
Have a look at the example as well, given at the end of the documentation.

Plotting currents data on a map : Too many value to unpack error

So here is my code :
from netCDF4 import *
import datetime as dt
import numpy as np
import numpy.ma as ma
from datetime import date, datetime, timedelta
import matplotlib.pyplot as plt
nc=Dataset('datasets/essai.nc')
time_var = nc.variables[str('forecast_time0')]
wave_var = nc.variables['DIST_GDS0_SFC']
lat = nc.variables['g0_lat_1'][:]
lon = nc.variables['g0_lon_2'][:]
uin = nc.variables['UOGRD_GDS0_DBSL'][:]
vin = nc.variables['VOGRD_GDS0_DBSL'][:]
plt.quiver(lon[::5], lat[::5], uin[::5], vin[::5], scale=200)
And here is the error I get :
Traceback (most recent call last):
File "nctry.py", line 37, in <module>
plt.quiver(lon[::5], lat[::5], uin[::5], vin[::5], scale=200)
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2877, in quive
r
ret = ax.quiver(*args, **kw)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 6627, in quiver
q = mquiver.Quiver(self, *args, **kw)
File "C:\Python27\lib\site-packages\matplotlib\quiver.py", line 394, in __init
__
X, Y, U, V, C = _parse_args(*args)
File "C:\Python27\lib\site-packages\matplotlib\quiver.py", line 356, in _parse
_args
nr, nc = U.shape
ValueError: too many values to unpack
I think the problem comes from the UOGRD and VOGRD which has many values but don't know how manipulate it?
Here is how uogrd looks like :
the forecast_time up as far as a number of 4.
Ok so I used the np.array and shape functions, here is what i've got :
C:\Python27>python nctry.py
(321,) //lat
(720,) //lon
(4, 321, 720) //uogrd
(4, 321, 720) //vogrd
Then I tried this code :
but it only shows one arrow. Can you tell me why?
So now I have some modelling which looks like this :
But this image doesn't change, for [0, :, :], 1, 2 or 3.. What's wrong?
Furthermore, all the arrows are going to the same direction, not really true when we're talking about currents' ocean. I use the deg2rad function.
Finally, I can't imagine the coastlines, is that normal?
Thank you
The problem is that UOGRD (and probably VOGRD too) contains too many dimensions. You need to reduce the number of dimensions by slicing it. However, given the current information, I cannot instruct you which dimension to remove.
However, you should convert the lat, lon, UOGRD and VOGRD to np.array using the command and print out each array's shape
UOGRD = np.array(UOGRD)
print UOGRD.shape
Do this for all 4 of your arrays. This will print out tuples (d1_length, d2_length, ..., dn_length).
If we know the dimensionality of lat=N and lon=M, we should hope that the dimensions of UOGRD will be something like (N, M, x, y, z) and we can slice out the dimensions we don't need.
EDIT:
From the shape functions in your, it shows us that uin and vin are both 3 dimensional. And you can see that len(lat) = 321, len(lon) = 720 and both uin and vin have dimensions (x, 321, 720). That means we want to plot the last two dimensions of uin and vin. Therefore, try this
uin = nc.variables['UOGRD_GDSO_DBSL']
uin = np.array(uin)
uin = uin[0, :, :]
vin = nc.variables['vOGRD_GDSO_DBSL']
vin = np.array(vin)
vin = vin[0, :, :]
Do not do uin = uin.shape as that will set uin = (4, 321, 720) instead of your data. The only thing is, we have to figure out is which index we should put in the slicing. Try [0,:,:] then [1,:,:] ... [3,:,:].

Categories

Resources