I want to make a simple boxplot of predictions (from 0 to 0.89), but don't see why there are no values for y-axis.
This is my commando used:
boxplot(dataset_validatie$predictions)
This is my result:
enter image description here
You can try to add something like this to your code, setting limits to your x, y axis:
fig, ax = plt.subplots()
ax.set_title('My plot')
ax.set_xlim(-10, 100)
top = 40
bottom = -5
ax.set_ylim(bottom, top)
ax.boxplot(data)
plt.show()
For some demos, please find the official site here
Related
I have some shelf sea data and need to plot values equal to NaN in one colour (for the land), values less than 2.7 in another colour, and values above 2.7 in a third colour. I've tried creating a custom colourbar with three colours (blue for 0, aqua for 0.001 to 2.7 and yellow for everything else), however, it plots a colour bar that has 3 equal sections (blue for 0 - 2, aqua for 2 - 4 and yellow for 4 - 6). I'm very new to Matlab and unsure what I am doing wrong.
Below is my code. The first part (included for clarity) works fine.
to_plot = hu_atl;
mask = zeros(size(to_plot));
mask(hz_atl>200) = NaN; %open ocean
mask(hz_atl<=200) = 1; %shelf
mask(mask==1) = to_plot(mask==1); %(or pick other suitable value)
log = log10(mask);
pcolor(log10(mask')); %plot h/u3 on a log scale
latlim = [-90 90]; %set the latitude limits
lonlim = [100 180]; %set the longitude limits
shading interp; %hide the grid lines, without this, the map just looks black due to all the grid lines
c = colorbar;
c.Label.String = 'log 10(h/u^3)'; %This labels the colourbar
caxis([0 6]); %This sets the upper and lower limits of the colourbar
Below is the colourbar code, which plots the figure below.
crange = [ 0 0.0001;0.0001 2.7;2.7 inf] ;
cmap = jet(size(crange,1)) ;
%%Arrange the colors range
colors = zeros(size(log));
for i = 1:size(crange,1)
colors(log > crange(i,1) & log<=crange(i,2)) = crange(i,2);
end
colormap(cmap)
title('Global tidal mixing front positions based on TPXO9 tidal data')
xlabel('Longitude')
ylabel('Latitude')
set(gca,'XTick',[]) %hides tick numbers on x axis
set(gca,'YTick',[]) %hides tick numbers on y axis
Is there a way to change the ranges on the colourbar to match my requirements above?
Thanks in advance for any assistance!
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;
I am trying to create area plots with 2 y-axis using plotyy by using 'area' function. I don't want to have any tick marks or labels or titles but I just want the outside box. I would also like to save just the plot (not the entire figure window) as a png file.
When I turn off the x and y-axis ticks and labels, the box looks thin on the bottom and left, thick on the top and right. What am I doing wrong?
Also, if I try to make the box 'LineWidth' fat, I see two tick marks at the bottom left and bottom right - which ruin the plot and I am unable to remove. Can someone help?
My test code is below:
x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));
fig_handle = figure('units','inches','position',[1 1 9 3]);
[a,p1,p2] = plotyy(x,y1,x,y2,'area');
c1 = get(p1,'child');
c2 = get(p2,'child');
set(c1,'facea',0.5,'FaceColor','b','EdgeColor',[0 0 0]);
set(c2,'facea',0.5,'FaceColor','r','EdgeColor',[0 0 0]);
set(c1,'Line','None');
set(c2,'Line','None');
set(a,'Layer','top')
set(a,'XTick',[]);
set(a(1),'YTick',[]);
set(a(2),'YTick',[]);
set(a,'TickDir','in')
set(a,'LineWidth',5);
Also, notice how the left Y-axis has red area on it, while the right Y-axis doesn't. Is this fix-able?
Any help would be appreciated! Also, I am new to StackOverflow - so, if these are too many questions in one post, please pardon me and I will post them as separate requests/questions.
Here is a workaround for the red appearing on the left Y-axis.
Since the axes line is quite thick, the data displayed close to it is drawn over it. To avoid this, you can slightly shift the x limits of the axes to make more room for the data. Do so by changing the XLim property of either axes since they share the same x limit:
XL = get(a,'Xlim');
xl = XL{1}; %// here XL{1} and XL{2} are the same...[1 100]
set(a(:),'Xlim',[xl(1)-.5 xl(2)+.5])
As for the annoying tick marks at the bottom of the plot, I must say that I don't know how to remove them while keeping the axes visible.
As an alternative solution to plotyy, here is a way to obtain a good result (I think) without plotyy. The trick is to superimpose 2 axes and make both of them not visible, then set the figure's color to white and add a black rectangle surrounding the plot.
Here is the code:
clear
clc
close all
x = 1:100;
y1 = rand(size(x));
y2 = 100*rand(size(x));
H1 = area(x,y1);
%// Set the figure color to white
set(gcf,'Color','w')
%// Plot data and set different properties for each axes
A1 = gca;
A2 = axes('Position',get(A1,'Position'));
H2 = area(x,y2);
set(A2,'YAxisLocation','right','Color','none','XTickLabel',[]);
set(A2,'XLim',get(A1,'XLim'),'XTick',[],'YTick',[]);
set(A1,'XTick',[],'YTick',[]);
%// Make both axes not visible.
set(A2,'Visible','off')
set(A1,'Visible','off')
axes(A1)
%// Get axes limits
XL = get(gca,'XLim');
YL= get(gca,'YLim');
%// Create rectangle as a bounding box
rectangle('Position',[XL(1) YL(1) XL(2)-1 YL(2)],'LineWidth',5)
%//===
%// Change the data color/properties
hP = findobj('Type','patch');
set(hP(1),'FaceColor','b','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')
set(hP(2),'FaceColor','r','FaceAlpha',.5,'EdgeColor',[0 0 0],'line','none')
And the output:
It's not perfect but hope that helps!
I have plotted three sub-images on a plot with Matlab as Figure 1 shows, you can see their titles and images are not aligned in a line. I want to plot a figure as Figure 2 or 3 where at least the titles are aligned. How can I do that? thank you.
Figure 1
Figure 2
Figure 3
The sort answer is that there is no way (that I know of) to align subplots. You could however manually give the exact positions and size of each image so that means you can compute the aligned locations manually.
It will be something along these lines:
y = .8; % vertical location stays fixed
subplot('Position', [.1 y width1 height1]);
imagesc(img1);
title('1');
[s1 s2] = size(img2);
subplot('Position', [.4 y width2 height2]);
imagesc(img2);
title('2');
[s1 s2] = size(img3);
subplot('Position', [.7 y width3 height3]);
imagesc(img3);
title('3');
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 :