I have this loglog plot that I would like to clean up on y-axis which, you see below, is a bit of a mess.
I would like the plot to look like this:
More specifically I want to remove the ticks that are visible between the values (0, 10e-2, 10e-4, 10e-6, 10e-8, 10e-10). How to achieve this?
You can turn the minor ticks off:
y = logspace(1,-8,5);
x = logspace(0.5,2,5);
loglog(x,y)
grid on
ax = gca;
ax.YAxis.MinorTick = 'off'; % and the same for the X-axis
ax.FontSize = 16;
Related
I'm plotting some polar coordinates in Matlab. See example below:
I'm only plotting a slice but can't figure out how to stretch/ expand this slice, as there is more space in the figure window than used. Speaking in pictures I'd like to 'open it like a fan'.
Minimal working example:
th = linspace(-pi/2,pi/2,100);
polarplot(th,1.1+zeros(size(th)),'g');
ax = gca;
thetalim([-5 5])
rlim([1.05 1.12])
I've tried using ax.Position = [0 0 1 1]; similar to how it works in Cartesian plots but this does not seem to work here.
Any commands or tips I'm missing so far?
I can only think of a workaround solution: increase the theta values passed to polarplot to get larger angles in the graph, and then relabel the theta axis:
stretch = 3; % stretch factor
th = linspace(-pi/2,pi/2,100);
polarplot(th*stretch,1.1+zeros(size(th)),'g'); % apply strech to theta
ax = gca;
thetalim([-5 5]*stretch) % take stretch into account when setting theta axis limit
rlim([1.05 1.12])
ax = gca;
ax.ThetaTickLabel = strsplit(num2str((ax.ThetaTick/stretch))); % correct theta labels
In the last line I am keeping the default theta ticks and relabelling them. You may prefer to specify other values manually, such as
ax.ThetaTick = stretch*(-5:5); % specify theta ticks, including stretch
ax.ThetaTickLabel = strsplit(num2str((ax.ThetaTick/stretch))); % correct theta labels
Question
When using polarhistogram(theta) to plot a dataset containing azimuths from 0-360 degrees. Is it possible to specify colours for given segments?
Example
In the plot bellow for example would it be possible to specify that all bars between 0 and 90 degrees (and thus 180-270 degrees also) are red? whilst the rest remains blue?
Reference material
I think if it exists it will be within here somewhere but I am unable to figure out which part exactly:
https://www.mathworks.com/help/matlab/ref/polaraxes-properties.html
If you use rose, you can extract the edges of the histogram and plot each bar one by one. It's a bit of a hack but it works, looks pretty and does not require Matlab 2016b.
theta = atan2(rand(1e3,1)-0.5,2*(rand(1e3,1)-0.5));
n = 25;
colours = hsv(n);
figure;
rose(theta,n); cla; % Use this to initialise polar axes
[theta,rho] = rose(theta,n); % Get the histogram edges
theta(end+1) = theta(1); % Wrap around for easy interation
rho(end+1) = rho(1);
hold on;
for j = 1:floor(length(theta)/4)
k = #(j) 4*(j-1)+1; % Change of iterator
h = polar(theta(k(j):k(j)+3),rho(k(j):k(j)+3));
set(h,'color',colours(j,:)); % Set the color
[x,y] = pol2cart(theta(k(j):k(j)+3),rho(k(j):k(j)+3));
h = patch(x,y,'');
set(h,'FaceColor',colours(j,:),'FaceAlpha',0.2);
uistack(h,'down');
end
grid on; axis equal;
title('Coloured polar histogram')
Result
I need to make a plot with a discrete colorbar in Matlab. I do this in the following way:
data = randi(10, 20);
imagesc(data)
my_colormap = rand(10, 3);
colormap(my_colormap)
cb = colorbar
set(cb,'YTickLabel',{'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';})
Now my problem is that the colorbar tick labels and the small lines in the colorbar don't align nicely. How can I even the colorbar tick labels and the small lines better as illustrated in the following pic:
The TickLabel on the colorbar each correspond to a value (a Tick). To place the TickLabels in the middle, you need to place the tick in the middle. To make this dynamic (so that It does not change when resizing the image) was I bit tricky I recall and I do not really recall. To set the ticks just once is not so hard though,
set(hCbar,'YTicks',RightYTicks);
EDIT:
On request I will post an example. This should give a hint of what to do.
x = 1:10;
y = 1:10;
cmap = jet(10);
[x, y] = meshgrid(x,y); %x and y grid
c = x-0.1; %Set color code to increase to the right
hFig = figure;
scatter(x(:),y(:),10,c(:),'filled'); % Simpler for the example
set(gca(hFig),'CLim',[0,10]);
colormap(cmap);
hCbar = colorbar;
set(hCbar,'YTicks',0.5:9.5);
set(hCbar,'YTickLabels',{'A','B','C','D','E','F','G','H','I','J'});
For newer matlab version, the YTicks may have changed name to Ticks And YTickLabels may be called TickLabels.
I would like to plot two graphs with the same x-axis but with different y-axies, one stacked above the other. There is a similar question asked below BUT it doesn't account for changes of dimensions in the y-axes. I have edited this code to do so but hope there would be a more elegant approach (maybe using a "hold on" type code).
How to plot graphs above each other in Matlab?
Here is some code which might do what you want...if I understood right. Basically you create 2 different axes in the same figure, one on top of the other, and you play around with XTick and YTick. You can start from this I guess.:
clear
close all
clc
x = 1:10;
y1 = -(x.^2);
y2 = sin(x);
figure('Units','Normalized');
hAxes1 = axes('Position',[0.1 0.1 .8 .4]);
yLim = get(hAxes1,'YLim');
Axes1Position = get(hAxes1,'Position');
NewAxesPosition = [Axes1Position(1) Axes1Position(2)+0.4 Axes1Position(3) Axes1Position(4)];
hAxes2 = axes('Position',NewAxesPosition);
plot(x,y1,'b','Parent',hAxes1);
TICK = get(hAxes1,'YTick')
set(hAxes1,'XTick',2:1:10,'YTick',TICK(1:end-1))
hold on
plot(x,y2,'r','parent',hAxes2)
set(gca,'XTick',[],'XTickLabel',[])
hold off
Giving this:
This is not optimal but due to lack of time I have to stop here :) Of course you can alter the display of the axis or the tick marks as you wish. Moreover you could use text annotations to customize the YTicks even more nicely.Hope that helps!
Example:
A = 1000;
a = 0.005;
b = 0.005;
t = 0:900;
z1 = A*exp(-a*t);
z2 = sin(b*t);
[ax,p1,p2] = plotyy(t,z1,t,z2,'semilogy','plot');
ylabel(ax(1),'Semilog Plot') % label left y-axis
ylabel(ax(2),'Linear Plot') % label right y-axis
xlabel(ax(2),'Time') % label x-axis
With reference to: http://www.mathworks.com/help/matlab/creating_plots/plotting-with-two-y-axes.html
I'm trying to position MATLAB's ticks to line up with my grid, but I can't find a good way to offset the labels.
Also, if I run set(gca,'XTickLabel',1:10), my x tick labels end up ranging from 1 to 5. What gives?
You need to move the ticks, but get the labels before and write them back after moving:
f = figure(1)
X = randi(10,10,10);
surf(X)
view(0,90)
ax = gca;
XTick = get(ax, 'XTick')
XTickLabel = get(ax, 'XTickLabel')
set(ax,'XTick',XTick+0.5)
set(ax,'XTickLabel',XTickLabel)
YTick = get(ax, 'YTick')
YTickLabel = get(ax, 'YTickLabel')
set(ax,'YTick',YTick+0.5)
set(ax,'YTickLabel',YTickLabel)
Or if you know everything before, do it manually from the beginning:
[N,M] = size(X)
set(ax,'XTick',0.5+1:N)
set(ax,'XTickLabel',1:N)
set(ax,'YTick',0.5+1:M)
set(ax,'YTickLabel',1:M)
The marked answer works with a surf or mesh plot, however, I needed a solution which worked for a 2d plot.
This can be done by creating two axes, one to display the grid and the other to display the labels as follows
xlabels=1:1:10; %define where we want to see the labels
xgrid=0.5:1:10.5; %define where we want to see the grid
plot(xlabels,xlabels.^2); %plot a parabola as an example
set(gca,'xlim',[min(xgrid) max(xgrid)]); %set axis limits so we can see all the grid lines
set(gca,'XTickLabel',xlabels); %print the labels on this axis
axis2=copyobj(gca,gcf); %make an identical copy of the current axis and add it to the current figure
set(axis2,'Color','none'); %make the new axis transparent so we can see the plot
set(axis2,'xtick',xgrid,'XTickLabel',''); %set the tick marks to the grid, turning off labels
grid(axis2,'on'); %turn on the grid
This script displays the following figure :