Matlab does not display enough digits on plot - matlab

I would like to plot a graph in MATLAB but it displays x-axis as x10^6 !
How can I make it display it 10 000 000 ? See image below.
frequency=[9999445,9999475,9999500,9999517,9999543,9999562,9999580,9999604,9999626,9999647,9999668,9999688,9999705,9999730,9999755,9999780,9999800,9999830,9999847,9999862,9999883,9999900,9999920,9999930,9999950,9999985,9999994,10000000,10000010,10000018,10000026,10000032,10000039,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045,10000045];
temperature=[283,293,299,303,306,309,312,315,318,320,323,326,328,330,333,336,338,342,343,345,348,350,352,353,353,357,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354,354];
time=[1:10:540]
%plot(frequency)
%figure,plot(temperature)
figure,plot(frequency,temperature);

You can use set function on the XTickLabel property of the current axis.
An example is this:
x=[999 1000 1001 1002 1003];
y=3*x;
plot(x,y);
set(gca, 'XTick', x, 'XTickLabel', sprintf('%4.0f|', x));
in your case you can do this:
x=linspace(min(frequency),max(frequency),5);
figure,plot(frequency,temperature);
set(gca, 'XTick', x, 'XTickLabel', sprintf('%7.0f|', x));
you can change the 5 value to have more or less ticks.

Related

Plot date labels in x-axis [MATLAB]

I have a plot with dates in form of mm/dd as x-axis data. Now I have
xData = ["01/22" "01/23" "01/24" "01/25" "01/26" "01/27" "01/28" "01/29"]
which is a string array of size 1 x 8.
How do I plot with
yData = [557 655 941 1433 2118 2927 5578 6167]
by using something like
plot(xData, yData)
with ["01/22" "01/23" "01/24" "01/25" "01/26" "01/27" "01/28" "01/29"] as the x-axis tick labels?
Currently I got the error messages Error using plot. Not enough input arguments. when running plot(xData, yData) as above, which I don't know what it exactly means.
I can think of two possibilities:
Convert the xData into a datetime array and use it in the plot
plot(datetime(xData, 'InputFormat', 'MM/dd', 'Format', 'MM/dd'), yData)
Use xData as the label of the x axis:
plot(yData)
set(gca, 'XTick', 1:length(yData), 'XTickLabel', xData)
plot(datetime(xData, 'InputFormat', 'MM/dd', 'Format', 'MM/dd'), yData)

MATLAB don't show x tick labels on subplots

I'm plotting 4 subplots, and for some reason, the x axis tick labels of the top 2 subplots don't appear on the figure (The x axis labels should be hours from datetime).
My code is:
figure
subplot(2,4,1)
plot(dateval,Height,'b','LineWidth',1)
datetick('x','HH:MM') %change the axis to time format
xlim([736886.619552373 736886.692032847]) %Limits
set(gca, 'FontName', 'Bookman','FontSize',7);
xlabel('Time','FontSize',7,'FontWeight','normal','Color','k','FontName','Bookman')
ylabel('GPS Height $[m]$','FontSize',7,'FontWeight','normal','Color','k','FontName','Bookman','Interpreter','latex')
subplot(2,4,2)
plot(dateval,Pressure,'b','LineWidth',1)
datetick('x','HH:MM') %change the axis to time format
xlim([736886.619552373 736886.692032847]) %Limits
xlabel('Time','FontSize',7,'FontWeight','normal','Color','k','FontName','Bookman')
ylabel('Sensor Pressure $[mb]$','FontSize',7,'FontWeight','normal','Color','k','FontName','Bookman','Interpreter','latex')
set(gca, 'FontName', 'Bookman','FontSize',7);
subplot(2,4,[5 6])
scatter(Pressure,Height,3,'b','*'); %Number is the size of dots
h=lsline;
set(h,'color','r','LineWidth',2);
R=corrcoef([Pressure,Height]);
Fit = polyfit(Pressure,Height,1);
ylabel('GPS Height $$[m]$$','FontSize',7,'FontWeight','normal','Color','k','FontName','Bookman','Interpreter','latex');
xlabel('Pressure $[mb]$','FontSize',7,'FontWeight','normal','Color','k','FontName','Bookman','Interpreter','latex');
xlim([1003 1015])
ylim([0 90])
set(gca, 'FontName', 'Bookman','FontSize',7);
box on
subplot(2,4,[3,4,7,8])
image( imread('TripMap.jpg') );
set(gca,'xtick',[])
set(gca,'xticklabel',[])
set(gca,'ytick',[])
set(gca,'yticklabel',[])
The figure:
What am I missing?
Thanks!!
Try to change the order of these two lines:
datetick('x','HH:MM') %change the axis to time format
xlim([736886.619552373 736886.692032847]) %Limits
To:
xlim([736886.619552373 736886.692032847]) %Limits
datetick('x','HH:MM') %change the axis to time format

