IOS-Charts cannot show any x-axis labels in a line chart - ios-charts

(Hopefully simple question)
How do I get X-axis labels to show. I want four words to show in a line chart with multiple plots.
The plots and everything else works beautifully!

You need to specify the x-axis labels title when you create the LineChartData object:
let chartData = LineChartData(xVals: labels, dataSet: dataSet)
chartView.data = chartData
The xVals parameter is the x-axis label title array.
You can check out how it's done in the example project: https://github.com/danielgindi/ios-charts, ChartsDemo folder

Related

How to plug in cell arrays in the x-axis of a plot?

I would like to make a plot in which I will the x-axis contains the values of a cell array.
I am using R2019a.
t.Format = 'dd-MMM-yyyy'
time = cellstr(t)
A = randn(3,1)
figure;
filename = plot(datetime(time),A);
xlim([min(datetime(time)) max(datetime(time))])
As an output, I am getting a graph that contains dates and time in the x-axis. The desired output should be limited in showing only the dates.
Use datetick:
datetick('x','dd-mmm-yyyy')

Creating a legend in MATLAB that includes scatter plots and normal plots

I want my legend to include the line from the plot and the marker from the scatterplot. For example,
rest = importdata('test.xlsx');
x = test.data(:,1);
y = test.data(:,2);
xx = min(x):0.001:max(x);
yy = interp1(x,y,xx,'cubic');
figure
s1 = scatter(x,y, 'filled', 'k');
hold on
p1 = plot(xx,yy, '--k');
legend(p1, 'x1');
This code creates the legend with only the dashes from the plot and not points from the scatterplot. I would like the legend to have the both the point and the dashed line at the same label. Something like "-.-"
Any help is much appreciated.
Thanks.
Option 1
Make a dummy plot with no data (nan) for the legend (also, as you can see here you can plot all the elements with one call to plot:
p = plot(nan,nan,'--ok', xx,yy,'--k', x,y,'ok');
set(p,{'MarkerFaceColor'},{'k'}); % fill the circles
legend('x1');
The result:
Option 2
Insted of legend(p1, 'x1');, write this:
[~,ico] = legend(p1,'x1'); % create the legend, and get handels to it's parts
ico(3).Marker = 'o'; % set the marker to circle
ico(3).MarkerFaceColor = 'k'; % set it's fill color to black
ico is:
3×1 graphics array:
Text (x1)
Line (x1)
Line (x1)
The first element is the text 'x1' in the figure. The second element is the dashed line, and the third is the (not-exist) marker of p1. This third element is reserved for cases like plot(xx,yy,'--ok'); where the legend include both marker and a line, but the line (in the legend) is represented with two points and the marker with only one, so we need different objects for them. Try to see what happens if you type ico(2).Marker = 'o'; in the example above.
Legend in MATLAB is additional axes that contains the same primitive object like lines and text.
If you want to draw custom legend the simple way will be using primitive commands line, text and patch for rectangles with filling. Also you can add one more axes object as a container.
By specifying p1 in the legend command you are telling MATLAB to only insert an item in the legend for the line corresponding to handle p1 - which is what you are seeing.
In your example case you just want
>>legend({'label_for_scatter','label_for_plot'});

How to put random labels in stacked bar plot matlab

I create a horizontally stacked bar plot as below:-
data(1,:)=[0,55,87,96,97,98,99,100,102,125,130];
data(2,:)=[0,55,65,107,110,129,131,0,0,0,0];
data(3,:)=[0,60,104,108,128,130,0,0,0,0,0];
barh(data,'stacked')
axis ij
set(gca, 'xlim',[0,1000], 'box','off');
The output is this:
My question is that i want to put labels inside each box randomly like say this:-
Labels can be any digit or any letter, not neccesarily 1.
Here is one option:
y = repelem(1:size(data,1),size(data,2)-1);
x = (cumsum(data(:,2:end),2)-data(:,2:end)./2).';
labels = data(:,2:end).'>0;
text(x(labels),y(labels),num2str((1:nnz(labels)).'))
This will print a number increasing by 1 in each box:
If you want to put random numbers, replace (1:nnz(labels)).' with rand(nnz(labels),1). If you want to put characters or mixed content use a cell array with one cell for a label.

Using datetick to label x axis: but don't show the last tick label

Does anyone know how to remove the last ticklabel on a plot in Matlab AFTER using the datetick function to put the labels there?
I am plotting Y data and X dates (years and months converted to a datenum).
Then I am using the following to plot the year labels on the xaxis:
close all;clear all;clc;
[num,txt,raw] = xlsread('data.xlsx');
yr = num(:,1);
mth= num(:,2);
data= num(:,3);
dates=datenum(yr,mth,1);
plot(dates,data,'r-.','linewidth',2);
dateFormat = 10;
datetick('x',dateFormat)
I would like to remove the last tick label, as it is including a year that isn't in the datset (presumably Matlab is optimising the distance between ticks and interpolating to the next year).
You should set the ticks you want on your chart first, e.g.:
set(gca, 'XTick', x_values_you_want_ticks_at);
Then use datetick with 'keepticks' option, which will preserve your ticks location:
datetick(gca, 'x', dateFormat, 'keepticks');

How To Put String Labels on Contours for Contour Plots in MATLAB

I am wondering if it is possible to label the contours of a MATLAB contour plot with a set of user-defined strings?
I am currently using the following code snipper to produce a labelled contour plot:
%Create Data
X = 0.01:0.01:0.10
Y = 0.01:0.01:0.10
Z = repmat(X.^2,length(X),1) + repmat(Y.^2,length(Y),1)';
%Create Plot
hold on
[C,h] = contourf(X,Y,Z);
%Add + Format Labels to Plot
hcl = clabel(C,h,'FontSize',10,'Color','k','Rotation',0);
set(hcl,'BackgroundColor',[1 1 1],'EdgeColor',[0 0 0],'LineStyle','-',)
hold off
The issue with this code is that the labels are automatically generated by MATLAB. Even as I can easily change the contours that are labels, I cannot change the labels that they get.
Ideally, I would like to label them with a set of strings that I define myself. However if that is not possible, then I am wondering if it is possible to change the numeric format of the labels. The reason for this is that the code above actually produce a contour plot for an error rate, which I would like to display as a % value (i.e. use 1% in the contour label, instead of 0.01 etc.).
In this case, hcl is actually an array which stores handles to every contour label on your plot. When you set properties using the array (as in your code),
set(hcl, 'name', 'value')
You will set the property of every label to the same value.
You can change the properties of individual labels by iterating over the array. For example, this is how you would add a percentage sign:
for i = 1:length(hcl)
oldLabelText = get(hcl(i), 'String');
percentage = str2double(oldLabelText)*100;
newLabelText = [num2str(percentage) ' %'];
set(hcl(i), 'String', newLabelText);
end