I have several data rows from measurements with x/y-data which I would like to represent as a 2d-surface/3d-surface. For a short test I wrote the following demo script:
#!/usr/bin/env python3
import h5py
import numpy as np
from numpy.core.numeric import full
import scipy.constants as scco
from scipy.signal import butter, lfilter, freqz
from scipy.integrate import solve_ivp
import matplotlib
matplotlib.use('Qt5Cairo')
from matplotlib import pyplot as plt
from scipy import interpolate, optimize
from PIL import Image, PngImagePlugin
from os import listdir
from os.path import isfile, join
import math
from string import Template
import subprocess
import types
import functools
from enum import Enum
import sys
first_position = 2
second_position = 4
third_position = 6
x_vec = np.linspace(0, 10, 1024)
def gen_function(x_vec, f_pos, factor):
return np.exp(-np.power(x_vec - f_pos, factor))
first_vec = gen_function(x_vec, first_position, 32)
second_vec = gen_function(x_vec, second_position, 8)
third_vec = gen_function(x_vec, third_position, 2)
plt.plot(x_vec, first_vec)
plt.plot(x_vec, second_vec)
plt.plot(x_vec, third_vec)
plt.show()
ax = plt.subplot(1, 1, 1, projection="3d")
ax.plot(x_vec, np.ones(x_vec.size) * first_position * 0.1, first_vec)
ax.add_collection3d(plt.fill_between(x_vec, 0.95 * first_vec, 1.05 * first_vec, alpha=0.3), zs = first_position, zdir='y')
ax.plot(x_vec, np.ones(x_vec.size) * second_position * 0.1, second_vec)
ax.add_collection3d(plt.fill_between(x_vec, 0.95 * second_vec, 1.05 * second_vec, alpha=0.3), zs = second_position, zdir='y')
ax.plot(x_vec, np.ones(x_vec.size) * third_position * 0.1, third_vec)
ax.add_collection3d(plt.fill_between(x_vec, 0.95 * third_vec, 1.05 * third_vec, alpha=0.3), zs = third_position, zdir='y')
plt.show()
Here, x_vec represents my x-data from my measurements, and the vectors first_vec, second_vec and third_vec represent example y data. When executing it, I get
I would now like to find a way to interpolate the empty space in between, such that I either can have a 3d-surface plot instead, or reduce the data to a 2d-surface plot. What could I use for that?
Related
I am subplotting over a loop, but the output is really crappy. I think it's stacking each single yticks.
Image Output
I need to give the image a single y axis, that's why I'm saving y_max and y_min for each iteration.
import matplotlib.pyplot as plt
import scipy
from scipy.optimize import curve_fit
from matplotlib import rc
rc('text', usetex=True)
import numpy as np
import math
from ctypes import *
import sys
np.finfo(np.dtype("float64"))
correlation_elements = 5
y_mag_max = np.zeros(correlation_elements)
y_mag_min = np.zeros(correlation_elements)
for i in range (correlation_elements):
fig_correl = plt.figure("correlations")
fig_correl.suptitle('Correlations')
start = i
end = i+correlation_elements
energy_correl_array = np.linspace(start, end, correlation_elements)
ax_correl_1 = fig_correl.add_subplot(1, 1, 1)
correlation_x_axis = np.linspace(0, correlation_elements-1, correlation_elements)
ax_correl_1.plot(correlation_x_axis, energy_correl_array,'.', label=r'$beta$ = {val:}'.format(val=i))
y_mag_max[i] = np.max(energy_correl_array)
y_mag_min[i] = np.min(energy_correl_array)
#Plotting Correlations
fig_correl.legend()
y_max = np.max(y_mag_max)
y_min = np.min(y_mag_min)
ax_correl_1.set_ylim(y_min, y_max)
ax_correl_1.set_ylabel('Energy')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import time
from sklearn.decomposition import PCA
t = np.random.randint(0, 50, (10, 10))
X = tpca = PCA(n_components=2)
X3d = pca.fit_transform(X)
X3d= np.round(X3d, 2)
print (X3d)
How to turn the coordinate found from distance matrix to be positive?
You can use absolute, import math library and use abs().
example:
import math
distance = -40
print(abs(distance))
Output:
40
I am currently having problem understanding how concatenating three numpy matrices can cause data to be changed. The three numpy matrices contains data for what should be shown in a spectogram.
static:
delta:
delta_delta:
but somehow when i concatenate them [static delta delta_delta] and make the same plot it looks like this:
why has the data set become corrupt in the last one?.. Here is how i concatenate and make the plots.
import os
import sys
from os import listdir
from os.path import isfile, join
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sb
import matplotlib
from matplotlib import cm
from PIL import Image
from python_speech_features import logfbank
from python_speech_features import delta
import scipy.io.wavfile as wav
import librosa
y,sr = librosa.load(fp,sr=16000) #fp being path to audio file
static_data = logfbank(y,sr,nfilt=13,preemph=0)
delta_data = delta(static_data,1)
delta_delta_data = delta(static_data,2)
output_hstacked = np.concatenate((static_data,delta_data,delta_delta_data),axis=0)
print output_hstacked.shape
Y = np.array(range(0,output_hstacked.shape[1]))
X = np.array(range(0,output_hstacked.shape[0]))
X,Y = np.meshgrid(X, Y)
plt.pcolormesh(X,Y,output_hstacked.T,cmap=cm.jet)
plt.xlabel('Frames(s)')
plt.ylabel('Frequency(Hz)')
plt.title('MFSS features of ' + filename)
#plt.yscale('log')
plt.colorbar()
#plt.show()
plt.savefig(spectogram_path_test+"/"+modified_name+"_stacked.png")
plt.close()
Y = np.array(range(0,static_data.shape[1]))
X = np.array(range(0,static_data.shape[0]))
X,Y = np.meshgrid(X, Y)
plt.pcolormesh(X,Y,static_data.T,cmap=cm.jet)
plt.xlabel('Frames(s)')
plt.ylabel('Frequency(Hz)')
plt.title('MFSS features of ' + filename)
#plt.yscale('log')
plt.colorbar()
#plt.show()
plt.savefig(spectogram_path_test+"/"+modified_name+"_static.png")
plt.close()
plt.pcolormesh(X,Y,delta_data.T,cmap=cm.jet)
plt.xlabel('Frames(s)')
plt.ylabel('Frequency(Hz)')
plt.title('MFSS features of ' + filename)
#plt.yscale('log')
plt.colorbar()
#plt.show()
plt.savefig(spectogram_path_test+"/"+modified_name+"_delta.png")
plt.close()
plt.pcolormesh(X,Y,delta_delta_data.T,cmap=cm.jet)
plt.xlabel('Frames(s)')
plt.ylabel('Frequency(Hz)')
plt.title('MFSS features of ' + filename)
#plt.yscale('log')
plt.colorbar()
#plt.show()
plt.savefig(spectogram_path_test+"/"+modified_name+"_delta_delta.png")
plt.close()
audio file can be found here
I am using the below codes to quantise the input signal for quantisation interval of 0.5 and this should give me staircase signal.The algorithm used here is same as used in Simulink.Could any one help me plot the quantised signal.
import numpy as np
import matplotlib.pyplot as plt
for i in range(0,10):
q=0.5;
x=q*np.round(i/q);
plt.plot(i,x)
plt.xlim([0,10])
plt.ylim([0,10])
plt.hold()
plt.grid()
plt.show()
Do you mean something like this?
import numpy as np
import matplotlib.pyplot as plt
q = 0.5
x = np.linspace(0, 10, 1000)
y = q * np.round(x/q)
plt.plot(x,y)
So I'd like to plot simple gamma function, but I have some problems. My code is:
#!/usr/bin/env python
# -*- coding: cp1250 -*-
#import math
from scipy.special import *
#from scitools.std import *
from pylab import *
def f1(x):
return gamma(x)
x = linspace(-6, 6, 512)
y1 = f1(x)
# Matlab-style syntax:
plot(x, y1)
xlabel('x')
ylabel('y')
legend(r'$\Gamma(x)$')
grid(True)
show()
I tried importing the gamma function from math, and from scipy.special but I get the following error:
Traceback (most recent call last): File "D:/faxstuff/3.godina/kvantna/plotgamma.py", line 13, in y1 = f1(x) File "D:/faxstuff/3.godina/kvantna/plotgamma.py", line 9, in f1 return gamma(x) File "mtrand.pyx", line 1599, in mtrand.RandomState.gamma (numpy\random\mtrand\mtrand.c:8389) ValueError: shape <= 0
How to do it? This should be easy, but I seem to fail :(
One of the modules (pylab, I think) is shadowing the gamma function by the gamma random variable function. This works, but I had to turn off the call to legend (I'm not sure why, yet).
from scipy.special import gamma as Gamma
#from scitools.std import *
from pylab import *
def f1(x):
return Gamma(x)
x = linspace(-6, 6, 512)
y1 = f1(x)
gca().set_autoscale_on(False)
# Matlab-style syntax:
plot(x, y1)
xlabel('x')
ylabel('y')
# legend(r'$\Gamma(x)$')
axis([-6, 6, -100, 100])
grid(True)
show()
Try this in a Sage notebook:
# Simple example demonstrating how to interact with matplotlib directly.
# Comment plt.clf() to get the plots overlay in each update.
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
#interact
def plot_gamma(a=(1,(1,10)), loc=(0,(0,10)), scale=(1,(1,10))):
rv = stats.gamma(a, loc, scale)
x = np.linspace(-1,20,1000)
plt.plot(x,rv.pdf(x))
plt.grid(True)
plt.savefig('plt.png')
plt.clf()