Matlab how to add values in the x-axis of a plot

Plot using `set(gca, 'XTick', [1 10 20 50 100])
Hi everyone!
I have created a graph with the function scatter and in the x-axis there are only three values shown: [1 10 100].
I'd like to add some values, specifically I'd like to show [1 5 10 20 50 100].
How can i do this?
My code is:
line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca,'XScale','log')
set(gca,'XTickLabel',num2str(get(gca,'XTick').'))
set(gca,'XTick',[1 10 20 50 100])
set(gca,'YScale','log')
set(gca,'YTickLabel',num2str(get(gca,'YTick').'))
grid on
You want to set your XTick values before you set your XTickLabels since you are constructing your XTickLabels from the values of the XTicks themselves.
What is currently happening is that you have 5 XTick values and only 3 labels. Because of this, MATLAB will repeat the labels that you have to populate labels for all XTick locations.
line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca,'XScale','log')
set(gca,'XTick',[1 10 20 50 100])
set(gca,'XTickLabel',num2str(get(gca,'XTick').'))
set(gca,'YScale','log')
set(gca,'YTickLabel',num2str(get(gca,'YTick').'))
grid on
Better yet, there is no real reason for you to be setting XTickLabel manually here. If you change the XTick locations, the labels will automatically be updated to reflect the new locations.
line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca, 'XScale', 'log', ...
'XTick', [1 10 20 50 100], ...
'YScale', 'log')

Bar graph starting from zero

I have a vector with 11 elements. I want to display the graph as in this picture:
but my graph starts from first month. How can I make it start from the zeroth month?
You can use both an x and y input argument for bar.
ax = axes();
x = 0:11;
bar(x,y);
If that doesn't give you the plot you want you can also use the Xlim, XTick, and XTickLabel properties of to control how the x axis looks.
set(ax, 'Xlim', [0,11])
set(ax, 'XTick', [0:11])

Creating Surf() with Labels

[ndata, text, alldata] = xlsread('Euro swap rates.xlsx',3);
%(although created from text dates is still a cell array?)
dates=text(:,1);
%(Same problem here)
rates_header=text(1,:);
rates=ndata;
surf(rates);
colormap(jet);
>> surf(rates,dates); %but when I try to add wither of the labels I get a problem?
??? Error using ==> surf at 78
X, Y, Z, and C cannot be complex.
>> surf(rates,dates,rates_header);
??? Error using ==> surf at 78
Z must be a matrix, not a scalar or vector.`
I'm assuming the problem is because dates and rates_header are still cell arrays?
How can I convert them to text? Is there a way to do it directly as part of xlsread?
Lastly on the plot I would like to make the first cell in the arrays dates and rates_header, the name for that axis of the surf plot with all the rest of the data being used to populate the axis.
getting closer
title('Euro Swap Rates');
xlabel('Maturity');
ylabel('Date');
zlabel('Swap Rate');
set(gca, 'YTick', 1:100:length(dates));
set(gca, 'YTickLabel', dates(1:100:length(dates)));
set(gca, 'XTick', 0:10:length(rates_header));
set(gca, 'XTickLabel', rates_header(0:10:length(rates_header)));
Two questions remain:
I would like the x Tick to be 1y then 10y20y...60y So that the first step size is 9y then 10y for all remaining points
I would like the dates to show the 1st of Jan and ist of June each year only (or the closest working days to those dates).
It looks like you want to set the labels on the x-axis.
You don't do this through surf, you do it after using set, like this:
set(axHandle, 'XTickLabel', xlabels);
Here is a complete example:
[x y] = meshgrid(-4:.25:4;);
z = x.^2 + y.^2;
xlab = {'one','two','three','four'};
surf(x,y,z);
set(gca,'XTickLabel', xlab);
You can use whatever labels you want provided that they are strings and saved to a cell array. This is what I did above for xlab.