Remove Exponents from Matlab LOG-LOG plot - matlab

I am trying to plot several frequency response curves on a logx and logy plot. I also want the axes to show values without exponents.
I cannot figure out a way to plot the log scale AND remove exponents. Using the following, I can only either set my scale to log, OR remove exponents. Not both.
figure(3);clf;
hold on;
%set(gca,'YScale', 'log', 'XScale', 'log')); %This doesnt work with ax.XAxis.Exponent = 0;
plot(freq, Pxx,'r', 'LineWidth', 1.5, 'DisplayName', 'Single-Sided Spectral Density');
ax=gca;
ax.XAxis.Exponent = 0; %remove exponents
ax.YScale = 'log'; %Log Y
ax.XScale = 'log'; %Log X
xlabel('Frequency [Hz]');ylabel('[WU^2 / Hz]');
title('Average Single-Sided Spectral Density [G_x_x] vs. Frequency')
grid on;
xlim([0 max(freq)]);legend('FontSize',14);
hold off
Plot with log scale, AND Exponents in scale values
Plot without log scale, without exponents in scale values

Changing the tick labels can be done like this:
figure;loglog([12 123 1233],[12 144 1111]);
set(gca,'xticklabel',arrayfun(#(x) num2str(x),(get(gca,'xtick')),'un',0))
set(gca,'yticklabel',arrayfun(#(x) num2str(x),(get(gca,'ytick')),'un',0))

Related

How to format graph so that border starts at max x and y and how to replace a plot command

I am trying to format my graph so that the border ends at the max x and max y so there is not extra space between them and the border. Also, I'm trying to completely replace my first plot command with my second one. Should I just delete my first plot? Currently, the second plot goes over my first plot, removing most of my formatting.
clear all, close all
%%command to clear all variables and log history
x = linspace(-pi, pi, 200);
%creating x variable between - pi and 200 with pi distance between each
%value
y = sin(x) + cos(x);
%Creating y varable with the help of x
figure
plot(x,y)
title('2-D Plot')
xlabel('Theta (in radians)')
ylabel('Function Y')
xlim([-pi pi])
ylim([-1.414 1.414])
grid
plot(x,y,'r--')
grid
To fit the axes box tightly around the data without manually adjusting the axis limits, use:
axis tight;
and instead of re-plotting, you can update the relevant properties of the line.
x = linspace(-pi, pi, 200);
y = sin(x) + cos(x);
figure;
h = plot(x,y); %handle for the line plot
title('2-D Plot');
xlabel('Theta (in radians)');
ylabel('Function Y');
grid;
axis tight; %to set the axis limits equal to the range of the data
set(h, 'LineStyle', '--', 'Color', 'r'); %Updating the plot with required changes

Figure created from contour changes to be in raster format when the number of rows and columns larger than some threshold

A 2D data matrix (data, 100 rows by 100 cols) was plotted using 'contour' function in matlab, and the figure was saved as *.emf file. Expectedly when I insert the emf figure into MS word file, the figure is a vector graphic. But when I improve the resolution of the data, i.e. using imresize in matlab to scale the data matrix with factor 2, and did the same thing as before (plot, save as emf and insert it into word), the figure change to raster format. Is there any settings in matlab that can improve the threshold that keeps vector graphics? The code used is as follows:
path = 'C:\Users\Administrator\Desktop\';
data = importdata([path, 'lsa2.txt'], ' ', 6);
cdata = data.data;
% scale = 2;
% cdata = imresize(cdata, scale);
n = 25;
contourf(cdata,n, 'LineStyle', 'none');
colormap(jet);
axis equal;
First of all don't use imresize if you want a vector image. Imresize is by default raster based and can act weird. Also be more specific with what you want please. If you already have a vector file you can just change it to any size.
Also if you have only a limited data set, you can never really get a true higher resolution, you can only interpolate inbetween points. So use interp2 to resize your data matrix.
[sx,sy] = size(cdata);
fact=1/4; %factor to interpolate over so 4 points per 1
[X,Y]=meshgrid(1:sx); %you can only use this because cdate is square, same size for sx and sy
[xq,yq]=meshgrid(1:fact:sx); %create a new mesh with more points
Cnew=interp2(X,Y,cdata,xq,yq,'spline'); %I suggest spline, probably does what you want
This is the entire code to generate the plot, make sure to save it as an .emf, .svg or .eps if you want a vector image
data = importdata('lsa2.txt', ' ', 6);
cdata = data.data;
rws=3
cls=2;
xl=[22 33]; yl=[13 23];
n = 25;
subplot(rws,cls,1)
contourf(cdata,n, 'LineStyle', 'none');
title('Subplot 1: Original no scaling')
colormap(jet);
axis equal;
xlim(xl);
ylim(yl);
scale = 4;
cresi = imresize(cdata, scale);
subplot(rws,cls,2)
contourf(cresi,n, 'LineStyle', 'none');
title('Subplot 2: Imresize function, scale 4')
colormap(jet);
axis equal;
xlim(scale*xl);
ylim(scale*yl);
[sx,sy] = size(cdata);
fact=1/4; %factor to interpolate over so 4 points per 1
[X,Y]=meshgrid(1:sx); %you can only use this because cdate is square, same size for sx and sy
[xq,yq]=meshgrid(1:fact:sx); %create a new mesh with more points
Cnew=interp2(X,Y,cdata,xq,yq);
subplot(rws,cls,3)
contourf(Cnew,n, 'LineStyle', 'none');
title('Subplot 3: Interp2 "linear", scale 4')
colormap(jet);
axis equal;
xlim(xl/fact);
ylim(yl/fact);
[sx,sy] = size(cdata);
fact=1/4; %factor to interpolate over so 4 points per 1
[X,Y]=meshgrid(1:sx);
[xq,yq]=meshgrid(1:fact:sx);
Cnew=interp2(X,Y,cdata,xq,yq,'spline');
subplot(rws,cls,4)
contourf(Cnew,n, 'LineStyle', 'none');
title('Subplot 4: Interp2 "spline", scale 4')
colormap(jet);
axis equal;
xlim(xl/fact);
ylim(yl/fact);
Cnew=interp2(X,Y,cdata,xq,yq,'cubic');
subplot(rws,cls,5)
contourf(Cnew,n, 'LineStyle', 'none');
title('Subplot 5: Interp2 "cubic", scale 4')
colormap(jet);
axis equal;
xlim(xl/fact);
ylim(yl/fact);
Cnew=interp2(X,Y,cdata,xq,yq,'nearest');
subplot(rws,cls,6)
contourf(Cnew,n, 'LineStyle', 'none');
title('Subplot 6: Interp2 "nearest", scale 4')
colormap(jet);
axis equal;
xlim(xl/fact);
ylim(yl/fact);

How to plot a histogram as a scatter plot in matlab?

The y axis should be frequency (which is the histogram part) while the x axis is the xdata used to plot the histogram. Essentially what I need is exactly a histogram but instead of bars, there should be points.
I'd do that using the histcounts command. It's like the histogram command, but instead of plotting the data, it returns the number in each bin and the bin edges. You can then plot that as points using the general plot command.
x = randn(1,1000); %Generate some data to plot
figure(1); clf;
subplot(2,1,1); hold on;
h = histogram(x,'normalization','probability');
title('Plotted as a histogram');
ylabel('Frequency');
subplot(2,1,2); hold on;
[N, edges] = histcounts(x,'normalization','probability');
centers = (edges(1:end-1) + edges(2:end))./2; %histcounts gives the bin edges, but we want to plot the bin centers
plot(centers,N,'ko');
title('Plotted as points');
ylabel('Frequency');

splitting the y axis into a linear and logarithmic scale matlab

I would like to split the y axis into a linear and logarithmic scale section while plotting the figure. For example, from 1-30 a linear scale and from 30 - 100 a logarithmic scale. Is there any way of doing that? Thank you
I don't know if there is a direct method for this. However, you can join both regions (linear and log) manually. See attached code:
clc; clear;
x = 1:100; % Values to plot
xorg = 0:10:100; % Ticks on the Y-axis
xticks = xorg; % Final tick location
x1 = log10(30); % start of logarithmic scale
x2 = log10(100); % end of logarithmic scale
dx = (x2-x1)*60; % 60 here is an arbitrary scaling factor
scale = #(x)(log10(x)-x1)/(x2-x1)*dx+30; % Scaling from lin to log
x(x>30) = scale(x(x>30)); % Apply scaling to plotting values
xticks(xticks>30) = scale(xticks(xticks>30)); % Apply scaling to Y ticks
plot(x);
ylim([0 max(xticks)]);
set(gca,'YTick',xticks,'YTickLabel',xorg); % Update the ticks
This produces a plot as below.

Hist3 Plotting and Axes Range

I'm plotting a 3-D histogram with MATLAB and it's working pretty ok, except for the different axes ranges. I'd like them to be defined in a way, where equal value pairs lie on the bisecting line.
My code looks like this (more or less "stolen" from the hist3 MATLAB example):
[vec_voxel_ids, vec_dose_values_reference, vec_dose_values_control] = ...
textread('_BOOSTINT_voxel_data.txt', '%u %u %u');
mat_dose_values = [vec_dose_values_reference, vec_dose_values_control];
hist3(mat_dose_values, [100, 100]);
xlabel('Dose Reference');
ylabel('Dose Control');
set(gcf, 'renderer', 'opengl');
set(get(gca,'child'), 'FaceColor', 'interp', 'CDataMode', 'auto');
This is how it looks:
In order to reposition the bins to align their centers the ticks and also choose the range of the bin values you can use hist3's 'edges' option (similar to that in histc):
data = 500+5*randn(1e3,2); % dummy data
d = 1; % width for x and y bins
x = 480:d:520; % range for x bins
y = x; % use same range for y bins, can be different
hist3(data, 'edges', {x-d/2,y-d/2}); % subtract half bin width to center bins
set(gcf, 'renderer', 'opengl');
set(get(gca,'child'), 'FaceColor', 'interp', 'CDataMode', 'auto');
view(2)
axis equal
axis([478 522 478 522])
grid minor
xlabel('x')
ylabel('y')
This example produces something like this:
Note, that this is a re-binning of your data so your output may look slightly different compared to before. The bins have been shifted to align their centers and the histogram recalculated.