I'm new to python and matplotlib, and I'd like to visualise / compare 3 mfcc files stored as numpy arrays in txt format.
I have the Octave code below, and I'd like to know how it can be done using python/matplotlib.
Any help is much appreciated.
load /dir/k11.txt
load /dir/t11.txt
load /dir/a11.txt
subplot(1,2,1);imagesc(j11);axis('xy');colormap(jet);colorbar;subplot(1,2,2);imagesc(t11);axis('xy');colormap(jet);colorbar;
c=[k11(:,end),k11(:,1:end-1)];
figure(1);
Ncep=size(c,2)-1;
a=real(fft([c,zeros(size(c,1),512-Ncep*2-1),c(:,end:-1:2)]'));
imagesc(a(1:end/2,:));
axis('xy');
colormap(jet);
c=t11;
figure(2);
Ncep=size(c,2)-1;
a=real(fft([c,zeros(size(c,1),512-Ncep*2-1),c(:,end:-1:2)]'));
imagesc(a(1:end/2,:));
axis('xy');
colormap(jet);
c=a11;
figure(3);
Ncep=size(c,2)-1;
a=real(fft([c,zeros(size(c,1),512-Ncep*2-1),c(:,end:-1:2)]'));
imagesc(a(1:end/2,:));
axis('xy');
colormap(jet);
Obviously your example has externalities so I can't reproduce it directly, but in
general here is an octave example and its equivalent python one using the image features you require.
in Octave
% Read an image from a url
Url = 'https://raw.githubusercontent.com/utkuozbulak/singular-value-decomposition-on-images/master/data/grayscale_cat.jpg';
A = imread( Url );
imagesc( A ); % Show image in 'colour-scaled' form
axis xy % Reverse the origin of the y-axis
colormap( jet ); % Choose the jet colormap
in Python3
import urllib.request # needed for reading urls
import matplotlib.pyplot as plt # needed for imread/imshow
import matplotlib.colors as cl # needed for colour-scaling
# Read an image from a url
Url = urllib.request.urlopen( 'https://raw.githubusercontent.com/utkuozbulak/singular-value-decomposition-on-images/master/data/grayscale_cat.jpg' )
A = plt.imread( Url, 'jpg' )
plt.imshow( A, # Create a pyplot 'image' instance
norm = cl.Normalize(), # Choose colour-scaled form
origin = 'lower', # Reverse the origin of the y-axis
cmap = 'jet' # Choose the jet colormap
)
plt.show()
Related
I have a 360x360 image I want to remove lines in it
the portion on it has periodic noisy lines I am working on MATLAB
I tried median filter, but not working how to denoise this image and remove lines?
I tried this
%image 360x360
[rows, columns, numberOfColorChannels] = size(image);
subplot(2, 2, 1);
imshow(image,[]);
horizontalProfile = mean(image);
subplot(2, 2, [2, 4]);
plot(horizontalProfile, 'b-');
grid on;
bottomEnvelope = movmin(horizontalProfile, 10);
upperEnvelope = movmax(horizontalProfile, 10);
deltaGL = mean(upperEnvelope- bottomEnvelope)
hold on;
plot(bottomEnvelope, 'r-', 'LineWidth', 2);
plot(upperEnvelope, 'r-', 'LineWidth', 2);
% Compute midline
midline = (bottomEnvelope + upperEnvelope) / 2;
plot(midline, 'm-', 'LineWidth', 2);
columnsToDim = horizontalProfile > midline;
image(:, columnsToDim) = image(:, columnsToDim) - deltaGL;
subplot(2, 2, 3);
imshow(image, []);
But that did not work better
I've uploaded the image data to Google Drive
This is a perfect use case for the Fast Fourier Transform (FFT).
FFT converts an image in the spatial domain to its frequency domain. The frequency domain can be used to smoothen particular noises (vertical lines in your case) in the spatial domain by removing the corresponding high frequency signals. There are tons of sources you can inform yourself about it, so I leave this part to you.
Here is my approach.*
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('coin.png',0)
# get the frequency domain
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))
# smoothen the vertical lines in the spatial domain =
# remove the high frequency signals (i.e. horizontal lines) in the frequency domain
rows, cols = img.shape
crow,ccol = rows//2 , cols//2
fshift[crow-5:crow+6, 0:ccol-10] = 0
fshift[crow-5:crow+6, ccol+11:] = 0
magnitude_spectrum_no_vertical = 20*np.log(np.abs(fshift))
# get the spatial domain back
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.real(img_back)
This is the output image only.
Feel free to play around with different approaches: Applying a gaussian filter before FFT to improve the outcome, masking background and so on.
*: Sorry I have no MATLAB. It should be easy to port my Python script to MATLAB, though.
The title says what I'd like.
The compare(zv, mtf) function generates a figure with two lines, a gray one, representing the real system data carried by zv variable, and another blue line representing the model's response to the same signal. How can I change the color of the blue line?
Compare's documentation gives the example of using 'r' for red, but I want to pass a specific RGB color. The syntax compare(zv, mtf,'color', [219/256 134/256 7/256]) gives the following error:
The string "color" is not a valid plot style.
I'm running R2015b (32bit). Unfortunately I wont be able to upgrade to a more recent version for some weeks still.
I found a way, but it is not very intuitive, and the legend color does not update with the update line color...
A better way is probably to obtain the response of the system and goodness of fit by calling
[y_sys,fit,~] = compare(z1, sys);
and plot these results yourself (see the docs).
% load sample data and system
load iddata1 z1;
sys = tfest(z1,3);
% give the tf system a name
tf_name = 'system';
sys.Name = tf_name;
% compare in figure
fig = figure(1); clf;
compare(z1,sys)
% get handle for line of system
children = get(gca, 'Children');
grp = findobj(children, '-regexp', 'DisplayName', tf_name); % use system.Name here!
l = grp.Children; % handle to line object
% get old color
color_old = l.Color; % same as output of 'lines(1)' when using default colors
% find all lines with color
ls = findobj(fig, 'Color', color_old);
% set new color
set(ls, 'Color', [219/256 134/256 7/256])
I'm trying to digitize this image using MATLAB:
I have the following script:
%// Get data from plot
clear all; close all;
%// Input
fname = 'Fig15a.PNG';
xvec = [1e3:1:1e8];
yvec = [1e-4:1:1e-1];
xt = [1e3 1e4 1e5 1e6 1e7 1e8];
yt = [1e-4 1e-3 1e-2 1e-1];
%// Read and plot the image
im = imread(fname);
figure(1), clf
im = im(end:-1:1,:,:);
image(xvec,yvec,im)
axis xy;
grid on;
%// Set ticks
set(gca,'xtick',xt,'ytick',yt); %// Match tick marks
%// Collect data
[x,y] = ginput; %// Click on points, and then hit ENTER to finish
%// Plot collected data
hold on; plot(x,y,'r-o'); hold off;
%// Then save data as:
save Fig15a.mat x y
The script works fine
Is there a way I can change the x and y axes to a log scale ?
I have tried adding the following code in different places without luck:
%// Set Log scale on x and y axes
set(gca,'XScale','log','YScale','log');
Below's a proof of concept that should get you on the right track. I have replaced things in your original code with what I consider "good practices".
function q36470836
%% // Definitions:
FIG_NUM = 36470836;
%% // Inputs:
fname = 'http://i.stack.imgur.com/2as4t.png';
xt = logspace(3,8,6);
yt = logspace(-4,-1,4);
%% // Init
figure(FIG_NUM); clf
% Read and plot the image
im = imread(fname);
hIMG = imshow(im); axis image;
%// Set ticks
hDigitizer = axes('Color','none',...
'XLim',[xt(1) xt(end)],'YLim',[yt(1) yt(end)],...
'XScale','log','YScale','log',...
'Position',hIMG.Parent.Position .* [1 1 696/785 (609-64+1)/609]);
uistack(hDigitizer,'top'); %// May be required in some cases
grid on; hold on; grid minor;
%// Collect data:
[x,y] = ginput; %// Click on points, and then hit ENTER to finish
%// Plot collected data:
scatter(x,y,'o','MarkerEdgeColor','r');
%// Save data:
save Fig15a.mat x y
Here's an example of what it looks like:
Few notes:
xt, yt may be created in a cleaner fashion using logspace.
It is difficult (possibly impossible) to align the digitization grid with the image correctly, which would inevitably result in errors in your data. Though this can be helped in the following scenarios (for which you will require a vector graphics editor, such as the freeware InkScape):
If, by any chance, you got this image from a PDF file, where it appears as a vector image (you can test this by zooming in as much as you like without the chart becoming pixelated; this seems to be your case from the way the .png looks), you would be better off saving it as a vector image and then you have two options:
Exporting the image to a bitmap with a greatly increased resolution and then attempting the digitization procedure again.
Saving the vector image as .svg then opening the file using your favorite text editor and getting the exact coordinates of the points.
If the source image is a bitmap (as opposed to vector graphic), you can "trace the bitmap", thus converting it to vectoric, then #GOTO step 1.
This solution doesn't (currently) support resizing of the figure.
The magic numbers appearing in the Position setting are scaling factors explained in the image below (and also size(im) is [609 785 3]). These can technically be found using "primitive image processing" but in this case I just hard-coded them explicitly.
You can plot in double logarithmic scale with
loglog(x,y);
help loglog or the documentation give additional information.
For a single logarithmic scale use
semilogx(x,y);
semilogy(x,y);
How to obtain the following surface via Matplotlib?
It is easy in matlab via:
mesh(peaks)
It seems matplotlib does not have an exact counterpart of mesh in matlab.
the Wireframe plots does not have any colormap option
While answering another question I found that you can easily do this using plot_surface to produce a color mapped surface, and then exchanging face and edge colors:
surf = ax.plot_surface(X, Y, Z, rstride=2, cstride=2, shade=False, cmap="jet", linewidth=1)
draw()
surf.set_edgecolors(surf.to_rgba(surf._A))
surf.set_facecolors("white")
show()
produces
The disadvantage this solution has over the other one is that the edges do not have smooth, per-pixel colouring, but one single color each.
It seems to be possible with matplotlib even if it is a bit of a hack:
from mpl_toolkits.mplot3d import axes3d
from mpl_toolkits.mplot3d import art3d
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
wire = ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
# Retrive data from internal storage of plot_wireframe, then delete it
nx, ny, _ = np.shape(wire._segments3d)
wire_x = np.array(wire._segments3d)[:, :, 0].ravel()
wire_y = np.array(wire._segments3d)[:, :, 1].ravel()
wire_z = np.array(wire._segments3d)[:, :, 2].ravel()
wire.remove()
# create data for a LineCollection
wire_x1 = np.vstack([wire_x, np.roll(wire_x, 1)])
wire_y1 = np.vstack([wire_y, np.roll(wire_y, 1)])
wire_z1 = np.vstack([wire_z, np.roll(wire_z, 1)])
to_delete = np.arange(0, nx*ny, ny)
wire_x1 = np.delete(wire_x1, to_delete, axis=1)
wire_y1 = np.delete(wire_y1, to_delete, axis=1)
wire_z1 = np.delete(wire_z1, to_delete, axis=1)
scalars = np.delete(wire_z, to_delete)
segs = [list(zip(xl, yl, zl)) for xl, yl, zl in \
zip(wire_x1.T, wire_y1.T, wire_z1.T)]
# Plots the wireframe by a a line3DCollection
my_wire = art3d.Line3DCollection(segs, cmap="hsv")
my_wire.set_array(scalars)
ax.add_collection(my_wire)
plt.colorbar(my_wire)
plt.show()
An official feature request is underway:
https://github.com/matplotlib/matplotlib/issues/3562
The accepted solution doesn't work when X and Y arrays are not the same size.
It seems the current matplotlib 1.3.1 does not handle such mesh plot or further PDF export. gnuplot.pygnuplot.py 1.8 might be a choice before there is further updates in matplotlib.
Here is an example created via gnuplot:
MayaVI2 does not support PDF exports but might be another good choice.
I'm trying to plot three time series on the same axes using matplotlib. Each time series has 10^6 data points. While I have no problem generating the figure, the PDF output is large and very slow to open in viewers. Aside from working in a rasterized format, or only plotting a subset of the time series, is there a way to get better graphical performance? I have tried "optimizing" in acrobat, and I have also had the same trouble with matlab.
The code is as follows:
import numpy as np
import matplotlib.pyplot as plt
data=np.loadtxt('data.txt')
idx = data[:,0]
y1 = data[:,1]
y2 = data[:,2]
y3 = data[:,3]
plt.rc('text', usetex=True)
plt.rc('font', size=16)
fig, ax = plt.subplots()
ax.plot(idx,y1,color='b',label=r'$y_1$',
linewidth=2.0,markersize=10,fillstyle='none')
ax.plot(idx,y2,color='g',label=r'$y_2$',
linewidth=2.0,markersize=10,fillstyle='none')
ax.plot(idx,y3,color='r',label=r'$y_3$',
linewidth=2.0,markersize=10,fillstyle='none')
plt.xlabel(r'Index')
plt.ylabel(r'Vales')
legend = ax.legend(loc='upper right',fontsize=16)
ax.set_xscale('log')
plt.savefig('fig1.pdf',bbox_inches='tight')
plt.show()